feat: add Span<T> params to C# struct Create methods for zero-allocation stackalloc support#8970
feat: add Span<T> params to C# struct Create methods for zero-allocation stackalloc support#8970statxc wants to merge 3 commits intogoogle:masterfrom
Conversation
…ion stackalloc support
|
@jtdavis777 Could you please review this PR? I'd appreciate any feedbacks. Thanks! |
|
@statxc In practice, it is better to use |
Yes. Good point! 100% right. Our Create methods only read from the array parameters - they never write to them. So the parameter type should be ReadOnlySpan instead of Span. Thanks @ODtian 👍. I will update soon |
|
@ODtian I updated all! Could you review again? thanks |
|
@statxc The rest looks good to me. I'm not very familiar with FlatBuffers itself though, so it'd be best to have someone with more expertise take a look. |
|
@jtdavis777 could you please review this PR? Welcome to any feedbacks |
|
@jtdavis777 please give me any updates 🙏 |
Summary
Span<T>parameters to C# structCreateXXXmethods under#if ENABLE_SPAN_T, enablingstackallocfor zero-GC struct creationT[,]) to 1DSpan<T>with computed indices for nested struct arraysT[]still compiles since arrays implicitly convert toSpan<T>Changes
src/idl_gen_csharp.cpp: AddSpan<T>codegen path withStructHasArrayFields()helper and flat indexing supporttests/MyGame/Example/{NestedStruct,ArrayStruct,LargeArrayStruct}.cs: Regenerated viaflatctests/FlatBuffers.Test/FlatBuffersExampleTests.cs: Updated test for flat array params underENABLE_SPAN_Ttests/FlatBuffers.Test/FlatBuffersFixedLengthArrayTests.cs: Addedstackalloctest demonstrating zero-allocation usageHow to test
Test plan
UNSAFE_BYTEBUFFER=true: 156 tests passedENABLE_SPAN_T=true+UNSAFE_BYTEBUFFER=true: 156 tests passedflatcoutput (zero diff)Closes #8927