Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
perf(search): stop re-normalizing a comparator's value once per note
Elian Doran4350d7477903
diff --git a/packages/trilium-core/src/services/search/services/build_comparator.spec.ts b/packages/trilium-core/src/services/search/services/build_comparator.spec.tsindex 896a46e7d9..7d62ea94ec 100644--- a/packages/trilium-core/src/services/search/services/build_comparator.spec.ts+++ b/packages/trilium-core/src/services/search/services/build_comparator.spec.ts@@ -71,6 +71,26 @@ describe("buildComparator", () => {expect(eq?.("the cafe is open")).toBe(false);});+ it("keeps each value's own verdict when values repeat and alternate", () => {+ // A comparator is reused across every note in the database and remembers the last+ // value it normalized, so a run of repeats must not carry a verdict into the next+ // distinct value — nor an unnormalized value into a repeat of a normalized one.+ const eq = buildComparator("=", "false");+ for (const [value, expected] of [+ ["false", true], ["false", true], ["true", false], ["false", true],+ ["", false], ["false", true], ["FALSE", true], ["falsey", false], ["false", true]+ ] as const) {+ expect([ value, eq?.(value) ]).toEqual([ value, expected ]);+ }++ const diacritics = buildComparator("=", "café");+ expect(diacritics?.("café")).toBe(true);+ expect(diacritics?.("cafe")).toBe(true);+ expect(diacritics?.("cafè")).toBe(true);+ expect(diacritics?.("cafes")).toBe(false);+ expect(diacritics?.("café")).toBe(true);+ });+it("negates the strict equality match and treats empty value as a match", () => {const neq = buildComparator("!=", "vienna");expect(neq?.("vienna")).toBe(false);diff --git a/packages/trilium-core/src/services/search/services/build_comparator.ts b/packages/trilium-core/src/services/search/services/build_comparator.tsindex 5603a02c2c..213d02fe56 100644--- a/packages/trilium-core/src/services/search/services/build_comparator.ts+++ b/packages/trilium-core/src/services/search/services/build_comparator.ts@@ -19,11 +19,13 @@ const stringComparators: Record<string, Comparator<string>> = {// in the internal "word=" operator."=": (comparedValue) => {const normalizedCompared = normalizeSearchText(comparedValue);- return (val) => normalizeSearchText(val) === normalizedCompared;+ const normalize = repeatAwareNormalizer();+ return (val) => normalize(val) === normalizedCompared;},"!=": (comparedValue) => {const normalizedCompared = normalizeSearchText(comparedValue);- return (val) => normalizeSearchText(val) !== normalizedCompared;+ const normalize = repeatAwareNormalizer();+ return (val) => normalize(val) !== normalizedCompared;},// Internal operator (not user-typable): word/phrase match used by the leading-"="// fulltext title comparison. Punctuation-aware via tokenizeIntoWords/stripWordPunctuation.@@ -113,6 +115,28 @@ const numericComparators: Record<string, Comparator<number>> = {"<=": (comparedValue) => (val) => parseFloat(val) <= comparedValue};+/**+ * {@link normalizeSearchText} bound to one comparator, holding on to its last input and result.+ *+ * `PropertyComparisonExp` and `LabelComparisonExp` apply a comparator value by value across the+ * database, and a property such as `isArchived`, `type` or `isProtected` yields the same handful+ * of strings throughout, so the repeated calls collapse into a string comparison. Values that are+ * distinct per note, such as titles, miss and normalize as before.+ */+function repeatAwareNormalizer(): (value: string) => string {+ let lastValue: string | undefined;+ let lastNormalized: string | undefined;++ return (value) => {+ if (lastNormalized === undefined || value !== lastValue) {+ lastValue = value;+ lastNormalized = normalizeSearchText(value);+ }++ return lastNormalized;+ };+}+function buildComparator(operator: string, comparedValue: string) {comparedValue = comparedValue.toLowerCase();