Skip to content

feat(loader): use TextDecoder for large strings #1471

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

Merged
merged 7 commits into from
Sep 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions lib/loader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,18 @@ const ARRAY_SIZE = 16;

const BIGINT = typeof BigUint64Array !== "undefined";
const THIS = Symbol();
const CHUNKSIZE = 1024;

const STRING_DECODE_THRESHOLD = 32;
const decoder = new TextDecoder("utf-16le");

/** Gets a string from an U32 and an U16 view on a memory. */
function getStringImpl(buffer, ptr) {
const U32 = new Uint32Array(buffer);
const U16 = new Uint16Array(buffer);
let length = U32[ptr + SIZE_OFFSET >>> 2] >>> 1;
let offset = ptr >>> 1;
if (length <= CHUNKSIZE) return String.fromCharCode.apply(String, U16.subarray(offset, offset + length));
let parts = '';
do {
const last = U16[offset + CHUNKSIZE - 1];
const size = last >= 0xD800 && last < 0xDC00 ? CHUNKSIZE - 1 : CHUNKSIZE;
parts += String.fromCharCode.apply(String, U16.subarray(offset, offset += size));
length -= size;
} while (length > CHUNKSIZE);
return parts + String.fromCharCode.apply(String, U16.subarray(offset, offset + length));
const len = new Uint32Array(buffer)[ptr + SIZE_OFFSET >>> 2] >>> 1;
const arr = new Uint16Array(buffer, ptr, len);
if (len <= STRING_DECODE_THRESHOLD) {
return String.fromCharCode.apply(String, arr);
}
return decoder.decode(arr);
}

/** Prepares the base module prior to instantiation. */
Expand Down
19 changes: 17 additions & 2 deletions lib/loader/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function test(file) {
// should be able to get an exported string
assert.strictEqual(exports.__getString(exports.COLOR), "red");

// should be able to allocate and work with a new string
// should be able to allocate and work with a new small string
{
let str = "Hello world!𤭢";
let ref = exports.__retain(exports.__allocString(str));
Expand All @@ -33,6 +33,21 @@ function test(file) {
exports.__release(ref);
}

// should be able to allocate and work with a new big string
{
let str = `
∀ ∁ ∂ ∃ ∄ ∅ ∆ ∇ ∈ ∉ ∊ ∋ ∌ ∍ ∎ ∏ ∐ ∑ − ∓ ∔ ∕ ∖ ∗ ∘ ∙ √ ∛
∜ ∝ ∞ ∟ ∠ ∡ ∢ ∣ ∤ ∥ ∦ ∧ ∨ ∩ ∪ ∫ ∬ ∭ ∮ ∯ ∰ ∱ ∲ ∳ ∴ ∵ ∶ ∷
∸ ∹ ∺ ∻ ∼ ∽ ∾ ∿ ≀ ≁ ≂ ≃ ≄ ≅ ≆ ≇ ≈ ≉ ≊ ≋ ≌ ≍ ≎ ≏ ≐ ≑ ≒ ≓
≔ ≕ ≖ ≗ ≘ ≙ ≚ ≛ ≜ ≝ ≞ ≟ ≠ ≡ ≢ ≣ ≤ ≥ ≦ ≧ ≨ ≩ ≪ ≫ ≬ ≭ ≮ ≯
≰ ≱ ≲ ≳ ≴ ≵ ≶ ≷ ≸ ≹ ≺ ≻ ≼ ≽ ≾ ≿
`;
let ref = exports.__retain(exports.__allocString(str));
assert.strictEqual(exports.__getString(ref), str);
assert.strictEqual(exports.strlen(ref), str.length);
exports.__release(ref);
}

// should be able to allocate a typed array
{
let arr = [1, 2, 3, 4, 5, 0x80000000 | 0];
Expand Down Expand Up @@ -290,4 +305,4 @@ function testInstantiate(file) {
assert(instance && instance instanceof WebAssembly.Instance);
assert(module && module instanceof WebAssembly.Module);
})();
}
}