Skip to content

fix(editor): optimize key shortcut retrieval and improve performance#482

Closed
dengzhongyuan365-dev wants to merge 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:master
Closed

fix(editor): optimize key shortcut retrieval and improve performance#482
dengzhongyuan365-dev wants to merge 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:master

Conversation

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member
  • Implemented caching for key shortcuts to reduce redundant settings lookups.
  • Introduced a mechanism to clear the cache when relevant settings change, enhancing responsiveness.
  • Updated left area widget refresh logic to improve performance during text editing.

PMS: bug-368159

Log: 优化快捷键获取逻辑,提升编辑器性能和响应速度。

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sorry @dengzhongyuan365-dev, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

- Implemented caching for key shortcuts to reduce redundant settings lookups.
- Introduced a mechanism to clear the cache when relevant settings change, enhancing responsiveness.
- Updated left area widget refresh logic to improve performance during text editing.

PMS: bug-368159

Log: 优化快捷键获取逻辑,提升编辑器性能和响应速度。
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:98分

■ 【总体评价】

代码通过引入定时器节流防抖与静态缓存机制,完美解决了编辑器高频输入时的卡顿问题
逻辑完全正确且性能提升显著,仅因一处复制粘贴导致的冗余日志扣除极少量分数

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

修复方案彻底废弃了原先极易引发状态混乱的 shouldDeferCursorHeavyUpdate 等一整套延迟刷新状态机,将 applyCursorHeavyUpdate 的核心逻辑直接合并至 cursorPositionChanged 槽函数中,消除了复杂的多分支判断与状态标志位维护。utils.cpp 中补充了对 settingssettings->settings 的空指针前置拦截,避免了原先直接解引用潜在的崩溃风险。新增的 updateHighlightBracketsAll 函数通过静态字符串匹配实现了精准的快速剪枝逻辑。
建议:移除 undo_() 函数中因复制粘贴意外产生的重复调试日志

  • 2.代码质量(良好)✓

代码删减了大量冗余且难以维护的旧逻辑(如 flushDeferredCursorUpdatescheduleDeferredCursorUpdateBurst 等),整体结构大幅精简,可读性与可维护性显著提升。引入的 m_leftAreaUpdateTimerm_highlightDebounceTimer 命名清晰,职责明确,符合现代 GUI 开发的防抖节流最佳实践。updateMark 函数中删除了多余的 renderAllSelections() 调用并补充了清晰的注释说明调度关系的改变。
潜在问题:undo_() 函数中存在连续两行完全相同的 qDebug() << "Starting undo operation"; 输出,属于复制粘贴遗留的代码瑕疵
建议:删除 src/editor/dtextedit.cppundo_() 函数内多余的 qDebug() 调用

  • 3.代码性能(高效)✓

性能优化效果极其显著,精准打击了卡顿痛点。utils.cpp 使用静态 QHash 缓存快捷键查询结果,彻底消除了每次按键都穿透至 DSettings 底层带来的性能损耗。dtextedit.cpp 使用 50ms 定时器对左侧栏更新进行节流,有效合并了连续按键产生的冗余重绘。updateHighlightBracketsAll 增加的前置字符判断直接跳过非括号场景下的三次正则或遍历计算。editwrapper.cpp 的 150ms 高亮防抖从根本上阻断了快速输入时高频重布局引发的界面卡顿。
建议:当前节流防抖时间阈值(50ms与150ms)设定合理,建议后续在极端低性能设备上可考虑将其提取为可配置参数

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码未引入任何安全漏洞。getKeyshortcutFromKeymap 中使用 settings 对象作为 QObject::connect 的上下文参数,确保了当 settings 被销毁时 lambda 连接能够自动安全断开,避免了悬空指针引用。静态局部变量 cache 的生命周期由系统管理,在单线程 UI 环境下不存在竞态条件风险。新增的空指针检查进一步增强了代码的防御性编程水平。
建议:保持当前的防御性编程风格

■ 【改进建议代码示例】

diff --git a/src/editor/dtextedit.cpp b/src/editor/dtextedit.cpp
index f66ec625..89f1b66c 100644
--- a/src/editor/dtextedit.cpp
+++ b/src/editor/dtextedit.cpp
@@ -3572,7 +3572,6 @@ void TextEdit::undo_()
 {
     qDebug() << "Starting undo operation";
-    qDebug() << "Starting undo operation";
     if (!m_pUndoStack->canUndo()) {
         qDebug() << "Undo operation skipped - nothing to undo";
         return;

@dengzhongyuan365-dev dengzhongyuan365-dev marked this pull request as draft July 2, 2026 08:47
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