Skip to content

Commit fc35f92

Browse files
committed
Limit buffer size, minor performance tweaks
1 parent 21e8ac0 commit fc35f92

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/compiler/sourcemap.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace ts {
1818
const names: string[] = [];
1919
let nameToNameIndexMap: ESMap<string, number> | undefined;
2020
const mappingCharCodes: number[] = [];
21+
// We will create a string from the char code buffer whenever it exceeds this length
22+
const mappingCommitThreshold = 1000;
2123
let mappings = "";
2224

2325
// Last recorded and encoded mappings
@@ -221,12 +223,14 @@ namespace ts {
221223
// Line/Comma delimiters
222224
if (lastGeneratedLine < pendingGeneratedLine) {
223225
// Emit line delimiters
226+
// This loop can potentially overflow the stack on the char code conversion if it were a single operation
224227
do {
225228
mappingCharCodes.push(CharacterCodes.semicolon); // ';'
226229
lastGeneratedLine++;
227-
lastGeneratedCharacter = 0;
228230
}
229231
while (lastGeneratedLine < pendingGeneratedLine);
232+
// Only need to set this once
233+
lastGeneratedCharacter = 0;
230234
}
231235
else {
232236
Debug.assertEqual(lastGeneratedLine, pendingGeneratedLine, "generatedLine cannot backtrack");
@@ -260,20 +264,26 @@ namespace ts {
260264
}
261265
}
262266

267+
if (mappings.length > mappingCommitThreshold) {
268+
flushMappingBuffer();
269+
}
270+
263271
hasLast = true;
264272
exit();
265273
}
266274

267-
function serializeMappings(): void {
268-
for (let i = 0, len = mappingCharCodes.length; i < len; i += 1024) {
275+
function flushMappingBuffer(): void {
276+
// If there are a very large number of skipped lines in the source mapping, this loop can iterate multiple times
277+
// Otherwise it should always have 1 iteration
278+
for (let i = 0, len = mappingCharCodes.length; i < len; i + 1024) {
269279
mappings += String.fromCharCode.apply(undefined, mappingCharCodes.slice(i, i + 1024));
270280
}
271281
mappingCharCodes.length = 0;
272282
}
273283

274284
function toJSON(): RawSourceMap {
275285
commitPendingMapping();
276-
serializeMappings();
286+
flushMappingBuffer();
277287
return {
278288
version: 3,
279289
file,

0 commit comments

Comments
 (0)