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
need example of sharing memory between JS and Rust #2456
Comments
Hey there. I think that this should do the trick -> https://rustwasm.github.io/wasm-bindgen/reference/types/pointers.html |
Sorry let me clarify. One approach off the top of my head would be to:
Let me know if you have any questions. Best of luck. |
@chinedufn Thanks so much! I think I'm close to having this working. After populating the shared memory on the JS side I pass the pointer and the number of elements to the Rust side. In the Rust code I can iterate over the numbers, but I don't know how to get them. I assume I have to do something like this with an #[wasm_bindgen]
pub fn sum(ptr: *mut f64, count: usize) -> f64 {
let mut sum = 0.0;
unsafe {
for i in 0..count {
let number = ptr[i * 8] as f64; // THIS LINE IS WRONG!
sum += number;
}
}
sum
} |
I have it working now, but the Rust version still is much slower than the JS version. |
Potential reasons:
You might be running into any number of those. Glad it all works. If you're in good shape feel free to close this issue. Good luck. |
Also you'll want to store that Something like: #[wasm_bindgen]
struct MyApp {
my_data: Vec<f32>
}
#[wasm_bindgen]
impl MyApp {
#[wasm_bindgen(constructor)]
fn new (capacity: usize) -> Self {
MyApp { my_data: Vec::with_capacity(capacity) }
}
} Then when you are summing you don't need to allocate a new vector. Although, as alluded to above, summing might not be the most informative/realistic/useful benchmark. I'm just sharing some tips under the assumption that you're mainly just experimenting. |
Interesting. If I use this approach of preallocating the vector by creating an instance of |
If you haven't already, have a look at the game of life tutorial https://rustwasm.github.io/book/introduction.html . |
Also see #1643 and #1993 (comment). |
Here is what I did. I allocate a vector is WASM and then pass it as a view (via view_mut_raw) to JS. I can then write a function that expects an #[wasm_bindgen]
pub struct WasmUint8Array(Vec<u8>);
#[wasm_bindgen]
impl WasmUint8Array {
#[wasm_bindgen(constructor)]
pub fn new(size: usize) -> Self {
let buffer = vec![0; size];
Self { 0: buffer }
}
#[wasm_bindgen(getter, js_name = buffer)]
pub fn buffer(&mut self) -> js_sys::Uint8Array {
unsafe { js_sys::Uint8Array::view_mut_raw(self.0.as_mut_ptr(), self.0.len()) }
}
} |
I think everything here looks like it's been answered and handled (thanks @chinedufn!), so I'm going to close. |
I want to create a
Float64Array
in JS code and make it available to a Rust function through shared memory without passing/copying it. I've spent quite a bit of time looking for an example of doing this and haven't found one. Does wasm-bindgen support this? If so, can you point me to an example?The text was updated successfully, but these errors were encountered: