Skip to content

Commit

Permalink
Avoid deopt from differing map types
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Jun 28, 2024
1 parent 9d343e4 commit 154656b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
41 changes: 17 additions & 24 deletions src/scopes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,14 @@ function _encodeOriginalScopes(
encodeInteger(writer, v, 0);
}

index = encodeTree(scopes, index, writer, state, endLine, endColumn, _encodeOriginalScopes);
for (index++; index < scopes.length; ) {
const next = scopes[index];
const { 0: l, 1: c } = next;
if (l > endLine || (l === endLine && c >= endColumn)) {
break;
}
index = _encodeOriginalScopes(scopes, index, writer, state);
}

writer.write(comma);
state[0] = encodeInteger(writer, endLine, state[0]);
Expand All @@ -124,7 +131,6 @@ export function decodeGeneratedRanges(input: string): GeneratedRange[] {
const stack: GeneratedRange[] = [];

let genLine = 0;
let genColumn = 0;
let definitionSourcesIndex = 0;
let definitionScopeIndex = 0;
let callsiteSourcesIndex = 0;
Expand All @@ -135,7 +141,7 @@ export function decodeGeneratedRanges(input: string): GeneratedRange[] {

do {
const semi = reader.indexOf(';');
genColumn = 0;
let genColumn = 0;

for (; reader.pos < semi; reader.pos++) {
genColumn = decodeInteger(reader, genColumn);
Expand Down Expand Up @@ -309,7 +315,14 @@ function _encodeGeneratedRanges(
}
}

index = encodeTree(ranges, index, writer, state, endLine, endColumn, _encodeGeneratedRanges);
for (index++; index < ranges.length; ) {
const next = ranges[index];
const { 0: l, 1: c } = next;
if (l > endLine || (l === endLine && c >= endColumn)) {
break;
}
index = _encodeGeneratedRanges(ranges, index, writer, state);
}

if (state[0] < endLine) {
catchupLine(writer, state[0], endLine);
Expand All @@ -323,26 +336,6 @@ function _encodeGeneratedRanges(
return index;
}

function encodeTree<T extends [number, number, ...number[]], S>(
arr: T[],
index: number,
writer: StringWriter,
state: S,
endLine: number,
endColumn: number,
fn: (arr: T[], index: number, writer: StringWriter, state: S) => number,
): number {
for (index++; index < arr.length; ) {
const next = arr[index];
const { 0: l, 1: c } = next;
if (l > endLine || (l === endLine && c >= endColumn)) {
break;
}
index = fn(arr, index, writer, state);
}
return index;
}

function catchupLine(writer: StringWriter, lastLine: number, line: number) {
do {
writer.write(semicolon);
Expand Down
6 changes: 1 addition & 5 deletions src/sourcemap-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ function sortComparator(a: SourceMapSegment, b: SourceMapSegment): number {
export function encode(decoded: SourceMapMappings): string;
export function encode(decoded: Readonly<SourceMapMappings>): string;
export function encode(decoded: Readonly<SourceMapMappings>): string {
// We can push up to 5 ints, each int can take at most 7 chars, and we
// may push a comma.
const writer = new StringWriter();
let genColumn = 0;
let sourcesIndex = 0;
let sourceLine = 0;
let sourceColumn = 0;
Expand All @@ -90,10 +87,9 @@ export function encode(decoded: Readonly<SourceMapMappings>): string {
for (let i = 0; i < decoded.length; i++) {
const line = decoded[i];
if (i > 0) writer.write(semicolon);

if (line.length === 0) continue;

genColumn = 0;
let genColumn = 0;

for (let j = 0; j < line.length; j++) {
const segment = line[j];
Expand Down

0 comments on commit 154656b

Please sign in to comment.