perf(javascript): index feature, semantic, IPC, and source map lookups (#13319)

perf(javascript): eliminate double preload AST pass and index feature/semantic/IPC/sourcemap lookups
This commit is contained in:
morluto authored and GitHub committed 2026-11-22 03:43:13 -05:00
1 parent 79887f7a3b
commit fde2c87b86
17 files changed
+1860 -535

No files matched your search

@@ -364,3 +364,46 @@ it.each(["ggggggEAAA", "gggggggBAAA"])(
).toThrow(/32-bit/u);
},
);
describe("binary search on dense single-line source maps", () => {
it("resolves exact, gap, tail, duplicate, and prior-line fallback positions", () => {
const denseMappings = `AAAA,${Array.from({ length: 1999 }, () => "EAAE").join(",")};UAAA`;
const denseMap = {
version: 3,
sources: ["dense.ts"],
names: [],
mappings: denseMappings,
};
expect(trace(denseMap, { line: 1, column: 2469 }).matches[0]).toMatchObject(
{
reported_source: "dense.ts",
original_position: { line: 1, column: 2468 },
},
);
expect(trace(denseMap, { line: 1, column: 5000 }).matches[0]).toMatchObject(
{
original_position: { line: 1, column: 3998 },
},
);
expect(trace(denseMap, { line: 2, column: 1 }).matches[0]).toMatchObject({
original_position: { line: 1, column: 3998 },
});
const duplicateColumnMap = {
version: 3,
sources: ["dup.ts"],
names: [],
mappings: "AAAA,AAEC,AAAC",
};
expect(
trace(duplicateColumnMap, { line: 1, column: 0 }).matches.map((m) =>
m.state === "mapped"
? [m.original_position.line, m.original_position.column]
: null,
),
).toEqual([
[1, 0],
[3, 1],
[3, 2],
]);
});
});
+86 -29
View File
@@ -25,6 +25,12 @@ import {
import { decodeValidatedSourceMapLeaves } from "../../domain/decodedSourceMap.js";
import { sha256Hex } from "../../kernel/sha256Hex.js";
const traceMappingVersion = z
.object({ version: z.string().min(1) })
.parse(
createRequire(import.meta.url)("@jridgewell/trace-mapping/package.json"),
).version;
/** Decode with the pinned upstream codec; preserve every equal-position candidate. */
export const traceSourceMap = (
text: string,
@@ -42,38 +48,39 @@ export const traceSourceMap = (
if (matched !== null) {
const line = matched.line - leaf.offset.line - 1;
const column = matched.column - (line === 0 ? leaf.offset.column : 0);
for (const segment of rows[line] ?? []) {
if (segment[0] !== column) continue;
const match = originalMatch(
segment,
map,
url,
inspected.declarations,
sourceBase,
);
bytes += Buffer.byteLength(JSON.stringify(match)) + 1;
if (bytes > limits.outputBytes)
throw sourceMapLimitFailure(
`Complete source-map point evidence exceeds output admission; no partial evidence was returned.`,
"output_bytes",
limits.outputBytes,
);
matches.push(match);
const row = line >= 0 ? rows[line] : undefined;
if (row !== undefined && row.length > 0) {
const start = firstSegmentIndexAt(row, column);
if (start !== -1) {
for (let index = start; index < row.length; index += 1) {
const segment = row[index]!;
if (segment[0] !== column) break;
const match = originalMatch(
segment,
map,
url,
inspected.declarations,
sourceBase,
);
bytes += Buffer.byteLength(JSON.stringify(match)) + 1;
if (bytes > limits.outputBytes)
throw sourceMapLimitFailure(
`Complete source-map point evidence exceeds output admission; no partial evidence was returned.`,
"output_bytes",
limits.outputBytes,
);
matches.push(match);
}
}
}
}
sourceBase += leaf.map.sources.length;
}
const metadata: unknown = createRequire(import.meta.url)(
"@jridgewell/trace-mapping/package.json",
);
const version = z
.object({ version: z.string().min(1) })
.parse(metadata).version;
const runtime = javaScriptRuntime(process.versions);
return webSourceMapReportSchema.parse({
source_map_sha256: sha256Hex(text),
url_context: url,
engine: { id: "jridgewell-trace-mapping", version },
engine: { id: "jridgewell-trace-mapping", version: traceMappingVersion },
// Bun reports V8-shaped heap statistics but ignores the codec's heap flags.
// Its launcher applies the same runtime policy, and the parent verifies it.
runtime:
@@ -115,12 +122,19 @@ const greatestPosition = (
for (const { leaf, rows } of leaves) {
const localLine = position.line - leaf.offset.line - 1;
for (let line = Math.min(localLine, rows.length - 1); line >= 0; line--) {
const row = rows[line];
if (row === undefined || row.length === 0) continue;
const lineOffset = line === 0 ? leaf.offset.column : 0;
let column: number | undefined;
for (const segment of rows[line] ?? []) {
const absoluteColumn =
segment[0] + (line === 0 ? leaf.offset.column : 0);
if (line < localLine || absoluteColumn <= position.column)
column = absoluteColumn;
if (line < localLine) {
// Rows from @jridgewell/trace-mapping are sorted by column; the last entry is greatest.
column = row[row.length - 1]![0] + lineOffset;
} else {
const index = greatestSegmentIndexAtOrBefore(
row,
position.column - lineOffset,
);
if (index >= 0) column = row[index]![0] + lineOffset;
}
if (column === undefined) continue;
const absoluteLine = leaf.offset.line + line + 1;
@@ -135,6 +149,49 @@ const greatestPosition = (
}
return matched;
};
/** Largest index in column-sorted `row` with `segment[0] <= targetColumn`, or `-1`. */
const greatestSegmentIndexAtOrBefore = (
row: readonly SourceMapSegment[],
targetColumn: number,
): number => {
let low = 0;
let high = row.length - 1;
let best = -1;
while (low <= high) {
const mid = low + ((high - low) >> 1);
if (row[mid]![0] <= targetColumn) {
best = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return best;
};
/** First index in column-sorted `row` with `segment[0] === column`, or `-1`. */
const firstSegmentIndexAt = (
row: readonly SourceMapSegment[],
column: number,
): number => {
let low = 0;
let high = row.length - 1;
let first = -1;
while (low <= high) {
const mid = low + ((high - low) >> 1);
const value = row[mid]![0];
if (value === column) {
first = mid;
high = mid - 1;
} else if (value < column) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return first;
};
const originalMatch = (
segment: Readonly<SourceMapSegment>,
map: TraceMap,