Skip to content

Conversation

@GaoFuhong
Copy link
Collaborator

📝 Description

优化了在使用曼哈顿路由时批量拖拽节点卡顿的问题:

  • 共享 ObstacleMap 缓存:同一帧更新内复用 ObstacleMap ,避免每条边重复构建
  • 路由开放集改为二叉堆:将按值排序数组替换为最小堆, add/pop 从 O(n) 降到 O(log n)
  • 路由降级:针对 manhattan(或 metro) 路由,在批量拖拽节点(使用 Selection plugin)过程中,支持配置 movingRouterFallback 进行路由降级,达到减少拖拽过程频繁计算路径的 CPU 消耗,从而提升流畅性。

🖼️ Screenshot

Before After
manhattan_router_before manhattan_router_after

💡 Motivation and Context

🧩 Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Enhancement (changes that improvement of current feature or performance)
  • Refactoring (changes that neither fixes a bug nor adds a feature)
  • Test Case (changes that add missing tests or correct existing tests)
  • Code style optimization (changes that do not affect the meaning of the code)
  • Docs (changes that only update documentation)
  • Chore (changes that don't modify src or test files)

🔍 Self Check before Merge

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @GaoFuhong, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在显著提升在使用曼哈顿路由时批量拖拽节点的流畅性。通过引入共享障碍图缓存机制,减少了不必要的重复计算;同时,将路由算法中的开放集数据结构从排序数组优化为二叉堆,大幅提高了路径查找的效率。此外,为解决批量拖拽时频繁路由计算导致的卡顿问题,Selection 插件新增了路由降级功能,允许在拖拽期间临时切换到性能开销更小的路由,从而确保用户界面的响应性和流畅度。

Highlights

  • 曼哈顿路由性能优化: 引入共享 ObstacleMap 缓存,避免重复构建,并使用二叉堆优化路由开放集,将 add/pop 操作复杂度从 O(n) 降低到 O(log n)。
  • 批量拖拽流畅性提升: Selection 插件新增 movingRouterFallback 配置项,在批量拖拽节点时,可将连接边的曼哈顿路由临时降级为更轻量的路由(如 orth),拖拽停止后自动恢复,显著减少 CPU 消耗并提升拖拽体验。
  • 文档更新与测试覆盖: 相应地更新了路由和选择插件的英文和中文文档,并增加了针对路由降级功能的新测试用例。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

本次 PR 旨在通过共享 ObstacleMap 缓存、使用二叉堆优化开放集以及在批量拖拽时进行路由降级等方式,优化曼哈顿路由在批量拖拽节点时的性能和流畅性。代码改动清晰地实现了这些优化目标,特别是 SortedSet 的重构和 ObstacleMap 的缓存机制,对性能提升有显著帮助。文档和测试用例也得到了相应的更新,覆盖了新功能。

我在代码中发现了一个可能导致缓存失效的严重问题,并提出了一些关于性能和代码可维护性的改进建议,希望能帮助进一步提升代码质量。

@codecov-commenter
Copy link

codecov-commenter commented Dec 25, 2025

Codecov Report

❌ Patch coverage is 95.56452% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.21%. Comparing base (403cbae) to head (59384f0).

Files with missing lines Patch % Lines
src/plugin/selection/selection.ts 92.22% 7 Missing ⚠️
src/registry/router/manhattan/router.ts 85.71% 3 Missing ⚠️
src/registry/router/manhattan/obstacle-map.ts 98.50% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4979      +/-   ##
==========================================
+ Coverage   82.72%   83.21%   +0.48%     
==========================================
  Files         334      334              
  Lines       38186    38390     +204     
  Branches     7756     7858     +102     
==========================================
+ Hits        31591    31946     +355     
+ Misses       6572     6421     -151     
  Partials       23       23              
Flag Coverage Δ
x6 83.21% <95.56%> (+0.48%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/registry/router/manhattan/sorted-set.ts 100.00% <100.00%> (ø)
src/registry/router/manhattan/obstacle-map.ts 97.91% <98.50%> (+0.44%) ⬆️
src/registry/router/manhattan/router.ts 78.52% <85.71%> (-1.34%) ⬇️
src/plugin/selection/selection.ts 85.19% <92.22%> (+2.85%) ⬆️

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@GaoFuhong
Copy link
Collaborator Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

本次 PR 进行了几项重要的性能优化,显著提升了在使用曼哈顿路由时批量拖拽节点的流畅度。

主要改动包括:

  1. SortedSet 实现从排序数组改为二叉堆:这是一个核心的算法改进,将 add/pop 操作的时间复杂度从 O(n) 降低到 O(log n),极大地提升了路由寻路的效率。
  2. 共享并缓存 ObstacleMap:通过 WeakMap 为每个 Model 实例缓存 ObstacleMap,并利用事件监听机制实现缓存失效,有效避免了在同一帧内为每条边重复构建障碍物地图的问题。
  3. 拖拽时路由降级:新增了 movingRouterFallback 选项,允许在批量拖拽节点时临时将高消耗的 manhattan 路由降级为轻量的 orth 路由,减少了拖拽过程中的计算量,提升了交互的流畅性。

代码实现质量很高,新功能也配备了相应的文档和单元测试。我只发现了一个关于代码清晰度的小问题,具体请看我的评论。

整体来看,这是一次非常出色的重构和性能优化!

@GaoFuhong
Copy link
Collaborator Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

本次 PR 围绕曼哈顿路由在批量拖拽节点时的性能问题进行了重构和优化,整体质量非常高。主要改动包括:共享 ObstacleMap 缓存、将路由开放集改为二叉堆、以及增加了拖拽过程中的路由降级策略。这些改动设计精良,实现稳健,显著提升了性能和用户体验。同时,相关的文档和测试也得到了相应的更新。

我发现了一些可以改进的小地方:一处文档示例代码可以更清晰,以及一处缓存失效逻辑中存在冗余的事件监听。具体细节请查看我的评论。

Comment on lines 143 to 144
model.on('node:change:position', () => markDirty(model))
model.on('node:change:size', () => markDirty(model))
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

事件监听器存在冗余。cell:change:positioncell:change:size 事件已经覆盖了对节点的监听。因此,node:change:positionnode:change:size 的监听器是多余的,可以移除以简化代码并避免不必要的 markDirty 调用。

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.

多节点(100+)加上多边(100+)情况下,边设置了曼哈顿路由 ,全选节点拖拽卡顿

3 participants