-
Notifications
You must be signed in to change notification settings - Fork 5.8k
experiment: faster wasm streaming in Rust #21323
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
|
||
| use deno_core::error::type_error; | ||
| use deno_core::error::AnyError; | ||
| use deno_core::v8; | ||
| use deno_core::OpState; | ||
| use deno_core::ResourceId; | ||
| use std::cell::RefCell; | ||
| use std::rc::Rc; | ||
|
|
||
| /// The Wasm streaming compilation pipeline. | ||
| pub fn handle_wasm_streaming( | ||
| state: Rc<RefCell<OpState>>, | ||
| scope: &mut v8::HandleScope, | ||
| value: v8::Local<v8::Value>, | ||
| mut wasm_streaming: v8::WasmStreaming, | ||
| ) { | ||
| let (url, rid) = match compile_response(scope, value) { | ||
| Ok(Some((url, rid))) => (url, rid), | ||
| Ok(None) => { | ||
| // 2.7 | ||
| wasm_streaming.finish(); | ||
| return; | ||
| } | ||
| Err(e) => { | ||
| // 2.8 | ||
| let err = v8::String::new(scope, &e.to_string()).unwrap(); | ||
| wasm_streaming.abort(Some(err.into())); | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| wasm_streaming.set_url(&url); | ||
|
|
||
| deno_core::unsync::spawn(async move { | ||
| loop { | ||
| let resource = state.borrow().resource_table.get_any(rid); | ||
| let resource = match resource { | ||
| Ok(r) => r, | ||
| Err(_) => { | ||
| state.borrow().borrow::<deno_core::V8TaskSpawner>().spawn( | ||
| move |scope| { | ||
| wasm_streaming.abort(Some( | ||
| v8::String::new(scope, "Failed to get resource.") | ||
| .unwrap() | ||
| .into(), | ||
| )) | ||
| }, | ||
| ); | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| let view = deno_core::BufMutView::new(65536); | ||
| let (bytes, view) = match resource.read_byob(view).await { | ||
| Ok(bytes) => bytes, | ||
| Err(e) => { | ||
| state.borrow().borrow::<deno_core::V8TaskSpawner>().spawn( | ||
| move |scope| { | ||
| wasm_streaming.abort(Some( | ||
| v8::String::new( | ||
| scope, | ||
| &format!("Error reading wasm resource: {}", e), | ||
| ) | ||
| .unwrap() | ||
| .into(), | ||
| )) | ||
| }, | ||
| ); | ||
|
|
||
| return; | ||
| } | ||
| }; | ||
| /* EOF */ | ||
| if bytes == 0 { | ||
| break; | ||
| } | ||
|
|
||
| wasm_streaming.on_bytes_received(&view[..bytes]); | ||
| } | ||
|
|
||
| /* Spawn a task on JS loop to finish the streaming compilation */ | ||
| state | ||
| .borrow() | ||
| .borrow::<deno_core::V8TaskSpawner>() | ||
| .spawn(move |_| wasm_streaming.finish()); | ||
| }); | ||
| } | ||
|
|
||
| // Partially implements https://webassembly.github.io/spec/web-api/#compile-a-potential-webassembly-response | ||
| pub fn compile_response( | ||
| scope: &mut v8::HandleScope, | ||
| value: v8::Local<v8::Value>, | ||
| ) -> Result<Option<(String, ResourceId)>, AnyError> { | ||
| let object = value | ||
| .to_object(scope) | ||
| .ok_or_else(|| type_error("Response is not an object."))?; | ||
| let url = get_string(scope, object, "url")?; | ||
|
|
||
| // 2.3. | ||
| // The spec is ambiguous here, see | ||
| // https://github.com/WebAssembly/spec/issues/1138. The WPT tests expect | ||
| // the raw value of the Content-Type attribute lowercased. We ignore this | ||
| // for file:// because file fetches don't have a Content-Type. | ||
| if !url.starts_with("file://") { | ||
| let headers = get_value(scope, object, "headers")?; | ||
| let content_type = call_method(scope, headers, "get", "Content-Type")?; | ||
|
|
||
| if content_type.to_lowercase() != "application/wasm" { | ||
| return Err(type_error("Response is not a wasm file.")); | ||
| } | ||
| } | ||
|
|
||
| // 2.5 | ||
| let ok = get_value(scope, object, "ok")?; | ||
| if !ok.is_true() { | ||
| return Err(type_error("Response is not ok.")); | ||
| } | ||
|
|
||
| let body = get_value(scope, object, "body")?; | ||
|
|
||
| if body.is_null() { | ||
| return Ok(None); | ||
| } | ||
| let body = body | ||
| .to_object(scope) | ||
| .ok_or_else(|| type_error("Failed to get body object."))?; | ||
| let rid = get_value(scope, body, "rid")? | ||
| .to_uint32(scope) | ||
| .ok_or_else(|| type_error("Failed to get rid."))? | ||
| .value() as ResourceId; | ||
|
|
||
| Ok(Some((url, rid))) | ||
| } | ||
|
|
||
| fn get_value<'a, 'b>( | ||
| scope: &'b mut v8::HandleScope<'a>, | ||
| obj: v8::Local<'a, v8::Object>, | ||
| key: &'static str, | ||
| ) -> Result<v8::Local<'a, v8::Value>, AnyError> { | ||
| let key = v8::String::new(scope, key) | ||
| .ok_or_else(|| type_error("Failed to create key."))?; | ||
| Ok( | ||
| obj | ||
| .get(scope, key.into()) | ||
| .ok_or_else(|| type_error("Failed to get value."))?, | ||
| ) | ||
| } | ||
|
|
||
| fn get_string( | ||
| scope: &mut v8::HandleScope, | ||
| obj: v8::Local<v8::Object>, | ||
| key: &'static str, | ||
| ) -> Result<String, AnyError> { | ||
| let key = v8::String::new(scope, key) | ||
| .ok_or_else(|| type_error("Failed to create key."))?; | ||
| let value = obj | ||
| .get(scope, key.into()) | ||
| .ok_or_else(|| type_error("Failed to get value."))?; | ||
|
|
||
| Ok(value.to_rust_string_lossy(scope)) | ||
| } | ||
|
|
||
| fn call_method<'a>( | ||
| scope: &mut v8::HandleScope<'a>, | ||
| obj: v8::Local<'a, v8::Value>, | ||
| method: &'static str, | ||
| arg: &'static str, | ||
| ) -> Result<String, AnyError> { | ||
| let key = v8::String::new(scope, method) | ||
| .ok_or_else(|| type_error("Failed to create key."))?; | ||
| let function = obj | ||
| .to_object(scope) | ||
| .ok_or_else(|| type_error("Failed to create object."))?; | ||
| let function = function | ||
| .get(scope, key.into()) | ||
| .ok_or_else(|| type_error("Failed to get value."))?; | ||
| let function: v8::Local<v8::Function> = function.try_into()?; | ||
| let arg = v8::String::new(scope, arg) | ||
| .ok_or_else(|| type_error("Failed to create arg."))?; | ||
| Ok( | ||
| function | ||
| .call(scope, obj, &[arg.into()]) | ||
| .ok_or_else(|| type_error("Failed to call."))? | ||
| .to_rust_string_lossy(scope), | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should make a call into JS here to do a branding check and extract the stream RID, as this could in theory be created w/a Response that is not sourced from fetch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be mean having two wasm streaming callbacks. One in JS doing the brand check and one for streaming in Rust. I don't think the brand check is actually enforced by wpt.