Skip to content

Optimize deep compare for formatted text#26749

Open
brrichards wants to merge 4 commits intomicrosoft:mainfrom
brrichards:optimize-deep-compare-clean
Open

Optimize deep compare for formatted text#26749
brrichards wants to merge 4 commits intomicrosoft:mainfrom
brrichards:optimize-deep-compare-clean

Conversation

@brrichards
Copy link
Contributor

@brrichards brrichards commented Mar 16, 2026

Summary

Optimize buildDeltaFromTree with cursor-based run detection. buildDeltaFromTree iterated through every character individually via charactersWithFormatting(), calling formatToQuillAttributes and JSON.stringify per character for format comparison. To help with operations like this two new cursor-based API's have been added to FormattedTextAsTree.Tree:

  • getuniformRun(startIndex, maxLength) - returns length of consecutive characters with identical formating using raw cursor comparison. Handles StringLineAtom by always returning 1 (Quill only supports one per line by default).
  • textString(startIndex, length) - returns a substring extracted via cursor, handling both StringTextAtom and StringLineAtom content types.

buildDeltaFromTree now walks the tree in runs rather than character-by-character, calling formatToQuillAttributes() once per run instead of once per character.

@brrichards brrichards requested a review from a team as a code owner March 16, 2026 22:52
Copilot AI review requested due to automatic review settings March 16, 2026 22:52
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes formatted-text to Quill Delta generation by adding cursor-based “run” APIs to FormattedTextAsTree.Tree and updating buildDeltaFromTree to emit ops per uniform formatting run rather than per character.

Changes:

  • Add getUniformRun(startIndex, maxLength?) and textString(startIndex, length) to FormattedTextAsTree.Tree (backed by a borrowed cursor implementation).
  • Update buildDeltaFromTree to walk the tree by uniform runs and call formatToQuillAttributes() once per run.
  • Add unit tests covering getUniformRun/textString, including behavior around line atoms.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
packages/framework/react/src/text/formatted/quillFormattedView.tsx Switch delta building to run-based traversal using the new tree APIs.
packages/dds/tree/src/text/textDomainFormatted.ts Implement new cursor-based APIs (getUniformRun, textString) and expose them on the tree type.
packages/dds/tree/src/test/text/textDomainFormatted.spec.ts Add tests for the new APIs, including line-atom scenarios.

Comment on lines +340 to +345
const limit = Math.min(length, arrayLength - startIndex);
let result = "";

cursor.enterNode(startIndex);
for (let index = 0; index < limit; index++) {
if (index > 0 && !cursor.nextNode()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to assume that .join() is slower for small runs (a sentence, or a paragraph) compared to +=. Maybe the overhead cost is worth it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants