Skip to content

Commit bffe009

Browse files
Skip re-creating DataView in decodeArray
1 parent 78b442d commit bffe009

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Runtime/src/js-value.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,18 @@ export const decode = (
5151
// Note:
5252
// `decodeValues` assumes that the size of RawJSValue is 16.
5353
export const decodeArray = (ptr: pointer, length: number, memory: Memory) => {
54+
// fast path for empty array
55+
if (length === 0) { return []; }
56+
5457
let result = [];
58+
// It's safe to hold DataView here because WebAssembly.Memory.buffer won't
59+
// change within this function.
60+
const view = memory.dataView();
5561
for (let index = 0; index < length; index++) {
5662
const base = ptr + 16 * index;
57-
const kind = memory.readUint32(base);
58-
const payload1 = memory.readUint32(base + 4);
59-
const payload2 = memory.readFloat64(base + 8);
63+
const kind = view.getUint32(base, true);
64+
const payload1 = view.getUint32(base + 4, true);
65+
const payload2 = view.getFloat64(base + 8, true);
6066
result.push(decode(kind, payload1, payload2, memory));
6167
}
6268
return result;

0 commit comments

Comments
 (0)