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

Will rayon runtime use heap allocation in idle state? #3

Open
noname0310 opened this issue Feb 8, 2024 · 0 comments
Open

Will rayon runtime use heap allocation in idle state? #3

noname0310 opened this issue Feb 8, 2024 · 0 comments

Comments

@noname0310
Copy link

When memory.grow on the WASM side, all typed array based pointers created in js are invalidated.
see: rustwasm/wasm-bindgen#2222

So if your code has any processing on the JS side, threading can be dangerous

The parallel iterator is still safe because it blocks the main thread until all tasks are finished. However, if any of the tasks executed (by the rayon runtime) while the rayon threads are waiting have heap allocations, this library is potentially dangerous to use with JS-side code.

Here is sample code for the scenario described earlier

#[wasm_bindgen]
pub fn foo() {
    let v = [1, 2, 3];
    // this code causes a heap allocation but it's blocking the main thread so it's fine
    let _ = v.iter().map(|x| x + 1).collect::<Vec<_>>();
    // but after this code, there is potential to cause a heap allocation by worker threads idling tasks(?)
}

#[wasm_bindgen]
pub fn allocate_buffer(size: usize) -> *mut u8 {
    let mut vec = vec![0; size].into_boxed_slice();
    let ptr = vec.as_mut_ptr();
    std::mem::forget(vec);
    ptr
}
let ptr = wasm.allocate_buffer(10);
let t = new Uint8Array(memory.buffer, ptr, 10); // create typed array based pointer

t[0] = 1;

wasm.foo(); // the pointer may be invalidated because memory.grow may be executed
if (t.length !== 10) {// So we check to see if the pointer is valid
    t = new Uint8Array(memory.buffer, ptr, 10); // If it is invalidate, create a new pointer
    // In a WASM in a single-threaded environment,
    // we only need to check that the pointer is valid once before accessing it,
    // but in a multithreaded scenario,
    // this is not a safe thing to do because memory.grow can be executed by a worker thread.
}

console.log(t[0]);

For this reason If the rayon runtime is guaranteed not to do any heap allocation when all workerthreads are idle, it seems to be safe to use. So I'm curious about this.

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