Skip to content

Conversation

petersvp
Copy link
Contributor

@petersvp petersvp commented Aug 3, 2025

I've added 2 new functions to C#'s ByteBuffers:
public byte[] ToSizedArrayPadded(int padLeft, int padRight)
public T[] ToArrayPadded<T>(int pos, int len, int padLeft, int padRight)

I needed those to create byte buffers with room in front of them where i can hotwire a header containing the type info and/on package length for TCP Framing.

Code that calls .ToSizedArray only to create new byte array and yet another copy to add some padding wasn't the best approach, so I decided to add the padding feature in the library itself. This is great for highly optimized networking.
My old framing code was the bottleneck because of Buffer.BlockCopy that is after a Buffer.BlockCopy during the .ToSizedArray()

Consider adding something similar to other languages thay may need padding during their framing / similar use cases, where ToSizedArray is THE thing that creates the initial byte array.

…os, len, padLeft, padRight) to the byteBuffers.

This is for API completion and to avoid unnecessary copy when framing my packets. I needed this to create a flat buffer with space in front of it for header / metadata.
Copy link

google-cla bot commented Aug 3, 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 Aug 3, 2025
@petersvp petersvp changed the title Added ToSizedArrayPadded(int padLeft, int padRight) to ByteBuffers to avoid unnecessary copying. [C#] Added ToSizedArrayPadded(int padLeft, int padRight) to ByteBuffers to avoid unnecessary copying. Aug 3, 2025
@bjornharrtell bjornharrtell self-requested a review August 24, 2025 15:11
@bjornharrtell
Copy link
Collaborator

@aardappel consider merge

@bjornharrtell bjornharrtell self-requested a review September 9, 2025 17:10
@aardappel aardappel enabled auto-merge (squash) September 9, 2025 18:22
@aardappel aardappel disabled auto-merge September 9, 2025 18:38
@aardappel aardappel merged commit 3c0511f into google:master Sep 9, 2025
48 of 49 checks passed
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.

3 participants