From 6e76173637645498e10842744975b5b43b988caa Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 30 Jul 2022 19:43:51 +0300 Subject: [PATCH 1/7] simplify --- src/util/text.ts | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/src/util/text.ts b/src/util/text.ts index d76d9a2408..cf2fafbbb6 100644 --- a/src/util/text.ts +++ b/src/util/text.ts @@ -433,23 +433,9 @@ function lookupInUnicodeMap(code: u16, map: u16[]): bool { return false; } -const indentX1 = " "; -const indentX2 = " "; -const indentX4 = " "; - /** Creates an indentation matching the number of specified levels. */ export function indent(sb: string[], level: i32): void { - while (level >= 4) { - sb.push(indentX4); - level -= 4; - } - if (level >= 2) { - sb.push(indentX2); - level -= 2; - } - if (level) { - sb.push(indentX1); - } + sb.push(" ".repeat(level)); } /** Escapes a string using the specified kind of quote. */ From ad68eacd5973ac933a552fa4c4fa4aa476a2b428 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 30 Jul 2022 19:48:04 +0300 Subject: [PATCH 2/7] avoid push empty strings --- src/util/text.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/text.ts b/src/util/text.ts index cf2fafbbb6..3b5717a489 100644 --- a/src/util/text.ts +++ b/src/util/text.ts @@ -435,7 +435,7 @@ function lookupInUnicodeMap(code: u16, map: u16[]): bool { /** Creates an indentation matching the number of specified levels. */ export function indent(sb: string[], level: i32): void { - sb.push(" ".repeat(level)); + if (level) sb.push(" ".repeat(level)); } /** Escapes a string using the specified kind of quote. */ From 17a3c6febe20f37fd77ddd024a448e8f3a4d4ddf Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 30 Jul 2022 20:26:07 +0300 Subject: [PATCH 3/7] use more clever approach --- src/util/text.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/util/text.ts b/src/util/text.ts index 3b5717a489..8b1262ca56 100644 --- a/src/util/text.ts +++ b/src/util/text.ts @@ -434,8 +434,29 @@ function lookupInUnicodeMap(code: u16, map: u16[]): bool { } /** Creates an indentation matching the number of specified levels. */ +const indentX1 = " "; +const indentX2 = " "; +const indentX3 = " "; +const indentX4 = " "; +const indentCache = new Map(); + export function indent(sb: string[], level: i32): void { - if (level) sb.push(" ".repeat(level)); + if (level <= 4) { + switch (level) { + case 1: sb.push(indentX1); break; + case 2: sb.push(indentX2); break; + case 3: sb.push(indentX3); break; + case 4: sb.push(indentX4); break; + } + } else { + if (!indentCache.has(level)) { + let spaces = " ".repeat(level); + sb.push(spaces); + indentCache.set(level, spaces); + } else { + sb.push(indentCache.get(level)!); + } + } } /** Escapes a string using the specified kind of quote. */ From f92dd9a1646555b99c172463b89dd97c6752829c Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 30 Jul 2022 20:27:59 +0300 Subject: [PATCH 4/7] fix --- src/util/text.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/text.ts b/src/util/text.ts index 8b1262ca56..740d58beac 100644 --- a/src/util/text.ts +++ b/src/util/text.ts @@ -454,7 +454,7 @@ export function indent(sb: string[], level: i32): void { sb.push(spaces); indentCache.set(level, spaces); } else { - sb.push(indentCache.get(level)!); + sb.push(assert(indentCache.get(level))); } } } From 744cbb8e650abd802df9b7d748ef4b1227f0b64a Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 1 Aug 2022 06:53:14 +0300 Subject: [PATCH 5/7] use indentX1 instead inline literal --- src/util/text.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/text.ts b/src/util/text.ts index 740d58beac..da14da1edb 100644 --- a/src/util/text.ts +++ b/src/util/text.ts @@ -450,7 +450,7 @@ export function indent(sb: string[], level: i32): void { } } else { if (!indentCache.has(level)) { - let spaces = " ".repeat(level); + let spaces = indentX1.repeat(level); sb.push(spaces); indentCache.set(level, spaces); } else { From 6e515f29ef7974c192a3205083bbd2fc2170dbea Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Mon, 1 Aug 2022 06:56:42 +0300 Subject: [PATCH 6/7] refactor --- src/util/text.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/util/text.ts b/src/util/text.ts index da14da1edb..330c18f6d7 100644 --- a/src/util/text.ts +++ b/src/util/text.ts @@ -449,13 +449,13 @@ export function indent(sb: string[], level: i32): void { case 4: sb.push(indentX4); break; } } else { - if (!indentCache.has(level)) { - let spaces = indentX1.repeat(level); - sb.push(spaces); - indentCache.set(level, spaces); + let indents: string; + if (indentCache.has(level)) { + indents = assert(indentCache.get(level)); } else { - sb.push(assert(indentCache.get(level))); + indentCache.set(level, (indents = indentX1.repeat(level))); } + sb.push(indents); } } From 0c1332b25b8adf9845425242d6e0eb722b2f29e0 Mon Sep 17 00:00:00 2001 From: MaxGraey Date: Sat, 6 Aug 2022 13:45:32 +0300 Subject: [PATCH 7/7] limit cache usage --- src/util/text.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/util/text.ts b/src/util/text.ts index 330c18f6d7..2c25418f03 100644 --- a/src/util/text.ts +++ b/src/util/text.ts @@ -450,10 +450,16 @@ export function indent(sb: string[], level: i32): void { } } else { let indents: string; - if (indentCache.has(level)) { - indents = assert(indentCache.get(level)); + // Limit number of indent entries to 1024 for avoiding unnecessary + // memory consumetion + if (indentCache.size <= 1024) { + if (indentCache.has(level)) { + indents = assert(indentCache.get(level)); + } else { + indentCache.set(level, (indents = indentX1.repeat(level))); + } } else { - indentCache.set(level, (indents = indentX1.repeat(level))); + indents = indentX1.repeat(level); } sb.push(indents); }