Skip to content

Build your personal knowledge base with Trilium Notes

perf(becca): delete attributes in constant time

Elian Dorana42b48d850a6
diff --git a/packages/trilium-core/src/becca/becca-interface.ts b/packages/trilium-core/src/becca/becca-interface.ts
index 249030ccfd..8701c583db 100644
--- a/packages/trilium-core/src/becca/becca-interface.ts
+++ b/packages/trilium-core/src/becca/becca-interface.ts
@@ -26,8 +26,8 @@ export default class Becca {
branches!: Record<string, BBranch>;
childParentToBranch!: Record<string, BBranch>;
attributes!: Record<string, BAttribute>;
- /** Points from attribute type-name to list of attributes */
- attributeIndex!: Record<string, BAttribute[]>;
+ /** Points from attribute type-name to its attributes, in insertion order. */
+ attributeIndex!: Record<string, Set<BAttribute>>;
options!: Record<string, BOption>;
etapiTokens!: Record<string, BEtapiToken>;
@@ -77,7 +77,7 @@ export default class Becca {
}
const key = `${type}-${name}`;
- return Object.hasOwn(this.attributeIndex, key) ? this.attributeIndex[key] : [];
+ return Object.hasOwn(this.attributeIndex, key) ? Array.from(this.attributeIndex[key]) : [];
}
findAttributesWithPrefix(type: string, name: string): BAttribute[] {
@@ -86,7 +86,7 @@ export default class Becca {
for (const idx in this.attributeIndex) {
if (idx.startsWith(key)) {
- resArr.push(this.attributeIndex[idx]);
+ resArr.push(Array.from(this.attributeIndex[idx]));
}
}
diff --git a/packages/trilium-core/src/becca/becca_loader.spec.ts b/packages/trilium-core/src/becca/becca_loader.spec.ts
index ee3e276ab9..fcdfce0c18 100644
--- a/packages/trilium-core/src/becca/becca_loader.spec.ts
+++ b/packages/trilium-core/src/becca/becca_loader.spec.ts
@@ -7,6 +7,7 @@ import { getSql } from "../services/sql/index.js";
import ws from "../services/ws.js";
import becca from "./becca.js";
import beccaLoader, { load, reload } from "./becca_loader.js";
+import BAttribute from "./entities/battribute.js";
import BNote from "./entities/bnote.js";
let counter = 0;
@@ -135,6 +136,35 @@ describe("becca_loader", () => {
expect(Object.keys(becca.attributes).length).toBe(attributesBefore);
});
+
+ it("attributeDeleted removes attributes sharing a name in linear time", () => {
+ const makeLabel = (attributeId: string, noteId: string) => new BAttribute({
+ attributeId, noteId, type: "label", name: "loaderFloodLabel",
+ value: "", isInheritable: false, position: 0
+ });
+ const deleteAttribute = (attribute: BAttribute) => eventService.emit(
+ eventService.ENTITY_DELETED,
+ { entityName: "attributes", entityId: attribute.attributeId, entity: attribute }
+ );
+
+ const flood: BAttribute[] = [];
+ for (let i = 0; i < 40_000; i++) {
+ flood.push(makeLabel(`loaderFlood${i}`, `loaderFloodNote${i % 400}`));
+ }
+ const survivor = makeLabel("loaderFloodSurvivor", "loaderFloodNote0");
+
+ const start = performance.now();
+ for (const attribute of flood) {
+ deleteAttribute(attribute);
+ }
+ expect(performance.now() - start).toBeLessThan(1000);
+
+ expect(becca.findAttributes("label", "loaderFloodLabel")).toEqual([ survivor ]);
+ expect(becca.getNote("loaderFloodNote1")).toBeNull();
+
+ deleteAttribute(survivor);
+ expect(becca.findAttributes("label", "loaderFloodLabel")).toEqual([]);
+ });
});
describe("ENTER_PROTECTED_SESSION listener", () => {
diff --git a/packages/trilium-core/src/becca/becca_loader.ts b/packages/trilium-core/src/becca/becca_loader.ts
index 7f6ee2e2fd..4d74bb07d2 100644
--- a/packages/trilium-core/src/becca/becca_loader.ts
+++ b/packages/trilium-core/src/becca/becca_loader.ts
@@ -448,9 +448,7 @@ function attributeDeleted(attributeId: string) {
const key = `${attribute.type}-${attribute.name.toLowerCase()}`;
- if (key in becca.attributeIndex) {
- becca.attributeIndex[key] = becca.attributeIndex[key].filter((attr) => attr.attributeId !== attribute.attributeId);
- }
+ becca.attributeIndex[key]?.delete(attribute);
dropOrphanedSkeleton(note);
}
diff --git a/packages/trilium-core/src/becca/entities/battribute.ts b/packages/trilium-core/src/becca/entities/battribute.ts
index c76f501038..dadbac2f80 100644
--- a/packages/trilium-core/src/becca/entities/battribute.ts
+++ b/packages/trilium-core/src/becca/entities/battribute.ts
@@ -84,8 +84,8 @@ class BAttribute extends AbstractBeccaEntity<BAttribute> {
this.becca.notes[this.noteId].ownedAttributes.push(this);
const key = `${this.type}-${this.name.toLowerCase()}`;
- this.becca.attributeIndex[key] = this.becca.attributeIndex[key] || [];
- this.becca.attributeIndex[key].push(this);
+ this.becca.attributeIndex[key] ??= new Set();
+ this.becca.attributeIndex[key].add(this);
const targetNote = this.targetNote;