Skip to content

Commit c2c8285

Browse files
committed
perf: merge two-pass utf8 registration into single pass in wasm-engine
Eliminates second scan of length-prefixed string data during WASM column registration. Now builds offsets/lengths and copies data simultaneously.
1 parent 69ccf46 commit c2c8285

1 file changed

Lines changed: 10 additions & 18 deletions

File tree

src/wasm-engine.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -451,35 +451,27 @@ export class WasmEngine {
451451
}
452452

453453
case "utf8": {
454-
// Scan length-prefixed strings to build offset/length arrays
454+
// Single-pass: build offsets/lengths and copy string data simultaneously
455455
const offsets = new Uint32Array(totalRows);
456456
const lengths = new Uint32Array(totalRows);
457-
let dataLen = 0;
458-
let pos = 0;
457+
const strData = new Uint8Array(flat.byteLength); // upper bound; trimmed at WASM copy
459458
const view = new DataView(flat.buffer, flat.byteOffset, flat.byteLength);
459+
let pos = 0;
460+
let dOff = 0;
460461
let ri = 0;
461462
while (pos + 4 <= flat.byteLength && ri < totalRows) {
462463
const strLen = view.getUint32(pos, true);
463464
pos += 4;
464-
offsets[ri] = dataLen;
465+
offsets[ri] = dOff;
465466
lengths[ri] = strLen;
466-
dataLen += strLen;
467-
pos += strLen;
468-
ri++;
469-
}
470-
471-
// Copy string data (without length prefixes) to WASM
472-
const strData = new Uint8Array(dataLen);
473-
pos = 0;
474-
let dOff = 0;
475-
const view2 = new DataView(flat.buffer, flat.byteOffset, flat.byteLength);
476-
for (let i = 0; i < ri; i++) {
477-
const strLen = view2.getUint32(pos, true);
478-
pos += 4;
479-
strData.set(flat.subarray(pos, pos + strLen), dOff);
467+
if (strLen > 0 && pos + strLen <= flat.byteLength) {
468+
strData.set(flat.subarray(pos, pos + strLen), dOff);
469+
}
480470
dOff += strLen;
481471
pos += strLen;
472+
ri++;
482473
}
474+
const dataLen = dOff;
483475

484476
const offsetsPtr = this.exports.alloc(offsets.byteLength);
485477
if (!offsetsPtr) return false;

0 commit comments

Comments
 (0)