Skip to content

Commit aa732cf

Browse files
Go back to TextDecoder, try ASCII.
1 parent f850a85 commit aa732cf

2 files changed

Lines changed: 18 additions & 16 deletions

File tree

src/compiler/emitter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,12 +532,14 @@ namespace ts {
532532
const bundle = sourceFileOrBundle.kind === SyntaxKind.Bundle ? sourceFileOrBundle : undefined;
533533
const sourceFile = sourceFileOrBundle.kind === SyntaxKind.SourceFile ? sourceFileOrBundle : undefined;
534534
const sourceFiles = bundle ? bundle.sourceFiles : [sourceFile!];
535+
const guessedLength = sourceFile?.text.length ?? reduceLeft(sourceFiles, (acc, file) => acc + file.text.length, 0);
535536

536537
let sourceMapGenerator: SourceMapGenerator | undefined;
537538
if (shouldEmitSourceMaps(mapOptions, sourceFileOrBundle)) {
538539
sourceMapGenerator = createSourceMapGenerator(
539540
host,
540541
getBaseFileName(normalizeSlashes(jsFilePath)),
542+
guessedLength,
541543
getSourceRoot(mapOptions),
542544
getSourceMapDirectory(mapOptions, jsFilePath, sourceFile),
543545
mapOptions);

src/compiler/sourcemap.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ namespace ts {
44
extendedDiagnostics?: boolean;
55
}
66

7-
export function createSourceMapGenerator(host: EmitHost, file: string, sourceRoot: string, sourcesDirectoryPath: string, generatorOptions: SourceMapGeneratorOptions): SourceMapGenerator {
7+
declare let TextDecoder: undefined | (new() => { decode(buffer: ArrayBuffer | ArrayBufferView): string });
8+
const decoder = new (typeof TextDecoder !== "undefined" ? TextDecoder : require("util").TextDecoder)("ascii");
9+
let mappingsBuffer: Uint8Array;
10+
11+
export function createSourceMapGenerator(host: EmitHost, file: string, guessedInputLength: number, sourceRoot: string, sourcesDirectoryPath: string, generatorOptions: SourceMapGeneratorOptions): SourceMapGenerator {
812
const { enter, exit } = generatorOptions.extendedDiagnostics
913
? performance.createTimer("Source Map", "beforeSourcemap", "afterSourcemap")
1014
: performance.nullTimer;
@@ -17,15 +21,18 @@ namespace ts {
1721

1822
const names: string[] = [];
1923
let nameToNameIndexMap: ESMap<string, number> | undefined;
20-
let mappings = "";
21-
let mappingsBuffer = new Uint8Array(1024);
22-
let mappingsBufferPos = 0;
24+
mappingsBuffer ||= new Uint8Array(guessedInputLength + 1 >> 1);
25+
let lastMappings: string | undefined;
26+
let mappingsPos = 0;
2327
function setMapping(charCode: number) {
24-
if (mappingsBufferPos >= mappingsBuffer.length) {
25-
mappings += String.fromCharCode.apply(undefined, mappingsBuffer);
26-
mappingsBufferPos = 0;
28+
// resize to 1.5 * length
29+
if (mappingsPos >= mappingsBuffer.length) {
30+
const oldLength = mappingsBuffer.length + 1;
31+
const replacementBuffer = new Uint8Array(oldLength + ((oldLength + 1) >> 1));
32+
replacementBuffer.set(mappingsBuffer);
33+
mappingsBuffer = replacementBuffer;
2734
}
28-
mappingsBuffer[mappingsBufferPos++] = charCode;
35+
mappingsBuffer[mappingsPos++] = charCode;
2936
}
3037

3138
function base64VLQFormatEncode(inValue: number) {
@@ -297,16 +304,9 @@ namespace ts {
297304
exit();
298305
}
299306

300-
function flushMappings(): void {
301-
if (mappingsBufferPos > 0) {
302-
mappings += String.fromCharCode.apply(undefined, mappingsBuffer.subarray(0, mappingsBufferPos));
303-
mappingsBufferPos = 0;
304-
}
305-
}
306-
307307
function toJSON(): RawSourceMap {
308308
commitPendingMapping();
309-
flushMappings();
309+
const mappings = (lastMappings ??= decoder.decode(mappingsBuffer.subarray(0, mappingsPos)));
310310
return {
311311
version: 3,
312312
file,

0 commit comments

Comments
 (0)