flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

[C#] GetByKey needs an allocation-free overload

Open CTVjweiss opened this issue 3 years ago • 1 comments

I'm using Flatbuffers in C# because I want an allocation free way of accessing my data. However, the current implementation of GetByKey() has only one pathway that takes in a string and converts it to a newly allocated byte[] in the generated __lookup_by_key function.

In my opinion this goes against the fundamental design principles of FlatBuffers of minimizing allocations whenever possible. I think it would be pretty easily fixed doing one of the following:

  1. Allow the caller to pass in an ArraySegment or a byte[] with offset and length that already points to the UTF8 encoded string.
  2. Add a byte[] parameter that defaults to null for GetByKey() that will be used for encoding to UTF8 for the binary search. If passed in, rely on the caller having passed a large enough byte array to the conversion to use the UTF8.GetBytes() function that writes to an existing array.

CTVjweiss avatar May 03 '22 21:05 CTVjweiss

Feel free to submit a PR for this.

dbaileychess avatar May 11 '22 16:05 dbaileychess

This issue is stale because it has been open 6 months with no activity. Please comment or label not-stale, or this will be closed in 14 days.

github-actions[bot] avatar Mar 04 '23 01:03 github-actions[bot]

This issue was automatically closed due to no activity for 6 months plus the 14 day notice period.

github-actions[bot] avatar Mar 19 '23 20:03 github-actions[bot]