Skip to content

Build your personal knowledge base with Trilium Notes

perf(search): resolve each path segment title once per search

Elian Doran135e7374ac3b
diff --git a/packages/trilium-core/src/becca/becca_service.ts b/packages/trilium-core/src/becca/becca_service.ts
index cd1a6b9649..99d2b37a03 100644
--- a/packages/trilium-core/src/becca/becca_service.ts
+++ b/packages/trilium-core/src/becca/becca_service.ts
@@ -67,7 +67,11 @@ function getNoteTitleAndIcon(childNoteId: string, parentNoteId?: string) {
}
}
-function getNoteTitleArrayForPath(notePathArray: string[]) {
+/**
+ * Titles for each segment of a note path. Results under the same ancestors resolve the same
+ * segments, so a caller ranking many of them can pass a cache and resolve each pair once.
+ */
+function getNoteTitleArrayForPath(notePathArray: string[], segmentTitles?: SegmentTitleCache) {
if (!notePathArray || !Array.isArray(notePathArray)) {
throw new Error(`${notePathArray} is not an array.`);
}
@@ -88,9 +92,7 @@ function getNoteTitleArrayForPath(notePathArray: string[]) {
for (const noteId of notePathArray) {
// start collecting path segment titles only after hoisted note
if (hoistedNotePassed) {
- const title = getNoteTitle(noteId, parentNoteId);
-
- titles.push(title);
+ titles.push(segmentTitles ? getCachedNoteTitle(segmentTitles, noteId, parentNoteId) : getNoteTitle(noteId, parentNoteId));
}
if (!hoistedNotePassed && (noteId === hoistedNoteId || outsideOfHoistedSubtree)) {
@@ -103,6 +105,27 @@ function getNoteTitleArrayForPath(notePathArray: string[]) {
return titles;
}
+/** Nested by parent then child, so a lookup builds no key string and allocates nothing. */
+export type SegmentTitleCache = Map<string, Map<string, string>>;
+
+function getCachedNoteTitle(segmentTitles: SegmentTitleCache, noteId: string, parentNoteId: string) {
+ let byChild = segmentTitles.get(parentNoteId);
+
+ if (!byChild) {
+ byChild = new Map();
+ segmentTitles.set(parentNoteId, byChild);
+ }
+
+ let title = byChild.get(noteId);
+
+ if (title === undefined) {
+ title = getNoteTitle(noteId, parentNoteId);
+ byChild.set(noteId, title);
+ }
+
+ return title;
+}
+
function getNoteTitleForPath(notePathArray: string[]) {
const titles = getNoteTitleArrayForPath(notePathArray);
diff --git a/packages/trilium-core/src/services/search/search_result.ts b/packages/trilium-core/src/services/search/search_result.ts
index 212f0b6d7f..8a042aab8a 100644
--- a/packages/trilium-core/src/services/search/search_result.ts
+++ b/packages/trilium-core/src/services/search/search_result.ts
@@ -1,5 +1,5 @@
import becca from "../../becca/becca.js";
-import becca_service from "../../becca/becca_service.js";
+import becca_service, { type SegmentTitleCache } from "../../becca/becca_service.js";
import type { ContentMatchQuality } from "./match_quality.js";
import {
calculateOptimizedEditDistance,
@@ -82,9 +82,9 @@ class SearchResult {
highlightedAttributeSnippet?: string;
private fuzzyScore: number; // Track fuzzy score separately
- constructor(notePathArray: string[]) {
+ constructor(notePathArray: string[], segmentTitles?: SegmentTitleCache) {
this.notePathArray = notePathArray;
- this.pathTitleSegments = becca_service.getNoteTitleArrayForPath(notePathArray);
+ this.pathTitleSegments = becca_service.getNoteTitleArrayForPath(notePathArray, segmentTitles);
this.notePathTitle = this.pathTitleSegments.join(" › ");
this.score = 0;
this.fuzzyScore = 0;
diff --git a/packages/trilium-core/src/services/search/services/search.ts b/packages/trilium-core/src/services/search/services/search.ts
index a4185ff0ee..7ea3059e3b 100644
--- a/packages/trilium-core/src/services/search/services/search.ts
+++ b/packages/trilium-core/src/services/search/services/search.ts
@@ -338,6 +338,8 @@ function performSearch(expression: Expression, searchContext: SearchContext, ena
const noteSet = expression.execute(allNoteSet, executionContext, searchContext);
+ // Results under the same ancestors share path segments, so each pair resolves once per search.
+ const segmentTitles = new Map<string, Map<string, string>>();
const searchResults = noteSet.notes.map((note) => {
const notePathArray = executionContext.noteIdToNotePath[note.noteId] || note.getBestNotePath();
@@ -345,7 +347,7 @@ function performSearch(expression: Expression, searchContext: SearchContext, ena
throw new Error(`Can't find note path for note ${JSON.stringify(note.getPojo())}`);
}
- return new SearchResult(notePathArray);
+ return new SearchResult(notePathArray, segmentTitles);
});
// Derived once rather than per result: every match is scored against the same query.