Skip to content

Commit 38fccbe

Browse files
committed
perf: cache SetOperator sorted keys, pre-allocate null interleave arrays
- SetOperator.rowKey() called Object.keys().sort() per row — now cached on first call - decodePage null interleave arrays use pre-allocated new Array(n) instead of push()
1 parent 2951019 commit 38fccbe

2 files changed

Lines changed: 8 additions & 5 deletions

File tree

src/decode.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ export function decodePage(
241241
return v2Strings as (string | null)[];
242242
}
243243
// Parquet-style: packed non-null values, interleave with nulls
244-
const withNulls: (string | null)[] = [];
244+
const withNulls = new Array<string | null>(rowCount);
245245
let vi = 0;
246246
for (let i = 0; i < rowCount; i++) {
247-
withNulls.push(nulls.has(i) ? null : (vi < v2Strings.length ? v2Strings[vi++] : null));
247+
withNulls[i] = nulls.has(i) ? null : (vi < v2Strings.length ? v2Strings[vi++] : null);
248248
}
249249
return withNulls;
250250
}
@@ -261,10 +261,10 @@ export function decodePage(
261261
return values;
262262
}
263263
// Parquet-style: packed non-null values, interleave with nulls
264-
const withNulls: (number | bigint | string | null)[] = [];
264+
const withNulls = new Array<number | bigint | string | null>(rowCount);
265265
let vi = 0;
266266
for (let i = 0; i < rowCount; i++) {
267-
withNulls.push(nulls.has(i) ? null : (vi < values.length ? values[vi++] : null));
267+
withNulls[i] = nulls.has(i) ? null : (vi < values.length ? values[vi++] : null);
268268
}
269269
return withNulls;
270270
}

src/operators.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,8 +1142,11 @@ export class SetOperator implements Operator {
11421142
if (mode !== "union_all") this.seen = new Set();
11431143
}
11441144

1145+
private cachedKeys: string[] | null = null;
1146+
11451147
private rowKey(row: Row): string {
1146-
const keys = Object.keys(row).sort();
1148+
if (!this.cachedKeys) this.cachedKeys = Object.keys(row).sort();
1149+
const keys = this.cachedKeys;
11471150
let result = "";
11481151
for (let i = 0; i < keys.length; i++) {
11491152
if (i > 0) result += "\x00";

0 commit comments

Comments
 (0)