flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

FR [C#]: Allow parsing IList<byte> objects as Flatbuffer tables

Open rhofour opened this issue 3 years ago • 3 comments

Right now to parse data as a Flatbuffer table (in C#) requires a ByteBuffer which must be built from a byte array. This means if I have something that looks like an array (such as a ArraySegment) I have to make a copy before I can interpret it as a Flatbuffer table.

Instead, it would be nice if we could take any object which allows indexing (such as IList) and interpret it as a flatbuffer.

To implement this we can't simply allow ByteBuffer to be built from any IList type because ByteBuffer supports a bunch of mutating operations used for constructing Flatbuffer objects. Instead I think what would be needed is a new ReadOnlyByteBuffer which supports just the reading operations.

rhofour avatar Oct 10 '21 14:10 rhofour

Such indirections sometimes come with performance tradeoffs, and it is pretty important that basic reading/writing of bytes is not impacted. @dbaileychess

aardappel avatar Oct 14 '21 19:10 aardappel

I'm open to PRs to see how it would look. I'm not looking to implement this myself at the moment.

dbaileychess avatar Dec 13 '21 21:12 dbaileychess

I came across this exact scenario where I was using pooled byte arrays that were larger than the actual content and solved it this way.

/// <summary>
/// A ByteBufferAllocator that does not require the byte[] to be exactly sized
/// </summary>
public class ArraySegmentByteBufferAllocator : FlatBuffers.ByteBufferAllocator
{
    public ArraySegmentByteBufferAllocator(byte[] buffer, int length)
    {
        this.Buffer = buffer;
        this.Length = length;
    }

    public override void GrowFront(int newSize)
    {
        throw new NotImplementedException();
    }
}

Then you can make a ByteBuffer using that allocator:

byte[] oversizedByteArray = null;
int actualContentLength = 0;
var fbByteBuffer = new FlatBuffers.ByteBuffer(new ArraySegmentByteBufferAllocator(oversizedByteArray, actualContentLength), 0);

thansen24 avatar Mar 09 '22 14:03 thansen24

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 18 '23 20:03 github-actions[bot]