Tencent / Tencent/cherry-markdown

[Bug Report] 关于setValue开启保持光标位置的性能问题

Open
#720 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

🐞bug
Dominant language
JavaScript
Stars
4.9k
Forks
572
Avg merge
17h 5m
Merged PRs (30d)
14

Description

Prerequisites
Describe the Bug

使用中发现setMarkdown时如果开启保持光标位置功能的话,在某些场景下会导致加载明显变慢,初步定位是这段计算导致的

setValue(content, keepCursor = false) {
    this.editor.storeDocumentScroll();
    if (keepCursor === false) {
      return this.editor.editor.setValue(content);
    }
    const codemirror = this.editor.editor;
    const old = this.getValue();
    const pos = codemirror.getDoc().indexFromPos(codemirror.getCursor());
    const newPos = getPosBydiffs(pos, old, content);
    codemirror.setValue(content);
    const cursor = codemirror.getDoc().posFromIndex(newPos); 
    codemirror.setCursor(cursor);
  }

这里对cursor的计算会占用大量时间,由于我这里是编译后的代码调试没有继续去定位是计算cursor还是前面计算pos导致的性能问题,麻烦看下这里的计算逻辑是否还能进行优化

由于是由生产数据观测到的问题我这里没办法给您提供复现的样例,您可以尝试输入一段较长的内容进行setMarkdown看看是否能够复现

同时我还有一点无关痛痒的疑问,在setMarkdown这个过程中他甚至阻塞了我页面上添加的loading的更新(由react状态控制),这是和渲染机制有关吗

Reproduction Steps

No response

System Information

No response

Contributing

None

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the setValue method shown in the report, especially getPosBydiffs, indexFromPos, getCursor, posFromIndex, and setValue. Benchmark setMarkdown with long content while keepCursor is enabled to identify whether position calculation or cursor conversion dominates. Done means locating the bottleneck and improving the delay without changing cursor-preservation behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
frontend, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.