Skip to content

Fix: issue #309 编辑器内容过多表格数据容易卡顿 。减少node2Vnode,node2html重复执行#592

Merged
cycleccc merged 4 commits into
wangeditor-next:masterfrom
wsczy:309-编辑器内容过多表格数据容易卡顿
May 20, 2025

Hidden character warning

The head ref may contain hidden characters: "309-\u7f16\u8f91\u5668\u5185\u5bb9\u8fc7\u591a\u8868\u683c\u6570\u636e\u5bb9\u6613\u5361\u987f"
Merged

Fix: issue #309 编辑器内容过多表格数据容易卡顿 。减少node2Vnode,node2html重复执行#592
cycleccc merged 4 commits into
wangeditor-next:masterfrom
wsczy:309-编辑器内容过多表格数据容易卡顿

Conversation

@wsczy

@wsczy wsczy commented May 19, 2025

Copy link
Copy Markdown
Contributor

Changes Overview

Implementation Approach

Testing Done

Verification Steps

Additional Notes

Checklist

  • I have created a changeset for this PR if necessary.
  • My changes do not break the library.
  • I have added tests where applicable.
  • I have followed the project guidelines.
  • I have fixed any lint issues.

Related Issues

Summary by CodeRabbit

  • Performance Improvements

    • Enhanced editor responsiveness when handling large tables by introducing caching for virtual nodes and HTML serialization, reducing lag and improving efficiency during heavy content loads.
  • Bug Fixes

    • Addressed issues with lag and stuttering when editing or viewing extensive table data in the editor.

陈子逸 added 3 commits May 19, 2025 17:56
…itor-next into 309-编辑器内容过多表格数据容易卡顿

# Conflicts:
#	packages/core/src/editor/plugins/with-content.ts
#	packages/core/src/text-area/update-view.ts
#	packages/core/src/utils/weak-maps.ts
@coderabbitai

coderabbitai Bot commented May 19, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

This update introduces caching mechanisms to the editor core for improved performance, especially when handling large tables. New weak maps are used to cache virtual nodes and HTML representations of nodes. The editor's content and view update logic now leverage these caches, reducing redundant computations and improving responsiveness with heavy content loads.

Changes

File(s) Change Summary
.changeset/shy-moose-fly.md Documents the update of '@wangeditor-next/core' to optimize performance with large table data, aiming to reduce lag and improve editor responsiveness.
packages/core/src/editor/plugins/with-content.ts Extends the editor's apply method to clear cached vnode and HTML mappings for affected nodes after operations, especially for set_selection. Updates getHtml to cache and reuse HTML strings per top-level child node, introducing memoization for serialization.
packages/core/src/text-area/update-view.ts Modifies updateView to check for and reuse cached vnodes for editor child nodes, updating index tracking as needed. Avoids unnecessary vnode creation, thus optimizing rendering efficiency.
packages/core/src/utils/weak-maps.ts Adds two new weak maps: NODE_TO_VNODE for mapping nodes to their vnodes and indices, and NODE_TO_HTML for mapping nodes to their HTML strings, supporting the new caching and memoization strategies in the editor core.

Sequence Diagram(s)

sequenceDiagram
    participant Editor
    participant WeakMaps
    participant Renderer

    Editor->>WeakMaps: Check NODE_TO_VNODE/NODE_TO_HTML cache for node
    alt Cache hit
        WeakMaps-->>Editor: Return cached vnode/HTML
    else Cache miss
        Editor->>Renderer: Generate vnode/HTML for node
        Renderer-->>Editor: Return vnode/HTML
        Editor->>WeakMaps: Store vnode/HTML in cache
    end
    Editor->>Renderer: Use vnode/HTML for rendering or output
Loading

Poem

In the warren where editors dwell,
Tables once lagged, but now all is well!
With caches for nodes and HTML in tow,
The typing is swift, the scrolling will flow.
A hop and a skip—no more stutter or groan,
This bunny’s delighted with speed newly grown!
🐇✨

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (3)
packages/core/src/text-area/update-view.ts (2)

20-21: Add missing trailing comma.

According to ESLint rules, a trailing comma should be added after NODE_TO_VNODE.

  EDITOR_TO_WINDOW,
-  NODE_TO_VNODE,
+  NODE_TO_VNODE,
  NODE_TO_INDEX

79-94: Fix indentation issues in caching logic.

The caching implementation is good, but there are indentation inconsistencies that should be fixed to follow the project's coding style.

    if (NODE_TO_VNODE.has(node)) {
      const [index, cached] = NODE_TO_VNODE.get(node as any) as any
      if (cached) {
        if (index !== i) {
-            // 设置相关 weakMap 信息
-            NODE_TO_INDEX.set(node, i)
-            NODE_TO_VNODE.set(node, [i, cached])
+          // 设置相关 weakMap 信息
+          NODE_TO_INDEX.set(node, i)
+          NODE_TO_VNODE.set(node, [i, cached])
        }
        return cached
-        }
       }
+    }
    const vnode = node2Vnode(node, i, editor, editor)

    normalizeVnodeData(vnode) // 整理 vnode.data 以符合 snabbdom 的要求
    NODE_TO_VNODE.set(node as any, [i, vnode])
🧰 Tools
🪛 ESLint

[error] 80-80: Expected blank line after variable declarations.

(newline-after-var)


[error] 83-83: Expected indentation of 10 spaces but found 12.

(indent)


[error] 84-84: Expected indentation of 10 spaces but found 12.

(indent)


[error] 85-85: Expected indentation of 10 spaces but found 12.

(indent)


[error] 88-88: Expected indentation of 6 spaces but found 8.

(indent)


[error] 89-89: Expected indentation of 4 spaces but found 6.

(indent)

packages/core/src/editor/plugins/with-content.ts (1)

23-23: Format object import following project style.

This import should use multiple lines to match the project's coding style.

-import { EDITOR_TO_SELECTION, NODE_TO_KEY, NODE_TO_VNODE, NODE_TO_HTML } from '../../utils/weak-maps'
+import { 
+  EDITOR_TO_SELECTION, 
+  NODE_TO_KEY, 
+  NODE_TO_VNODE, 
+  NODE_TO_HTML 
+} from '../../utils/weak-maps'
🧰 Tools
🪛 ESLint

[error] 23-23: Expected a line break after this opening brace.

(object-curly-newline)


[error] 23-23: Expected a line break before this closing brace.

(object-curly-newline)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a0bdd47 and 1ba4543.

📒 Files selected for processing (4)
  • .changeset/shy-moose-fly.md (1 hunks)
  • packages/core/src/editor/plugins/with-content.ts (3 hunks)
  • packages/core/src/text-area/update-view.ts (2 hunks)
  • packages/core/src/utils/weak-maps.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/core/src/editor/plugins/with-content.ts (1)
packages/core/src/utils/weak-maps.ts (3)
  • NODE_TO_KEY (57-57)
  • NODE_TO_VNODE (39-39)
  • NODE_TO_HTML (40-40)
🪛 ESLint
packages/core/src/editor/plugins/with-content.ts

[error] 23-23: Expected a line break after this opening brace.

(object-curly-newline)


[error] 23-23: Expected a line break before this closing brace.

(object-curly-newline)


[error] 186-195: Expected blank line after variable declarations.

(newline-after-var)

packages/core/src/text-area/update-view.ts

[error] 21-22: Missing trailing comma.

(@typescript-eslint/comma-dangle)


[error] 21-22: Missing trailing comma.

(comma-dangle)


[error] 80-80: Expected blank line after variable declarations.

(newline-after-var)


[error] 83-83: Expected indentation of 10 spaces but found 12.

(indent)


[error] 84-84: Expected indentation of 10 spaces but found 12.

(indent)


[error] 85-85: Expected indentation of 10 spaces but found 12.

(indent)


[error] 88-88: Expected indentation of 6 spaces but found 8.

(indent)


[error] 89-89: Expected indentation of 4 spaces but found 6.

(indent)

🔇 Additional comments (7)
packages/core/src/utils/weak-maps.ts (1)

39-40: Added caching for VNodes and HTML rendering.

These two new WeakMaps will help cache virtual nodes and HTML representations of DOM nodes, which can significantly improve performance, especially when handling large tables or complex content structures.

.changeset/shy-moose-fly.md (1)

1-5: Approve changeset addition.

The changeset correctly describes the purpose of this PR: optimizing editor performance when handling large table data.

packages/core/src/text-area/update-view.ts (1)

77-94: Great performance optimization using VNode caching.

The implementation efficiently reuses existing VNodes when possible, reducing unnecessary recreation of virtual DOM nodes. This will have a significant positive impact on performance, especially when dealing with large tables.

Key optimization points:

  1. Checks if a node already has a cached VNode
  2. Updates index tracking when node position changes
  3. Reuses existing VNodes when appropriate
  4. Only creates new VNodes when necessary
🧰 Tools
🪛 ESLint

[error] 80-80: Expected blank line after variable declarations.

(newline-after-var)


[error] 83-83: Expected indentation of 10 spaces but found 12.

(indent)


[error] 84-84: Expected indentation of 10 spaces but found 12.

(indent)


[error] 85-85: Expected indentation of 10 spaces but found 12.

(indent)


[error] 88-88: Expected indentation of 6 spaces but found 8.

(indent)


[error] 89-89: Expected indentation of 4 spaces but found 6.

(indent)

packages/core/src/editor/plugins/with-content.ts (4)

105-111: Handling selection changes - good addition.

Adding support for set_selection operations ensures that nodes affected by selection changes are properly tracked and updated. This helps maintain cache consistency.


121-129: Effective cache invalidation strategy.

The implementation properly clears cached VNodes and HTML strings when nodes change, preventing stale data and potential rendering issues. The check for nodes with a type property helps focus on meaningful nodes.


186-195: HTML caching optimization.

This implementation significantly improves performance by memoizing HTML serialization per node. For editors with large tables or complex content, this can dramatically reduce processing time.

Consider adding a blank line after the const html = ... variable declaration to match the project's coding style.

🧰 Tools
🪛 ESLint

[error] 186-195: Expected blank line after variable declarations.

(newline-after-var)


186-195:

Details

✅ Verification successful

Verify potential memory usage.

While this caching approach will improve performance, it's worth monitoring memory usage in scenarios with extremely large documents to ensure the WeakMap caching doesn't lead to unexpected memory issues.


🏁 Script executed:

#!/bin/bash
# Inspect how many nodes might be cached in a typical document
# This helps assess the memory footprint of the caching strategy

# Look for node creation throughout the codebase
rg -A 2 "createNode|createElement" --type ts

# Check if there are any memory management or cache clearing mechanisms
rg -A 5 "clear.*cache|invalidate.*cache|reset.*cache" --type ts

# Look for any existing performance benchmarks
rg -A 2 "performance|benchmark" --type ts

Length of output: 6267


WeakMap-Based Caching Does Not Require Manual Cache Invalidation

The current implementation uses a WeakMap for NODE_TO_HTML, so entries are automatically released when their corresponding nodes are garbage-collected. Memory usage will only scale with the number of live DOM nodes in the editor tree, and there’s no unexpected/unbounded growth beyond that. No explicit cache-clearing logic or additional benchmarks are required at this time.

🧰 Tools
🪛 ESLint

[error] 186-195: Expected blank line after variable declarations.

(newline-after-var)

@cycleccc cycleccc merged commit e6f4533 into wangeditor-next:master May 20, 2025
3 checks passed
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