Skip to content

Commit 6e023a2

Browse files
committed
improve parser memoization
1 parent f7bdf51 commit 6e023a2

File tree

10 files changed

+105
-171
lines changed

10 files changed

+105
-171
lines changed

dictionary/parser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,13 @@ const dictionaryParser = spaces
491491
),
492492
)
493493
)
494-
.parser();
494+
.generateParser();
495495

496496
const definitionExtractor = spaces
497497
.with(all(optionalAll(lex(head)).with(lex(match(/[^;]*;/, "definition")))))
498498
.skip(end)
499-
.parser();
500-
const definitionParser = spaces.with(definition).skip(end).parser();
499+
.generateParser();
500+
const definitionParser = spaces.with(definition).skip(end).generateParser();
501501

502502
export function parseDictionary(sourceText: string): Dictionary {
503503
const arrayResult = dictionaryParser(sourceText);

misc/misc.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,14 @@ export function compound(
4949
return `${initText} ${conjunction} ${last}`;
5050
}
5151
}
52+
export function lazy<T>(fn: () => T): () => T {
53+
let defined = false;
54+
let value: null | T;
55+
return () => {
56+
if (!defined) {
57+
defined = true;
58+
value = fn();
59+
}
60+
return value!;
61+
};
62+
}

src/cache.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/local_storage.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Lazy } from "./cache.ts";
1+
import { lazy } from "../misc/misc.ts";
22

33
export const checkLocalStorage = lazy(() => {
44
// https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API
@@ -28,7 +28,3 @@ export function setIgnoreError(key: string, value: string): void {
2828
}
2929
}
3030
}
31-
function lazy<T>(fn: () => T): () => T {
32-
const cache = new Lazy(fn);
33-
return () => cache.getValue();
34-
}

src/main.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { loadCustomDictionary } from "./dictionary.ts";
1616
import { checkLocalStorage, setIgnoreError } from "./local_storage.ts";
1717
import { flattenError } from "../misc/misc.ts";
1818
import { translate } from "./mod.ts";
19-
import { clearCache } from "./parser/cache.ts";
2019
import { settings } from "./settings.ts";
2120
import {
2221
loadFromElements,
@@ -219,7 +218,6 @@ function main(): void {
219218
confirmButton.addEventListener("click", () => {
220219
loadFromElements();
221220
updateLabel();
222-
clearCache();
223221
settingsDialogBox.close();
224222
});
225223
cancelButton.addEventListener("click", () => {
@@ -271,7 +269,6 @@ function main(): void {
271269
try {
272270
loadCustomDictionary(value);
273271
setIgnoreError(DICTIONARY_KEY, value);
274-
clearCache();
275272
customDictionaryDialogBox.close();
276273
} catch (error) {
277274
const errors = flattenError(error);

src/mod.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { translate as rawTranslate } from "./translator/translator.ts";
88

99
export { ArrayResultError, type ArrayResultOptions } from "./array_result.ts";
1010
export { loadCustomDictionary } from "./dictionary.ts";
11-
export { clearCache } from "./parser/cache.ts";
1211
export {
1312
defaultSettings,
1413
type RedundancySettings,

src/parser/cache.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/parser/lexer.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { sumOf } from "@std/collections/sum-of";
22
import { settings } from "../settings.ts";
3-
import { cache } from "./cache.ts";
43
import {
54
all,
65
allAtLeastOnce,
@@ -207,8 +206,6 @@ const combinedGlyphsToken = combinedGlyphs
207206
.map((words) => ({ type: "combined glyphs", words }) as const);
208207
const wordToken = word.map((word) => ({ type: "word", word }) as const);
209208

210-
Parser.startCache(cache);
211-
212209
export const token = choiceOnlyOne<Token>(
213210
xAlaX,
214211
multipleA,
@@ -226,5 +223,3 @@ export const token = choiceOnlyOne<Token>(
226223
headlessLongGlyphStart,
227224
insideLongGlyph,
228225
);
229-
230-
Parser.endCache();

src/parser/parser.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
SimpleHeadedWordUnit,
2525
SimpleWordUnit,
2626
} from "./ast.ts";
27-
import { cache } from "./cache.ts";
2827
import { everyWordUnitInSentence } from "./extract.ts";
2928
import {
3029
CLAUSE_RULE,
@@ -59,11 +58,10 @@ import {
5958
UnrecognizedError,
6059
} from "./parser_lib.ts";
6160
import { describe, Token } from "./token.ts";
61+
import { lazy as lazyEval } from "../../misc/misc.ts";
6262

6363
const spaces = match(/\s*/, "spaces");
6464

65-
Parser.startCache(cache);
66-
6765
const specificToken = memoize(
6866
<T extends Token["type"]>(type: T): Parser<Token & { type: T }> =>
6967
token.map((token) =>
@@ -219,7 +217,7 @@ function optionalCombined(
219217
);
220218
}
221219
const number = manyAtLeastOnce(wordFrom(numeralSet, "numeral"));
222-
const phrase: Parser<Phrase> = lazy(() =>
220+
const phrase: Parser<Phrase> = lazy(lazyEval(() =>
223221
choice<Phrase>(
224222
sequence(
225223
number,
@@ -273,7 +271,7 @@ const phrase: Parser<Phrase> = lazy(() =>
273271
})),
274272
)
275273
.filter(filter(PHRASE_RULE))
276-
);
274+
));
277275
const nanpa = sequence(wordUnit(new Set(["nanpa"]), '"nanpa"'), phrase)
278276
.map(([nanpa, phrase]) => ({ nanpa, phrase }))
279277
.filter(filter(NANPA_RULES));
@@ -723,6 +721,4 @@ export const parse = spaces
723721
.filter(filter(MULTIPLE_SENTENCES_RULE))
724722
.map((sentences) => ({ type: "sentences", sentences })),
725723
))
726-
.parser();
727-
728-
Parser.endCache();
724+
.generateParser();

0 commit comments

Comments
 (0)