-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add an unsafe method Uint8Array::view_mut_raw(ptr: *mut u8, length: usize)
so that JS can efficiently initialize a wasm buffer
#1643
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
Comments
Seems reasonable to me to add! I'm not 100% convinced we need this for mutability reasons, it seems a long stretch for anything bad to happen. Having this for unininitialized memory though is more compelling to me and seems pretty reasonable! |
In the same vein, I'd like to see a version of |
Oops, I think I meant |
|
Seems reasonable to me to add as well! |
@That3Percent Do you mean you want to be able to pass the uninitialized |
is there any way to check if |
Here's the workflow as it exists today and how it could be improved by the issue. Task: Given some Today:
Step 2 is what we want to get rid of. Why write 0 when it's going to be overridden immediately? But, it's not possible without invoking Undefined Behavior, because it's not valid to even have a Proposed:
|
I just took a look at #1855 . I think that what you built there is a decent step toward a more performant version of the existing |
@That3Percent does this match your use case?
It is not exactly what you asked for, the signature of the function is:
|
@That3Percent I think you can use the existing
Do I miss anything? Here is the entire test case:
|
Can anyone review the |
This touches more APIs, for example: pub fn read_pixels_with_opt_u8_array(
this: &WebGlRenderingContext,
(...)
pixels: Option<&mut [u8]>,
) -> Result<(), JsValue>; Currently there's no (legal and sound from Rust's POV) way of avoiding zero-initialization without changing this API. |
Motivation
I am using the following pattern to efficiently initialize a large buffer inside wasm memory from JavaScript without copies (see #1079 for full motivation):
There are two problems with this:
Uint8Array::view
takes an immutable slice ref, but then I go ahead and mutate the contents. Lying to the compiler is dangerous.If I wanted to eliminate the zero initialization, I could do that using
reserve
+set_len
on the vec. However, rust forbids wrapping uninitialized values in a slice - you have to use raw pointers when initializing uninitialized memory. But the current signature ofUint8Array::view
requires me to make a slice.Proposed Solution
I propose adding a new method to
Uint8Array
(and maybe to the other typed array variants):pub unsafe fn view_mut_raw(ptr: *mut u8, length: usize)
This would let me change my rust code to the following:
It solves the problems: We've eliminated the zero initialization, we're not constructing a slice around uninitialized memory, and we're not lying to the compiler about the mutation anymore.
Alternatives
I'm not aware of any obvious alternatives.
The text was updated successfully, but these errors were encountered: