Skip to content

Instantly share code, notes, and snippets.

@alexcrichton
Created September 20, 2019 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexcrichton/7eeb9385dff03147be4aed02f174dc66 to your computer and use it in GitHub Desktop.
Save alexcrichton/7eeb9385dff03147be4aed02f174dc66 to your computer and use it in GitHub Desktop.
diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs
index 6a7bba55c..d433f7cc3 100644
--- a/crates/cli-support/src/js/mod.rs
+++ b/crates/cli-support/src/js/mod.rs
@@ -900,30 +900,6 @@ impl<'a> Context<'a> {
self.expose_uint8_memory();
self.require_internal_export("__wbindgen_realloc")?;
- // A fast path that directly writes char codes into WASM memory as long
- // as it finds only ASCII characters.
- //
- // This is much faster for common ASCII strings because it can avoid
- // calling out into C++ TextEncoder code.
- //
- // This might be not very intuitive, but such calls are usually more
- // expensive in mainstream engines than staying in the JS, and
- // charCodeAt on ASCII strings is usually optimised to raw bytes.
- let encode_as_ascii = "\
- let len = arg.length;
- let ptr = wasm.__wbindgen_malloc(len);
-
- const mem = getUint8Memory();
-
- let offset = 0;
-
- for (; offset < len; offset++) {
- const code = arg.charCodeAt(offset);
- if (code > 0x7F) break;
- mem[ptr + offset] = code;
- }
- ";
-
// TODO:
// When converting a JS string to UTF-8, the maximum size is `arg.length * 3`,
// so we just allocate that. This wastes memory, so we should investigate
@@ -932,28 +908,25 @@ impl<'a> Context<'a> {
self.global(&format!(
"function passStringToWasm(arg) {{
{}
- {}
- if (offset !== len) {{
- if (offset !== 0) {{
- arg = arg.slice(offset);
- }}
- ptr = wasm.__wbindgen_realloc(ptr, len, len = offset + arg.length * 3);
+ let len = arg.length;
+ let ptr = wasm.__wbindgen_malloc(len);
+ let offset = 0;
+
+ while (true) {{
const view = getUint8Memory().subarray(ptr + offset, ptr + len);
- const ret = encodeString(arg, view);
- {}
- offset += ret.written;
+ const {{ read, written }} = encodeString(arg, view);
+ offset += written;
+ if (read == arg.length) {{
+ break;
+ }}
+ arg = arg.substring(read);
+ ptr = wasm.__wbindgen_realloc(ptr, len, len += arg.length * 3);
}}
WASM_VECTOR_LEN = offset;
return ptr;
}}",
debug,
- encode_as_ascii,
- if self.config.debug {
- "if (ret.read != arg.length) throw new Error('failed to pass whole string');"
- } else {
- ""
- },
));
Ok(())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment