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#: Use Spans instead of e.g. byte[] #1080

Open
jsturtevant opened this issue Nov 6, 2024 · 3 comments · May be fixed by #1138
Open

c#: Use Spans instead of e.g. byte[] #1080

jsturtevant opened this issue Nov 6, 2024 · 3 comments · May be fixed by #1138
Assignees
Labels
gen-c# Related to the C# code generator

Comments

@jsturtevant
Copy link
Collaborator

Use Spans in wit-bindgen-generated code to minimize copies (example while using streams)

@jsturtevant
Copy link
Collaborator Author

#1138 address using span/Memory for parameters where they are explicitly definded (such as in the WriteAsync) but the link above also points to a different type of API where the paramater isn't a list but a value of the number of items to fill.

// TODO: should we add a special case to the bindings generator
                    // to allow passing a buffer to IStreams.InputStream.Read and
                    // avoid the extra copy?
                    var result = input.Read(16 * 1024);

I think the intent of the comment is that we would have a functions like:

input.Read(buffer.AsSpan());
// use buffer that was filed

The streams wit looks like

@since(version = 0.2.0)
        read: func(
            /// The maximum number of bytes to read
            len: u64
        ) -> result<list<u8>, stream-error>;

We could return a Span here but I don't know how we detect that we should re-write the input.Read to special case it for taking a span and filling it.

@pavelsavara
Copy link

I think the intent is that we will use buffer provided by the caller.

Bring You Own Buffer

And that the generated C# proxy would allow that to maximal possible extent. Perhaps even passing that buffer to the host where appropriate.

@jsturtevant
Copy link
Collaborator Author

The hang up for me here is how do translate the WIT:

read: func(len: u64) -> result<list<u8>, stream-error>;

Into a function, that we could pass a buffer so that it can be filled, ie input.Read(buffer.AsSpan());.

Other than the specific wasi interface name there aren't any real heuristics to know that a function signature of read: func(len: u64) -> result<list<u8>, stream-error>; should be translated to input.Read(buffer.AsSpan()). But maybe I am missing something?

I am going to propose to not worry about his for now, since the API will change with WASI 0.3 and there is also discussions around a FillableArray type in 0.3.x that would solve this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gen-c# Related to the C# code generator
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

2 participants