Skip to content

Commit

Permalink
Fixes #41268: Do not avoid sliced strings
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Jan 8, 2018
1 parent 7c6ef65 commit 30d3f7a
Showing 1 changed file with 1 addition and 42 deletions.
Expand Up @@ -25,48 +25,8 @@ export class TextBufferFactory implements ITextBufferFactory {
}
}

const AVOID_SLICED_STRINGS = true;
const PREALLOC_BUFFER_CHARS = 1000;

const emptyString = '';
const asciiStrings: string[] = [];
for (let i = 0; i < 128; i++) {
asciiStrings[i] = String.fromCharCode(i);
}

function optimizeStringMemory(buff: Buffer, s: string): string {
const len = s.length;

if (len === 0) {
return emptyString;
}

if (len === 1) {
const charCode = s.charCodeAt(0);
if (charCode < 128) {
return asciiStrings[charCode];
}
}

if (AVOID_SLICED_STRINGS) {
// See https://bugs.chromium.org/p/v8/issues/detail?id=2869
// See https://github.com/nodejs/help/issues/711

if (len < PREALLOC_BUFFER_CHARS) {
// Use the same buffer instance that we have allocated and that can fit `PREALLOC_BUFFER_CHARS` characters
const byteLen = buff.write(s, 0);
return buff.toString(undefined, 0, byteLen);
}

return Buffer.from(s).toString();
}

return s;
}

class ModelLineBasedBuilder {

private buff: Buffer;
private BOM: string;
private lines: string[];
private currLineIndex: number;
Expand All @@ -75,7 +35,6 @@ class ModelLineBasedBuilder {
this.BOM = '';
this.lines = [];
this.currLineIndex = 0;
this.buff = Buffer.alloc(3/*any UTF16 code unit could expand to up to 3 UTF8 code units*/ * PREALLOC_BUFFER_CHARS);
}

public acceptLines(lines: string[]): void {
Expand All @@ -88,7 +47,7 @@ class ModelLineBasedBuilder {
}

for (let i = 0, len = lines.length; i < len; i++) {
this.lines[this.currLineIndex++] = optimizeStringMemory(this.buff, lines[i]);
this.lines[this.currLineIndex++] = lines[i];
}
}

Expand Down

0 comments on commit 30d3f7a

Please sign in to comment.