Skip to content

[C#] Support Span<T> instead of / on top of T[] for array getters/setters #587

Closed
@damageboy

Description

@damageboy

Hi,
I'm opening this issue to start a minimal discussion before I go ahead and attempt to implement a change to the code generation that sbe does for C#.

C# has support for Span<T> which can be used to encapsulate various types of array of T where those can be:

  • Unmanaged memory
  • stack allocated memory
  • Existing managed / GC memory

If we take the generated code for Car from: https://github.com/real-logic/simple-binary-encoding/blob/master/csharp/.nuget/examples/baseline/Car.cs

Here's a short relevant excerpt:

https://github.com/real-logic/simple-binary-encoding/blob/8ec67b1c28ce70b845ad43839f309dc7bf7b5473/csharp/.nuget/examples/baseline/Car.cs#L964-L986

We can clearly see that SBE currently generates Get/Set method pairs that can take as input/output a managed array of the underlying type (byte in case of a string).

I would like to either replace or add additional Get/Set methods that accept a Span<byte> in this case, or a Span<T> in the general case.

Adding such Getters/Setters would allow users to copy strings / arrays out of the message into stack allocated memory, for example, without requireing to have a managed buffer as it currently required, for example in the example usage for the same sample:
https://github.com/real-logic/simple-binary-encoding/blob/8ec67b1c28ce70b845ad43839f309dc7bf7b5473/csharp/.nuget/examples/CarExample.cs#L184

https://github.com/real-logic/simple-binary-encoding/blob/8ec67b1c28ce70b845ad43839f309dc7bf7b5473/csharp/.nuget/examples/CarExample.cs#L259

I think this could be a nice first step in improving SBE's friendliness for high-perf / zero allocation users.

As a second step I ALSO think it would be great if additional GetXXX() methods would be introduced returning a Span<T> pointing to the internal memory, but I would first want to get this done before opening a separate issue on that matter and implementing that.

Any thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions