Skip to content

Build your personal knowledge base with Trilium Notes

perf(help): read the help tree once, as its content already was

Elian Doran4b48d09ef5db
diff --git a/apps/server/src/in_app_help_provider.spec.ts b/apps/server/src/in_app_help_provider.spec.ts
index 23a31b3d4b..7d8bc25a0f 100644
--- a/apps/server/src/in_app_help_provider.spec.ts
+++ b/apps/server/src/in_app_help_provider.spec.ts
@@ -16,6 +16,23 @@ describe("NodejsInAppHelpProvider", () => {
expect(provider.getHelpHiddenSubtreeData()).toEqual(data);
});
+ // Every becca load asks for the tree, and there are several in a session.
+ it("reads each file once, however often it is asked", () => {
+ const readFileSync = vi.spyOn(fs, "readFileSync")
+ .mockReturnValue(Buffer.from(JSON.stringify([])) as never);
+
+ const provider = new NodejsInAppHelpProvider();
+ for (let i = 0; i < 3; i++) {
+ provider.getHelpHiddenSubtreeData();
+ provider.getHelpContent();
+ }
+
+ expect(readFileSync.mock.calls.map(([ file ]) => String(file).replace(/^.*[\\/]/, "")))
+ .toEqual([ "help_meta.json", "help_content.json" ]);
+ });
+
+ // A failed read is remembered too, rather than retried on every load: the files ship with the
+ // application, so one that is missing now will be missing for the life of the process.
it("returns an empty list and warns when the meta file cannot be read", () => {
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
vi.spyOn(fs, "readFileSync").mockImplementation(() => {
diff --git a/apps/server/src/in_app_help_provider.ts b/apps/server/src/in_app_help_provider.ts
index 91cbf77434..21354a2a5f 100644
--- a/apps/server/src/in_app_help_provider.ts
+++ b/apps/server/src/in_app_help_provider.ts
@@ -13,10 +13,20 @@ import { RESOURCE_DIR } from "./services/resource_dir.js";
export default class NodejsInAppHelpProvider extends InAppHelpProvider {
/** The guide only changes with the application, so both files are read once per process. */
+ private helpMeta: HiddenSubtreeItem[] | null = null;
private helpContent: HelpBundle | null = null;
+ /**
+ * The tree is asked for on every becca load, of which there are several in a session — a
+ * restore, an import, a consistency check — so it is worth not re-reading a fifth of a
+ * megabyte each time. Callers only read it: the injection builds notes from the items rather
+ * than writing to them.
+ */
getHelpHiddenSubtreeData(): HiddenSubtreeItem[] {
- return readHelpFile<HiddenSubtreeItem[]>("help_meta.json") ?? [];
+ if (!this.helpMeta) {
+ this.helpMeta = readHelpFile<HiddenSubtreeItem[]>("help_meta.json") ?? [];
+ }
+ return this.helpMeta;
}
getHelpContent(): HelpBundle {