Skip to content

Build your personal knowledge base with Trilium Notes

perf(share): check each clone once when finding a note's site position

Elian Doranf3a52f3d736a
diff --git a/packages/share-theme/src/model/page.spec.ts b/packages/share-theme/src/model/page.spec.ts
index 8f69bfecdf..eeedf25705 100644
--- a/packages/share-theme/src/model/page.spec.ts
+++ b/packages/share-theme/src/model/page.spec.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it } from "vitest";
+import { describe, expect, it, vi } from "vitest";
import {
getChildLinks, getContentClasses, getHtmlSnippets, getLastUpdated, getNavigationTree, getPageHead, getPageLanguages,
@@ -255,6 +255,33 @@ describe("getPrevNextLinks", () => {
expect(links(lonely, lonely)).toStrictEqual([ null, null ]);
expect(links(elsewhere)).toStrictEqual([ null, null ]);
});
+
+ it("reads the parents of each note outside the site once, however many paths lead to it", () => {
+ // Sixteen layers of two clones each, every clone a child of both clones of the layer above,
+ // make 2^16 paths from `page` up to the share root outside the site.
+ const shareRoot = fakeNote({ noteId: "shareRoot" });
+ const site = addChild(shareRoot, fakeNote({ noteId: "site" }));
+ const clones: FakeNote[] = [];
+ let layer = [ shareRoot ];
+ for (let depth = 0; depth < 16; depth++) {
+ const next = [ fakeNote({ noteId: `left${depth}` }), fakeNote({ noteId: `right${depth}` }) ];
+ for (const parent of layer) {
+ for (const child of next) {
+ addChild(parent, child);
+ }
+ }
+ clones.push(...next);
+ layer = next;
+ }
+ const page = addChild(layer[0], fakeNote({ noteId: "page" }));
+ addChild(site, page);
+ const reads = clones.map((clone) => vi.spyOn(clone, "getParentNotes"));
+
+ const { previous, next } = getPrevNextLinks(page, site);
+
+ expect([ previous?.title, next ]).toStrictEqual([ "site", null ]);
+ expect(reads.reduce((total, spy) => total + spy.mock.calls.length, 0)).toBeLessThan(100);
+ });
});
describe("getShareLink", () => {
diff --git a/packages/share-theme/src/model/page.ts b/packages/share-theme/src/model/page.ts
index 3324d2905b..8239c6e0d2 100644
--- a/packages/share-theme/src/model/page.ts
+++ b/packages/share-theme/src/model/page.ts
@@ -455,15 +455,25 @@ function getSitePosition(note: ShareNote, siteRoot: ShareNote) {
return null;
}
- const parent = note.getParentNotes().find((candidate) => isInSite(candidate, siteRoot));
+ const answers = new Map<string, boolean>();
+ const parent = note.getParentNotes().find((candidate) => isInSite(candidate, siteRoot, answers));
const siblings = parent?.getVisibleChildNotes() ?? [];
const index = siblings.findIndex((sibling) => sibling.noteId === note.noteId);
return parent && index !== -1 ? { parent, siblings, index } : null;
}
-function isInSite(note: ShareNote, siteRoot: ShareNote): boolean {
- return note.noteId === siteRoot.noteId
- || note.getParentNotes().some((parent) => isInSite(parent, siteRoot));
+/**
+ * Whether `note` is `siteRoot` or below it. `answers` keeps the answer for each note checked, so
+ * that clones reached by many paths are checked once.
+ */
+function isInSite(note: ShareNote, siteRoot: ShareNote, answers: Map<string, boolean>): boolean {
+ let answer = answers.get(note.noteId);
+ if (answer === undefined) {
+ answer = note.noteId === siteRoot.noteId
+ || note.getParentNotes().some((parent) => isInSite(parent, siteRoot, answers));
+ answers.set(note.noteId, answer);
+ }
+ return answer;
}
function toPageLink(note: ShareNote): PageLink {