Skip to content

Commit f850a85

Browse files
Just use a single 1024-byte buffer.
1 parent 1b4a431 commit f850a85

2 files changed

Lines changed: 13 additions & 21 deletions

File tree

src/compiler/emitter.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,14 +532,12 @@ 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);
536535

537536
let sourceMapGenerator: SourceMapGenerator | undefined;
538537
if (shouldEmitSourceMaps(mapOptions, sourceFileOrBundle)) {
539538
sourceMapGenerator = createSourceMapGenerator(
540539
host,
541540
getBaseFileName(normalizeSlashes(jsFilePath)),
542-
guessedLength,
543541
getSourceRoot(mapOptions),
544542
getSourceMapDirectory(mapOptions, jsFilePath, sourceFile),
545543
mapOptions);

src/compiler/sourcemap.ts

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

7-
let mappingsBuffer: Uint8Array;
8-
9-
export function createSourceMapGenerator(host: EmitHost, file: string, guessedInputLength: number, sourceRoot: string, sourcesDirectoryPath: string, generatorOptions: SourceMapGeneratorOptions): SourceMapGenerator {
7+
export function createSourceMapGenerator(host: EmitHost, file: string, sourceRoot: string, sourcesDirectoryPath: string, generatorOptions: SourceMapGeneratorOptions): SourceMapGenerator {
108
const { enter, exit } = generatorOptions.extendedDiagnostics
119
? performance.createTimer("Source Map", "beforeSourcemap", "afterSourcemap")
1210
: performance.nullTimer;
@@ -19,18 +17,15 @@ namespace ts {
1917

2018
const names: string[] = [];
2119
let nameToNameIndexMap: ESMap<string, number> | undefined;
22-
mappingsBuffer ||= new Uint8Array(guessedInputLength + 1 >> 1);
23-
let lastMappings: string | undefined;
24-
let mappingsPos = 0;
20+
let mappings = "";
21+
let mappingsBuffer = new Uint8Array(1024);
22+
let mappingsBufferPos = 0;
2523
function setMapping(charCode: number) {
26-
// resize to 1.5 * length
27-
if (mappingsPos >= mappingsBuffer.length) {
28-
const oldLength = mappingsBuffer.length + 1;
29-
const replacementBuffer = new Uint8Array(oldLength + ((oldLength + 1) >> 1));
30-
replacementBuffer.set(mappingsBuffer);
31-
mappingsBuffer = replacementBuffer;
24+
if (mappingsBufferPos >= mappingsBuffer.length) {
25+
mappings += String.fromCharCode.apply(undefined, mappingsBuffer);
26+
mappingsBufferPos = 0;
3227
}
33-
mappingsBuffer[mappingsPos++] = charCode;
28+
mappingsBuffer[mappingsBufferPos++] = charCode;
3429
}
3530

3631
function base64VLQFormatEncode(inValue: number) {
@@ -302,17 +297,16 @@ namespace ts {
302297
exit();
303298
}
304299

305-
function serializeMappings(len: number): string {
306-
let mappings = "";
307-
for (let i = 0; i < len; i += 1024) {
308-
mappings += String.fromCharCode.apply(undefined, mappingsBuffer.subarray(i, Math.min(len, i + 1024)));
300+
function flushMappings(): void {
301+
if (mappingsBufferPos > 0) {
302+
mappings += String.fromCharCode.apply(undefined, mappingsBuffer.subarray(0, mappingsBufferPos));
303+
mappingsBufferPos = 0;
309304
}
310-
return mappings;
311305
}
312306

313307
function toJSON(): RawSourceMap {
314308
commitPendingMapping();
315-
const mappings = (lastMappings ??= serializeMappings(mappingsPos));
309+
flushMappings();
316310
return {
317311
version: 3,
318312
file,

0 commit comments

Comments
 (0)