Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C#] String extraction/setting impossible without memory allocation with current API #729

Open
rca22 opened this issue Oct 4, 2019 · 0 comments

Comments

@rca22
Copy link

rca22 commented Oct 4, 2019

Using your example in the documentation

// Create a vehicle code of the correct encoding
VehicleCode = Encoding.GetEncoding(Car.VehicleCodeCharacterEncoding).GetBytes("abcdef");
car.SetVehicleCode(vehicleCode, 0); // second argument is offset

This is terrible. It defeats the whole point of SBE because it uses a method GetBytes() which allocates a new array of bytes, and then copies that to the direct buffer we are targeting.
We should be using this method to get the number of bytes which a particular string would generate.
We should then check that against the size available, and use
this method to write to the underlying buffer.

In order to do this, we need to be able to access the underlying byte array for the DirectBuffer, and it would be best to do this through support in the generated classes.

I can't see any way of writing a string into one of the generated classes without having auxiliary buffers of bytes hanging around if I want to avoid allocations, the way the accessor methods are currently provided.

Similarly to read a string, your example is

var vehicleCode = new byte[Car.VehicleCodeLength];
car.GetVehicleCode(vehicleCode, 0);
sb.Append(Encoding.GetEncoding(Car.VehicleCodeCharacterEncoding).GetString(vehicleCode, 0, Car.VehicleCodeLength));

Which suffers from the same problem...
We should be using this method to generate a string directly (perhaps having first looked through the buffer for a terminating \0 character to establish how many of the available bytes actually comprise the string we want to extract)

mjpt777 pushed a commit that referenced this issue May 25, 2021
)

* [C#] Add more string reading/writing methods to DirectBuffer
More, hopefully efficient, mechanisms to read and write strings to and from the DirectBuffer.

* [C#] Reverted to .net45

* [C#] Reverted to .net45

* [C#] Updated Samples to use the new String methods

* [C#] Fixed checkstyle

* [C#] Cleaning

* Added Modified Benchmarks\nBoth a modified version of the the CarBenchmark which encodes and decodes to/from string; and a version wich uses the new methods to encode and decode strings

Co-authored-by: Rob Purdy <rob.purdy@iongroup.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant