提交 30d3f7a2 编写于 作者: A Alex Dima

Fixes #41268: Do not avoid sliced strings

上级 7c6ef650
......@@ -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;
......@@ -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 {
......@@ -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];
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册