An open source design system that's fully customizable and agent ready
TypeScript
perf(richtext): stand in only long runs of spaces, so ordinary text exports directly (#7095)
Cindy Zhang604e0fd6fad8
diff --git a/packages/richtext/src/markdownSpaceRuns.test.ts b/packages/richtext/src/markdownSpaceRuns.test.tsindex 33ba9b0bd2..f62462f57a 100644--- a/packages/richtext/src/markdownSpaceRuns.test.ts+++ b/packages/richtext/src/markdownSpaceRuns.test.ts@@ -2,7 +2,9 @@import {describe, expect, it} from 'vitest';import {createHeadlessEditor} from '@lexical/headless';+import {$convertToMarkdownString} from '@lexical/markdown';import {$getRoot, $isTextNode} from 'lexical';+import {$convertToMarkdownKeepingTimeLinear} from './markdownSpaceRuns';import {markdownToEditorStateJSON} from './markdownSerializers';import {DEFAULT_NODES} from './editorNodes';import {DEFAULT_TRANSFORMERS} from './markdownTable';@@ -53,10 +55,15 @@ function leastCpuTime(run: () => void): number {}const RUN = ' '.repeat(40_000);+/** A run long enough to take the stand-in path. */+const LONG = ' '.repeat(70);describe('runs of spaces inside text', () => {it.each([['plain text', 'a b\n', 'a bx\n'],+ ['plain text with a long run', `a${LONG}b\n`, `a${LONG}bx\n`],+ ['bold text with a long run', `**a${LONG}b** c\n`, `**a${LONG}b** cx\n`],+ ['link text with a long run', `[a${LONG}b](u) c\n`, `[a${LONG}b](u) cx\n`],['bold text', '**a b** c\n', '**a b** cx\n'],['link text', '[a b](u) c\n', '[a b](u) cx\n'],['tabs and spaces', 'a\t\t b\n', 'a\t\t bx\n'],@@ -106,11 +113,11 @@ function links(markdown: string): Array<[string, string | null]> {describe('stand-ins never touch what the export writes besides text', () => {it.each([- ['an address', 'a b [x](https://e.com/\uE000) c\n'],- ['a title', 'a b [x](https://e.com "t\uE000") c\n'],+ ['an address', `a${LONG}b [x](https://e.com/\uE000) c\n`],+ ['a title', `a${LONG}b [x](https://e.com "t\uE000") c\n`],['a table cell link address',- '| h |\n| - |\n| a b [x](https://e.com/\uE000) |\n',+ `| h |\n| - |\n| a${LONG}b [x](https://e.com/\uE000) |\n`,],])('keeps a private-use character in %s through an edit', (_, markdown) => {const before = links(markdown);@@ -129,10 +136,40 @@ describe('stand-ins never touch what the export writes besides text', () => {for (let round = 0; round < 60; round++) {const url = `https://e.com/${privateUse()}${privateUse()}`;const title = `t${privateUse()}`;- const markdown = `a${' '.repeat(2 + Math.floor(random() * 4))}b [x](${url} "${title}") c${' '.repeat(3)}d\n`;+ const markdown = `a${' '.repeat(64 + Math.floor(random() * 8))}b [x](${url} "${title}") c${LONG}d\n`;const before = links(markdown);expect(before).toEqual([[url, title]]);expect(links(editAndExport(markdown)), markdown).toEqual(before);}});});++describe('ordinary text exports as directly as before', () => {+ it('exports 3,000 paragraphs with double spaces at the direct export time', () => {+ const editor = createHeadlessEditor({+ nodes: [...DEFAULT_NODES],+ onError(error) {+ throw error;+ },+ });+ importMarkdownKeepingSource(+ editor,+ Array.from(+ {length: 3000},+ (_, index) => `Word ${index} more words.`,+ ).join('\n\n'),+ [...DEFAULT_TRANSFORMERS],+ );+ editor.getEditorState().read(() => {+ const root = $getRoot();+ const transformers = [...DEFAULT_TRANSFORMERS];+ const direct = leastCpuTime(() =>+ $convertToMarkdownString(transformers, root),+ );+ const linear = leastCpuTime(() =>+ $convertToMarkdownKeepingTimeLinear(transformers, root),+ );+ expect(linear).toBeLessThan(direct * 1.25 + 5);+ });+ });+});diff --git a/packages/richtext/src/markdownSpaceRuns.ts b/packages/richtext/src/markdownSpaceRuns.tsindex 72afc59be9..4d1a44ddd0 100644--- a/packages/richtext/src/markdownSpaceRuns.ts+++ b/packages/richtext/src/markdownSpaceRuns.ts@@ -22,8 +22,12 @@ import {$convertToMarkdownString} from '@lexical/markdown';import {$isElementNode, $isTextNode} from 'lexical';import {absentCharacters} from './markdownCharacterReferences';-/** Two or more spaces with other characters on both sides. */-const INNER_SPACE_RUN = /(?<=\S)\s{2,}(?=\S)/g;+/**+ * A run of spaces with other characters on both sides, long enough for+ * Lexical's pattern to cost noticeably more than one step per space; shorter+ * runs, as ordinary text has, export as they are.+ */+const INNER_SPACE_RUN = /(?<=\S)\s{64,}(?=\S)/g;/** Whether Lexical exports the text node's text as written, untouched. */function isLiteralText(node: LexicalNode): boolean {@@ -34,40 +38,46 @@ function isLiteralText(node: LexicalNode): boolean {}/**- * Lexical's `$convertToMarkdownString(transformers, root)`, with every run of- * two or more spaces inside a text exported as stand-ins and put back, so the+ * Lexical's `$convertToMarkdownString(transformers, root)`, with every long+ * run of spaces inside a text exported as stand-ins and put back, so the* export takes time linear in the text. The tree is read through views, never- * changed.+ * changed; a tree with no long run exports directly.*/export function $convertToMarkdownKeepingTimeLinear(transformers: Array<Transformer>,root: ElementNode,): string {- // Every string the export can write — texts, and the properties nodes- // serialize, such as a link's address and title — so a stand-in occurs- // nowhere else in the output.- const written: string[] = [];const spaces = new Set<string>();- const visit = (node: LexicalNode) => {- written.push(JSON.stringify(node.exportJSON()));+ const findRuns = (node: LexicalNode) => {if ($isTextNode(node)) {- const text = node.getTextContent();- written.push(text);if (!isLiteralText(node)) {- for (const run of text.match(INNER_SPACE_RUN) ?? []) {+ for (const run of node.getTextContent().match(INNER_SPACE_RUN) ?? []) {for (const space of run) {spaces.add(space);}}}- } else if ($isElementNode(node)) {- node.getChildren().forEach(visit);+ } else if ($isElementNode(node) && !$isCodeNode(node)) {+ node.getChildren().forEach(findRuns);}};- root.getChildren().forEach(visit);+ root.getChildren().forEach(findRuns);if (spaces.size === 0) {return $convertToMarkdownString(transformers, root);}+ // Every string the export can write — texts, and the properties nodes+ // serialize, such as a link's address and title — so a stand-in occurs+ // nowhere else in the output.+ const written: string[] = [];+ const collect = (node: LexicalNode) => {+ written.push(JSON.stringify(node.exportJSON()));+ if ($isTextNode(node)) {+ written.push(node.getTextContent());+ } else if ($isElementNode(node)) {+ node.getChildren().forEach(collect);+ }+ };+ root.getChildren().forEach(collect);const free = absentCharacters(written.join(''));const standIns = new Map<string, string>();for (const space of spaces) {