[C#] Improve Span<> utilization (#8588)

There are a couple instances where the ByteBuffer's Span property was accessed in a loop.
 + Extracted the use of the property outside of the loop to save a few cpu cycles.

Access to the allocator's internal buffer isn't exposed as a ReadOnlySpan<byte> from the ByteBuffer
or the FlatBufferBuilder.
 + Added a few convenience functions to access the buffer using a ReadOnlySpan<byte>.

There are a few cases where built in Span extensions can be used to run optimized code.
 + Added the use of Span.Fill() and ReadOnlySpan.SequenceCompareTo to replace existing loops.

Co-authored-by: Björn Harrtell <bjornharrtell@users.noreply.github.com>
Co-authored-by: Wouter van Oortmerssen <aardappel@gmail.com>
This commit is contained in:
bigjt
2025-08-28 19:06:18 -04:00
committed by GitHub
parent a6b337f803
commit 82396fa0fe
3 changed files with 45 additions and 6 deletions

View File

@@ -957,6 +957,21 @@ namespace Google.FlatBuffers
return _bb.ToSizedArray();
}
#if ENABLE_SPAN_T && UNSAFE_BYTEBUFFER
/// <summary>
/// A utility function return the ByteBuffer data as a
/// `ReadOnlySpan<byte>`.
/// </summary>
/// <returns>
/// A `ReadOnlySpan<byte>` that references the internal
/// ByteBuffer data.
/// </returns>
public ReadOnlySpan<byte> SizedReadOnlySpan()
{
return _bb.ToSizedReadOnlySpan();
}
#endif
/// <summary>
/// Finalize a buffer, pointing to the given `rootTable`.
/// </summary>