Skip to content

Commit 5581cdf

Browse files
committed
Improving the passStringToWasm function
1 parent e39e850 commit 5581cdf

File tree

1 file changed

+46
-26
lines changed
  • crates/cli-support/src/js

1 file changed

+46
-26
lines changed

crates/cli-support/src/js/mod.rs

+46-26
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,30 @@ impl<'a> Context<'a> {
805805
self.global("let WASM_VECTOR_LEN = 0;");
806806
}
807807

808+
fn expose_encode_as_ascii(&mut self) {
809+
if !self.should_write_global("encode_as_ascii") {
810+
return;
811+
}
812+
813+
self.expose_uint8_memory();
814+
815+
self.global("
816+
function encodeAsAscii(arg, ptr, len) {
817+
let offset = 0;
818+
819+
const mem = getUint8Memory();
820+
821+
for (; offset < len; offset++) {
822+
const code = arg.charCodeAt(offset);
823+
if (code > 0x7F) break;
824+
mem[ptr + offset] = code;
825+
}
826+
827+
return offset;
828+
}
829+
");
830+
}
831+
808832
fn expose_pass_string_to_wasm(&mut self) -> Result<(), Error> {
809833
if !self.should_write_global("pass_string_to_wasm") {
810834
return Ok(());
@@ -846,6 +870,8 @@ impl<'a> Context<'a> {
846870
self.expose_text_encoder()?;
847871
self.expose_uint8_memory();
848872

873+
self.expose_encode_as_ascii();
874+
849875
// A fast path that directly writes char codes into WASM memory as long
850876
// as it finds only ASCII characters.
851877
//
@@ -856,30 +882,25 @@ impl<'a> Context<'a> {
856882
// expensive in mainstream engines than staying in the JS, and
857883
// charCodeAt on ASCII strings is usually optimised to raw bytes.
858884
let start_encoding_as_ascii = format!(
859-
"
885+
"\
860886
{}
861-
let size = arg.length;
862-
let ptr = wasm.__wbindgen_malloc(size);
863-
let offset = 0;
864-
{{
865-
const mem = getUint8Memory();
866-
for (; offset < arg.length; offset++) {{
867-
const code = arg.charCodeAt(offset);
868-
if (code > 0x7F) break;
869-
mem[ptr + offset] = code;
870-
}}
871-
}}
887+
const len = arg.length;
888+
let ptr = wasm.__wbindgen_malloc(len);
889+
const offset = encodeAsAscii(arg, ptr, len);
872890
",
873891
debug
874892
);
875893

876894
// The first implementation we have for this is to use
877895
// `TextEncoder#encode` which has been around for quite some time.
878896
let use_encode = format!(
879-
"
897+
"\
880898
{}
881-
if (offset !== arg.length) {{
882-
const buf = cachedTextEncoder.encode(arg.slice(offset));
899+
if (offset !== len) {{
900+
if (offset !== 0) {{
901+
arg = arg.slice(offset);
902+
}}
903+
const buf = cachedTextEncoder.encode(arg);
883904
ptr = wasm.__wbindgen_realloc(ptr, size, size = offset + buf.length);
884905
getUint8Memory().set(buf, ptr + offset);
885906
offset += buf.length;
@@ -894,11 +915,13 @@ impl<'a> Context<'a> {
894915
// newer and isn't implemented everywhere yet. It's more efficient,
895916
// however, becaues it allows us to elide an intermediate allocation.
896917
let use_encode_into = format!(
897-
"
918+
"\
898919
{}
899-
if (offset !== arg.length) {{
900-
arg = arg.slice(offset);
901-
ptr = wasm.__wbindgen_realloc(ptr, size, size = offset + arg.length * 3);
920+
if (offset !== len) {{
921+
if (offset !== 0) {{
922+
arg = arg.slice(offset);
923+
}}
924+
ptr = wasm.__wbindgen_realloc(ptr, size, size = offset + len * 3);
902925
const view = getUint8Memory().subarray(ptr + offset, ptr + size);
903926
const ret = cachedTextEncoder.encodeInto(arg, view);
904927
{}
@@ -909,7 +932,7 @@ impl<'a> Context<'a> {
909932
",
910933
start_encoding_as_ascii,
911934
if self.config.debug {
912-
"if (ret.read != arg.length) throw new Error('failed to pass whole string');"
935+
"if (ret.read != len) throw new Error('failed to pass whole string');"
913936
} else {
914937
""
915938
},
@@ -932,12 +955,9 @@ impl<'a> Context<'a> {
932955
self.require_internal_export("__wbindgen_realloc")?;
933956
self.global(&format!(
934957
"
935-
let passStringToWasm;
936-
if (typeof cachedTextEncoder.encodeInto === 'function') {{
937-
passStringToWasm = function(arg) {{ {} }};
938-
}} else {{
939-
passStringToWasm = function(arg) {{ {} }};
940-
}}
958+
const passStringToWasm = (typeof cachedTextEncoder.encodeInto === 'function'
959+
? function (arg) {{ {} }}
960+
: function (arg) {{ {} }});
941961
",
942962
use_encode_into, use_encode,
943963
));

0 commit comments

Comments
 (0)