-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Speed up passing ASCII-only strings to WASM #1470
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1499,12 +1499,19 @@ impl<'a> Context<'a> { | |
arg = arg.slice(offset); | ||
ptr = wasm.__wbindgen_realloc(ptr, size, size = offset + arg.length * 3); | ||
const view = getUint8Memory().subarray(ptr + offset, ptr + size); | ||
const ret = cachedTextEncoder.encodeInto(arg, view); | ||
{} | ||
offset += cachedTextEncoder.encodeInto(arg, view).written; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could a debug assert of some form be included after this to ensure that it wrote everything? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't exactly use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah great. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I'm not sure what to check here. I mean, I'm passing all that's left of |
||
}} | ||
WASM_VECTOR_LEN = offset; | ||
return ptr; | ||
", | ||
start_encoding_as_ascii | ||
start_encoding_as_ascii, | ||
if self.config.debug { | ||
"if (ret.read != arg.length) throw new Error('failed to pass whole string');" | ||
} else { | ||
"" | ||
}, | ||
); | ||
|
||
// Looks like `encodeInto` doesn't currently work when the memory passed | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export function test_string_roundtrip(f) { | ||
const test = expected => { | ||
const actual = f(expected); | ||
if (actual === expected) | ||
return; | ||
throw new Error(`string roundtrip "${actual}" != "${expected}"`); | ||
}; | ||
|
||
test(''); | ||
test('a'); | ||
test('💖'); | ||
|
||
test('a longer string'); | ||
test('a longer 💖 string'); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use wasm_bindgen::prelude::*; | ||
use wasm_bindgen_test::*; | ||
|
||
#[wasm_bindgen(module = "/tests/headless/strings.js")] | ||
extern "C" { | ||
fn test_string_roundtrip(c: &Closure<Fn(String) -> String>); | ||
} | ||
|
||
#[wasm_bindgen_test] | ||
fn string_roundtrip() { | ||
test_string_roundtrip(&Closure::wrap(Box::new(|s| s))); | ||
} |
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.
@alexcrichton I see you already merged it, but this change... doesn't seem right. Why is it encoding same view twice now?