From d5bc88f893565dbffef237aaee9e9893cd6ac0b9 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 25 Sep 2026 20:57:58 -0400 Subject: [PATCH] perf: Memoize Prosemirror parsing in DocumentHelper (#13892) Nodes are cached against the content or state they were parsed from, so repeated helper calls on the same document parse it once. JSON transform helpers now replace attrs instead of mutating them, as Node.toJSON shares attrs with the cached node. --- server/models/helpers/DocumentHelper.test.ts | 49 +++++++++++++++++++ server/models/helpers/DocumentHelper.tsx | 44 +++++++++++------ server/models/helpers/ProsemirrorHelper.tsx | 26 +++++++--- server/queues/tasks/DocumentUpdateTextTask.ts | 8 +-- server/queues/tasks/ExportJSONTask.ts | 2 +- shared/utils/ProsemirrorHelper.ts | 10 ++-- 6 files changed, 110 insertions(+), 29 deletions(-) diff --git a/server/models/helpers/DocumentHelper.test.ts b/server/models/helpers/DocumentHelper.test.ts index 66b4cee329..646f1047ce 100644 --- a/server/models/helpers/DocumentHelper.test.ts +++ b/server/models/helpers/DocumentHelper.test.ts @@ -4,6 +4,7 @@ import { ChangesetHelper } from "@shared/editor/lib/ChangesetHelper"; import { EditorStyleHelper } from "@shared/editor/styles/EditorStyleHelper"; import { HeadingPrefixStyle } from "@shared/types"; import { DocumentHelper } from "./DocumentHelper"; +import { ProsemirrorHelper } from "./ProsemirrorHelper"; describe("DocumentHelper", () => { beforeAll(() => { @@ -83,6 +84,54 @@ describe("DocumentHelper", () => { }); }); + describe("toProsemirror", () => { + const linkContent = () => ({ + type: "doc", + content: [ + { + type: "paragraph", + content: [ + { + type: "text", + text: "link", + marks: [{ type: "link", attrs: { href: "/doc/internal-123" } }], + }, + ], + }, + ], + }); + + it("should memoize the node for the same content", async () => { + const document = await buildDocument({ content: linkContent() }); + const node = DocumentHelper.toProsemirror(document); + + expect(DocumentHelper.toProsemirror(document)).toBe(node); + }); + + it("should parse again when the content is replaced", async () => { + const document = await buildDocument({ content: linkContent() }); + const node = DocumentHelper.toProsemirror(document); + document.content = linkContent(); + + expect(DocumentHelper.toProsemirror(document)).not.toBe(node); + }); + + it("should not be changed by transforms of its JSON", async () => { + const document = await buildDocument({ content: linkContent() }); + const node = DocumentHelper.toProsemirror(document); + + ProsemirrorHelper.replaceInternalUrls(node, "/s/share-123"); + ProsemirrorHelper.replaceDocumentReferences( + node, + new Map([["internal-123", { id: "other", path: "/doc/other" }]]) + ); + + const text = + DocumentHelper.toProsemirror(document).firstChild?.firstChild; + expect(text?.marks[0].attrs.href).toBe("/doc/internal-123"); + }); + }); + describe("toJSON", () => { it("should return content directly if no transformation required", async () => { const document = await buildDocument(); diff --git a/server/models/helpers/DocumentHelper.tsx b/server/models/helpers/DocumentHelper.tsx index 7bcb4639f1..1e192c4dd1 100644 --- a/server/models/helpers/DocumentHelper.tsx +++ b/server/models/helpers/DocumentHelper.tsx @@ -107,7 +107,8 @@ export class DocumentHelper { /** * Returns the document as a Prosemirror Node. This method uses the derived content if available - * then the collaborative state, otherwise it falls back to Markdown. + * then the collaborative state, otherwise it falls back to Markdown. Results are memoized + * against the content or state they were parsed from, so the returned node must not be mutated. * * @param document The document or revision to convert * @returns The document content as a Prosemirror Node @@ -115,21 +116,33 @@ export class DocumentHelper { static toProsemirror( document: Document | Revision | Collection | ProsemirrorData ) { - if ("type" in document && document.type === "doc") { - return Node.fromJSON(schema, document); - } - if ("content" in document && document.content) { - return Node.fromJSON(schema, document.content); - } - if ("state" in document && document.state) { - const ydoc = new Y.Doc(); - Y.applyUpdate(ydoc, document.state); - return Node.fromJSON(schema, yDocToProsemirrorJSON(ydoc, "default")); + const source = + "type" in document && document.type === "doc" + ? document + : "content" in document && document.content + ? document.content + : "state" in document && document.state + ? document.state + : undefined; + + if (!source) { + const text = + document instanceof Collection ? document.description : document.text; + return parser.parse(text ?? "") || Node.fromJSON(schema, {}); } - const text = - document instanceof Collection ? document.description : document.text; - return parser.parse(text ?? "") || Node.fromJSON(schema, {}); + let node = DocumentHelper.prosemirrorCache.get(source); + if (!node) { + if (source instanceof Uint8Array) { + const ydoc = new Y.Doc(); + Y.applyUpdate(ydoc, source); + node = Node.fromJSON(schema, yDocToProsemirrorJSON(ydoc, "default")); + } else { + node = Node.fromJSON(schema, source); + } + DocumentHelper.prosemirrorCache.set(source, node); + } + return node; } /** @@ -1417,6 +1430,9 @@ export class DocumentHelper { }); } + /** Prosemirror nodes memoized against the content or state they were parsed from. */ + private static prosemirrorCache = new WeakMap(); + /** Comment marks memoized against the document body they were parsed from. */ private static commentMarksCache = new WeakMap(); } diff --git a/server/models/helpers/ProsemirrorHelper.tsx b/server/models/helpers/ProsemirrorHelper.tsx index 2805720cad..3d16e94d95 100644 --- a/server/models/helpers/ProsemirrorHelper.tsx +++ b/server/models/helpers/ProsemirrorHelper.tsx @@ -407,6 +407,7 @@ export class ProsemirrorHelper extends SharedProsemirrorHelper { } function replaceDocumentReferencesInner(node: ProsemirrorData) { + // Node.toJSON shares attrs with the node, so replace them rather than mutate. if ( node.type === "mention" && node.attrs?.type === MentionType.Document && @@ -414,7 +415,7 @@ export class ProsemirrorHelper extends SharedProsemirrorHelper { ) { const replacement = documents.get(node.attrs.modelId); if (replacement) { - node.attrs.modelId = replacement.id; + node.attrs = { ...node.attrs, modelId: replacement.id }; } } @@ -428,7 +429,7 @@ export class ProsemirrorHelper extends SharedProsemirrorHelper { node.text = href; } - mark.attrs.href = href; + mark.attrs = { ...mark.attrs, href }; } }); @@ -468,15 +469,16 @@ export class ProsemirrorHelper extends SharedProsemirrorHelper { } function replaceInternalUrlsInner(node: ProsemirrorData) { + // Node.toJSON shares attrs with the node, so replace them rather than mutate. if (typeof node.attrs?.href === "string") { - node.attrs.href = replaceUrl(node.attrs.href); + node.attrs = { ...node.attrs, href: replaceUrl(node.attrs.href) }; } else if (node.marks) { node.marks.forEach((mark) => { if ( typeof mark.attrs?.href === "string" && isInternalUrl(mark.attrs?.href) ) { - mark.attrs.href = replaceUrl(mark.attrs.href); + mark.attrs = { ...mark.attrs, href: replaceUrl(mark.attrs.href) }; } }); } @@ -561,14 +563,24 @@ export class ProsemirrorHelper extends SharedProsemirrorHelper { } function replaceAttachmentUrls(node: ProsemirrorData) { + // Node.toJSON shares attrs with the node, so replace them rather than mutate. if (node.attrs?.src) { - node.attrs.src = getMapping(node.attrs.src as string); + node.attrs = { + ...node.attrs, + src: getMapping(node.attrs.src as string), + }; } else if (node.attrs?.href) { - node.attrs.href = getMapping(node.attrs.href as string); + node.attrs = { + ...node.attrs, + href: getMapping(node.attrs.href as string), + }; } else if (node.marks) { node.marks.forEach((mark) => { if (mark.attrs?.href) { - mark.attrs.href = getMapping(mark.attrs.href as string); + mark.attrs = { + ...mark.attrs, + href: getMapping(mark.attrs.href as string), + }; } }); } diff --git a/server/queues/tasks/DocumentUpdateTextTask.ts b/server/queues/tasks/DocumentUpdateTextTask.ts index 627380ccbd..a6dc8a98f9 100644 --- a/server/queues/tasks/DocumentUpdateTextTask.ts +++ b/server/queues/tasks/DocumentUpdateTextTask.ts @@ -1,5 +1,4 @@ -import { Node } from "prosemirror-model"; -import { schema, serializer } from "@server/editor"; +import { serializer } from "@server/editor"; import { Document } from "@server/models"; import type { DocumentEvent } from "@server/types"; import { DocumentHelper } from "@server/models/helpers/DocumentHelper"; @@ -13,8 +12,9 @@ export default class DocumentUpdateTextTask extends BaseTask { return; } - const node = Node.fromJSON(schema, document.content); - document.text = serializer.serialize(node); + document.text = serializer.serialize( + DocumentHelper.toProsemirror(document) + ); // Loaded lazily to keep the language-detection corpus off the startup path — // only this worker task needs it. diff --git a/server/queues/tasks/ExportJSONTask.ts b/server/queues/tasks/ExportJSONTask.ts index 9023b38192..443d717303 100644 --- a/server/queues/tasks/ExportJSONTask.ts +++ b/server/queues/tasks/ExportJSONTask.ts @@ -142,7 +142,7 @@ export default class ExportJSONTask extends ExportTask { title: document.title, icon: document.icon, color: document.color, - data: DocumentHelper.toProsemirror(document).toJSON(), + data: await DocumentHelper.toJSON(document), createdById: document.createdById, createdByName: document.createdBy.name, createdByEmail: document.createdBy.email, diff --git a/shared/utils/ProsemirrorHelper.ts b/shared/utils/ProsemirrorHelper.ts index 2b1259e14c..3157d5de6e 100644 --- a/shared/utils/ProsemirrorHelper.ts +++ b/shared/utils/ProsemirrorHelper.ts @@ -63,13 +63,17 @@ export class ProsemirrorHelper { const markSet = new Set(marks); function removeMarksInner(node: ProsemirrorData) { + // Node.toJSON shares attrs with the node, so replace them rather than mutate. if (node.marks) { node.marks = node.marks.filter((mark) => !markSet.has(mark.type)); } if (node.attrs?.marks) { - node.attrs.marks = (node.attrs.marks as { type: string }[])?.filter( - (mark) => !markSet.has(mark.type) - ); + node.attrs = { + ...node.attrs, + marks: (node.attrs.marks as { type: string }[])?.filter( + (mark) => !markSet.has(mark.type) + ), + }; } if (node.content) { node.content.forEach(removeMarksInner);