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(board): look up whether a change reaches a card instead of scanning every card
Elian Doran34d53ec40bc6
diff --git a/apps/client/src/widgets/collections/board/data.spec.ts b/apps/client/src/widgets/collections/board/data.spec.tsindex 898d719622..5800b96b07 100644--- a/apps/client/src/widgets/collections/board/data.spec.ts+++ b/apps/client/src/widgets/collections/board/data.spec.ts@@ -9,8 +9,9 @@ import type { PromotedAttribute } from "../promoted_attributes";import type { SortContext, SortKey } from "../sorting";import { INBOX_COLUMN } from "./columns";import {- affectsSortOrder, applyCardMoves, type ColumnMap, type ColumnSort, filterColumnMap,- getBoardData, resolveColumnSorts, resolveSortWatch, sortColumnMap, unfilteredCardIndex+ affectsCardDefinitions, affectsSortOrder, applyCardMoves, type ColumnMap, type ColumnSort,+ definitionSources, filterColumnMap, getBoardData, resolveColumnSorts, resolveSortWatch,+ sortColumnMap, unfilteredCardIndex} from "./data";describe("applyCardMoves", () => {@@ -765,13 +766,51 @@ function noteIdsOf(map: ColumnMap, column: string) {}/** Builds the `LoadResults` a websocket message would produce. See `refresh_reason.spec.ts`. */+describe("affectsCardDefinitions", () => {+ /**+ * A card under `area`, taking attributes from `template`, which sits under `library`. The+ * attributes that reach the card are the ones `attributes.isAffecting()` reports.+ */+ function sources() {+ buildNote({ id: "cdLibrary", title: "Library", children: [+ { id: "cdTemplate", title: "Template" }+ ] });+ buildNote({ id: "cdArea", title: "Area", children: [+ { id: "cdCard", title: "Card", "~template": "cdTemplate" }+ ] });+ buildNote({ id: "cdElsewhere", title: "Elsewhere" });+ return definitionSources([ froca.notes["cdCard"] ]);+ }++ const affects = (name: string, noteId: string, isInheritable = false) => affectsCardDefinitions(+ changes({ attributes: [ [ name, noteId, isInheritable ] ] }), sources());++ it("answers for a definition on the card or on a note it takes attributes from", () => {+ expect(affects("label:priority", "cdCard")).toBe(true);+ expect(affects("relation:owner", "cdTemplate")).toBe(true);+ expect(affects("template", "cdCard")).toBe(true);+ expect(affects("inherit", "cdTemplate")).toBe(true);+ });++ it("answers for a definition above them only when it is inheritable", () => {+ expect(affects("label:priority", "cdArea")).toBe(false);+ expect(affects("label:priority", "cdArea", true)).toBe(true);+ expect(affects("label:priority", "cdLibrary", true)).toBe(true);+ });++ it("leaves out other notes and attributes that are no definition", () => {+ expect(affects("label:priority", "cdElsewhere", true)).toBe(false);+ expect(affects("color", "cdCard")).toBe(false);+ });+});+function changes({ notes = [], attributes = [] }: {notes?: string[];- /** `[ name, noteId ]` pairs. */- attributes?: [ string, string ][];+ /** `[ name, noteId, isInheritable ]` tuples. */+ attributes?: [ string, string, boolean? ][];}) {- const entityChanges = attributes.map(([ name, noteId ], index) => entityChange(- `attr${index}`, { attributeId: `attr${index}`, name, noteId }));+ const entityChanges = attributes.map(([ name, noteId, isInheritable = false ], index) =>+ entityChange(`attr${index}`, { attributeId: `attr${index}`, name, noteId, isInheritable }));const results = new LoadResults(entityChanges);for (const noteId of notes) {diff --git a/apps/client/src/widgets/collections/board/data.ts b/apps/client/src/widgets/collections/board/data.tsindex 87360117b3..c9b16497c4 100644--- a/apps/client/src/widgets/collections/board/data.ts+++ b/apps/client/src/widgets/collections/board/data.ts@@ -396,6 +396,63 @@ async function recursiveGroupBy(}}+/**+ * The notes whose attributes can reach a card, split the way `attributes.isAffecting()` checks+ * them: `own` holds each card and its `~template` and `~inherit` targets, and `ancestors` also+ * holds every note above those, following templates, which only an inheritable attribute reaches+ * from.+ */+export interface DefinitionSources {+ own: Set<string>;+ ancestors: Set<string>;+}++/** Collects the {@link DefinitionSources} of `cards`, visiting each note once. */+export function definitionSources(cards: Iterable<FNote>): DefinitionSources {+ const own = new Set<string>();+ const ancestors = new Set<string>();+ const pending: FNote[] = [];++ for (const card of cards) {+ own.add(card.noteId);+ pending.push(card);+ for (const source of card.getNotesToInheritAttributesFrom()) {+ if (source) {+ own.add(source.noteId);+ }+ }+ }++ for (let note = pending.pop(); note; note = pending.pop()) {+ if (ancestors.has(note.noteId)) {+ continue;+ }+ ancestors.add(note.noteId);+ pending.push(...note.getNotesToInheritAttributesFrom().filter(Boolean),+ ...note.getParentNotes());+ }++ return { own, ancestors };+}++/**+ * Whether a change can alter the definitions that reach a card: a definition, or a `~template` or+ * `~inherit` relation, on a note in `sources`. Equivalent to `attributes.isAffecting()` against+ * every card, at the cost of one lookup per changed attribute.+ */+export function affectsCardDefinitions(loadResults: LoadResults, sources: DefinitionSources) {+ return loadResults.getAttributeRows().some((attr) => isDefinitionSource(attr.name)+ && !!attr.noteId+ && (sources.own.has(attr.noteId)+ || (!!attr.isInheritable && sources.ancestors.has(attr.noteId))));+}++/** Returns whether an attribute with this name can change a note's `getAttributeDefinitions()`. */+function isDefinitionSource(name: string | undefined) {+ return !!name && (name.startsWith("label:") || name.startsWith("relation:")+ || name === "template" || name === "inherit");+}+/** Returns the note of every card in `byColumn`, once per note. */export function cardNotes(byColumn: ColumnMap | undefined): Set<FNote> {const notes = new Set<FNote>();diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsxindex 0d49db4131..324349fffc 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -74,8 +74,9 @@ import { useBoardReference } from "./reference";import { openBoardContextMenu, openCreateColumnMenu } from "./context_menu";import { useBoardSort } from "./sort";import {- affectsSortOrder, applyCardMoves, cardNotes, ColumnMap, filterColumnMap, getBoardData,- resolveColumnSorts, resolveSortWatch, sortColumnMap, unfilteredCardIndex+ affectsCardDefinitions, affectsSortOrder, applyCardMoves, cardNotes, ColumnMap,+ definitionSources, filterColumnMap, getBoardData, resolveColumnSorts, resolveSortWatch,+ sortColumnMap, unfilteredCardIndex} from "./data";import { useBoardKeyboard } from "./keyboard";@@ -533,6 +534,9 @@ export default function BoardView({// Includes the definitions from the cards, and hidden attributes, because a column can sort by// an attribute the cards do not show.const cards = useMemo(() => cardNotes(allByColumn), [ allByColumn ]);+ // Read again after a definition change, since a card can have gained a template.+ const cardDefinitionSources = useMemo(+ () => definitionSources(cards), [ cards, definitionRevision ]);const promotedAttributes = useMemo(() => resolvePromotedAttributes(parentNote, viewConfig?.promotedAttributes, [ statusAttribute ], cards),@@ -1345,9 +1349,8 @@ export default function BoardView({// to group by instead. A definition that reaches a card, directly or through `~template`// or `~inherit`, also changes `promotedAttributes`.if (loadResults.getAttributeRows().some(attr =>- (attr.name?.startsWith("label:") && attributes.isAffecting(attr, parentNote))- || (isDefinitionSource(attr.name)- && [ ...cards ].some(card => attributes.isAffecting(attr, card))))) {+ attr.name?.startsWith("label:") && attributes.isAffecting(attr, parentNote))+ || affectsCardDefinitions(loadResults, cardDefinitionSources)) {setDefinitionRevision(revision => revision + 1);}@@ -1701,12 +1704,6 @@ export function findRefreshReason(loadResults: LoadResults, statusAttribute: strreturn null;}-/** Returns whether an attribute with this name can change a note's `getAttributeDefinitions()`. */-function isDefinitionSource(name: string | undefined) {- return !!name && (name.startsWith("label:") || name.startsWith("relation:")- || name === "template" || name === "inherit");-}-function AddNewColumn({api, isInRelationMode, columnCount, onCreated, isCreating, setIsCreating}: {