Skip to content

Conversation

@vsmcea
Copy link
Contributor

@vsmcea vsmcea commented Oct 22, 2025

Hypothesis: len, pos, padLeft and padRight are expressed in bytes.

  • Add checks for alignment of lengths in bytes against Sizeof().
  • Avoid excessive buffer size allocation.
  • Make span and non-span versions functionaly equivalent.

Avoid allocating too large buffers (len is expressed in bytes, not in Ts).
Added validation to ensure len is a multiple of SizeOf<T>() before converting to array.
@google-cla
Copy link

google-cla bot commented Oct 22, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added the c# label Oct 22, 2025
I understand from failed test that pos, len, padLeft and padRight are expressed in Ts
@bjornharrtell
Copy link
Collaborator

So this intends to fix #8717 right? @vsmcea you need to sign the Google CLA to allow the project to accept your contribution.

The code change looks promising but there is a test failure for "Build .NET Windows" which does seem related to your changes so that needs to be adressed.

@vsmcea
Copy link
Contributor Author

vsmcea commented Oct 23, 2025

Ok, I managed to build and run the tests on my own, will try to find time today to work on it and provide a correction that passes the test.

@vsmcea
Copy link
Contributor Author

vsmcea commented Oct 23, 2025

I think there is an issue with pos and len signification. Are they expressed in bytes or in Ts?
Running the test in debug mode, I see coherence issues. For exemple, in function "ByteBuffer_Put_Array_Helper" at line "var bbArray = uut.ToArray(nOffset, data.Length);", nOffset is in bytes and data.Length is in Ts. I'm not capable of understanding the logic on my own, sorry.

@bjornharrtell
Copy link
Collaborator

Do you have any input here @petersvp?

@petersvp
Copy link
Contributor

petersvp commented Oct 23, 2025

Hello. Right now I cannot test all possible scenarios - feel free to revert my commit. I merely need these for a game I'm working on to optimize allocations - didn't even checked most other defines that might have existed.

EDIT 2: Looks like i created a chain of mess after me :// And I'm very sorry for this...

@bjornharrtell
Copy link
Collaborator

bjornharrtell commented Oct 23, 2025

@petersvp no worries, it's not easy to catch regressions when not found in tests. The regression went into flatbuffers because a test case/setup was and is missing for the scenario (i.e a test run with enabled ENABLE_SPAN_T and UNSAFE_BYTEBUFFER). So I think that should be adressed in any attempt to fix the regression, both to verify the fix and to avoid future regressions.

@aardappel can you provide insight here about the ByteBuffer details mentioned at #8734 (comment)?

@aardappel
Copy link
Collaborator

@bjornharrtell @vsmcea offsets and sizes are meant to be in bytes, since this functionality is used to access different sizes of types.

That said, the C# ByteBuffer was simply a clone of whatever we had in the Java implementation, to make porting easier, but it this point it can do whatever you want it to do. But universally working with bytes is definitely more sane.

@vsmcea
Copy link
Contributor Author

vsmcea commented Oct 24, 2025

OK I agree, so if you don't mind I will revert to my first correction that made the assumption that parameters are expressed in bytes and correct the unitary test to make it coherent with that. And add a test for ENABLE_SPAN_T and UNSAFE_BYTEBUFFER.

@bjornharrtell
Copy link
Collaborator

Sounds good @vsmcea, thanks for your efforts.

All functions parameters expressed in bytes for homogeneity
Tests run:
  - UNSAFE_BYTEBUFFER=true/ENABLE_SPAN_T=true: passed
  - UNSAFE_BYTEBUFFER=true/ENABLE_SPAN_T=false: passed
  - UNSAFE_BYTEBUFFER=false/ENABLE_SPAN_T=false: passed
  - UNSAFE_BYTEBUFFER=false/ENABLE_SPAN_T=true: configuration forbidden by compilation
Correction of FlatBuffers.Test.csproj to allow UNSAFE_BYTEBUFFER/ENABLE_SPAN_T tests
Correction of FlatBuffersExampleTests.cs: I think the test was not run because it could not pass (to be reviewed carefully)
@vsmcea
Copy link
Contributor Author

vsmcea commented Oct 24, 2025

Just signed my CLA but don't kown how to relaunch the check.
Another thing: I corrected the test .csproj to allow unsafe/span tests, but I don't know how to trigger unsafe/span compilation and test.

Copy link
Collaborator

@bjornharrtell bjornharrtell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good to me!

@bjornharrtell
Copy link
Collaborator

@aardappel will you consider this for merge?

@aardappel
Copy link
Collaborator

Thanks @vsmcea @bjornharrtell

@aardappel aardappel merged commit 95053e6 into google:master Oct 25, 2025
49 checks passed
@vsmcea vsmcea deleted the vsmcea-patch-ByteBuffer branch October 26, 2025 08:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants