mirror of
https://github.com/outline/outline.git
synced 2026-11-22 14:01:41 +03:00
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.
This commit is contained in:
1 parent
b6231f36c9
commit
d5bc88f893
6 files changed
+110
-29
No files matched your search
@@ -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();
|
||||
|
||||
@@ -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<object, Node>();
|
||||
|
||||
/** Comment marks memoized against the document body they were parsed from. */
|
||||
private static commentMarksCache = new WeakMap<object, CommentMark[]>();
|
||||
}
|
||||
@@ -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),
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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<DocumentEvent> {
|
||||
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.
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user