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

Avoid copy when the plugin returns #13

Merged

Conversation

arnaudgolfouse
Copy link
Collaborator

This should avoid copying the data until another function is called.

@arnaudgolfouse
Copy link
Collaborator Author

arnaudgolfouse commented Aug 5, 2023

Some notes:

Once in a lifetime !

Functions now return ReturnedData<'a>, preventing any other function being called while this structure is alive.

Technically the data returned by a function should no longer be accessible from inside the plugin after the return (if the plugin respect the spirit of the protocol), so maybe we could get away with the lifetime if absolutely needed (using a Rc<RefCell>/Arc<Mutex> for example).

Why not return ptr/len directly ?

The call to wasm_minimal_protocol_send_result_to_host seems redundant, does it not ? Well, I tried having plugin functions be something like

pub extern "C" fn hello() -> (*const u8, usize, i32) {
    let message = String::from("Hello world!");
    let (ptr, len, code) = (message.as_ptr(), message.len(), 0);
    std::mem::forget(message);
    (ptr, len, code)
}

But it does not seems quite ready yet... It works on wasm32-unknown-unknown by setting the env variable RUSTFLAGS="-C target-feature=+multivalue", which is already not ideal ; but then this breaks when using wasm32-wasi. See this (opened) issue: rust-lang/rust#73755

@astrale-sharp
Copy link
Owner

This looks great, thanks a ton, it doesn't seem to be missing anything either, are you okay with me merging it?

<3

@arnaudgolfouse
Copy link
Collaborator Author

Oh wait, rereading this I see I slightly messed up the documentation

@arnaudgolfouse
Copy link
Collaborator Author

Ok, looks good to me !

@arnaudgolfouse arnaudgolfouse mentioned this pull request Aug 6, 2023
@astrale-sharp astrale-sharp merged commit aa695aa into astrale-sharp:master Aug 6, 2023
@arnaudgolfouse arnaudgolfouse deleted the avoid-return-copy branch September 15, 2023 09:27
arnaudgolfouse pushed a commit to arnaudgolfouse/wasm-minimal-protocol that referenced this pull request Sep 21, 2023
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

Successfully merging this pull request may close these issues.

2 participants