Skip to content

fix(java): 尝试提升 Java 查找速度#3152

Merged
RyogiMutsuki merged 2 commits into
devfrom
fix/3134
Jun 17, 2026
Merged

fix(java): 尝试提升 Java 查找速度#3152
RyogiMutsuki merged 2 commits into
devfrom
fix/3134

Conversation

@Pigeon0v0

@Pigeon0v0 Pigeon0v0 commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

缩小了搜索深度,并进一步框定扫描范围

Resolved #3134

Partly generated by DeepSeek v4 Pro

Summary by Sourcery

提高 Java 运行时扫描性能,并缩小查找 java.exe 安装位置时的搜索范围。

改进内容:

  • 在扫描 Java 运行时时,减少目录遍历的最大深度。
  • 将默认搜索根目录限制在应用数据下更相关的 Minecraft 运行时位置。
  • 引入基于启发式的目录过滤(关键词、排除名称以及类似版本号的模式),以避免遍历无关文件夹。
  • 修复并统一用于目录过滤的 Java 搜索关键词常量名称。
Original summary in English

Summary by Sourcery

Improve Java runtime scanning performance and narrow the search scope for locating java.exe installations.

Enhancements:

  • Reduce maximum directory traversal depth when scanning for Java runtimes.
  • Restrict default search roots to more relevant Minecraft runtime locations under application data.
  • Introduce heuristic-based directory filtering (keywords, excluded names, and version-like patterns) to avoid exploring irrelevant folders.
  • Fix and standardize the Java search keyword constant name for use in directory filtering.

缩小了搜索深度,并进一步框定扫描范围
@pcl-ce-automation pcl-ce-automation Bot added 🛠️ 等待审查 Pull Request 已完善,等待维护者或负责人进行代码审查 size: M PR 大小评估:中型 labels Jun 15, 2026
@sourcery-ai

sourcery-ai Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

此 PR 通过减少搜索深度、收紧默认根目录(尤其是 %APPDATA% 下的路径),并优化 BFS 目录遍历逻辑,使其仅向下进入看起来与 Java 相关或类似版本号的目录,同时修复了 Java 关键字常量中的拼写错误,从而收紧了 Java 运行时扫描逻辑。

更新后的 Java 运行时 BFS 扫描流程图

flowchart TD
    A[Start _BfsSearch] --> B[Dequeue current, depth]
    B --> C{depth > MaxSearchDepth}
    C -->|Yes| D[Skip directory]
    C -->|No| E[EnumerateDirectories current]
    E --> F[For each subDir]
    F --> G[Combine subDir + java.exe]
    G --> H{File.Exists javaExe}
    H -->|Yes| I[results.Add javaExe]
    I --> F
    H -->|No| J{_ShouldExploreDeeper subDir}
    J -->|Yes| K[queue.Enqueue subDir, depth+1]
    J -->|No| F
    D --> L[End when queue empty]
    K --> F
Loading

File-Level Changes

Change Details Files
通过减少基于 BFS 的 Java 可执行文件扫描的广度和深度来加快发现速度,同时避免无关文件夹。
  • 在默认 Java 扫描器中将最大目录搜索深度从 8 降低到 6。
  • 调整默认搜索根目录,使得在 ApplicationData 下只扫描 .minecraft/runtime 子树,而不是整个文件夹。
  • 重构 BFS 目录遍历逻辑,始终枚举子目录,如果当前目录中存在 java.exe,立即记录,并且仅在子目录通过新的过滤谓词(_ShouldExploreDeeper)时才将其加入队列进行更深入的探索。
  • 引入 ShouldExploreDeeper,用于跳过名称包含排除关键字的文件夹,但允许名称中包含与 Java 相关关键字的文件夹,或看起来像版本目录的文件夹(例如:名称由数字、'.'、'' 和 '-' 组成,至少包含一个数字且长度受限)。
  • 新增辅助方法 _IsVersionLikeDirectory,通过 ReadOnlySpan 检查目录名,从而以较低成本判断其是否类似版本字符串。
PCL.Core/Minecraft/Java/Scanner/DefaultPathsScanner.cs
更正并重命名聚合 Java 关键字常量,以便被新的扫描逻辑使用。
  • 将 AllKeyworkds 重命名为 AllKeywords,以修复拼写错误,并与新的目录过滤用法保持一致。
  • 保留该组合数组为 PossibleKeywords 和 MostPossibleKeywords 的并集,用于目录名匹配。
PCL.Core/Minecraft/Java/JavaConsts.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#3134 通过收窄并优化 DefaultPathsScanner 使用的根搜索目录,减少 Java 扫描时间(尤其要避免类似整个用户配置文件和 LocalApplicationData 这样非常大的通用根目录)。 此 PR 将 ApplicationData 根目录收窄为 %AppData%.minecraft\runtime,但仍然将 Environment.SpecialFolder.LocalApplicationData 和 Environment.SpecialFolder.UserProfile 作为完整根目录进行扫描。该 issue 明确指出这些大型目录是导致扫描缓慢的主要原因,因此根目录的问题只得到部分缓解,尚未完全解决。
#3134 在所有 BFS 深度应用基于关键字(及类似)过滤的逻辑,使得只遍历可能与 Java 相关的目录,而不是在进入某个匹配目录后枚举其所有子目录。
#3134 将 Java 扫描的最大 BFS 搜索深度从过大的值降低到更接近典型 Java 安装路径深度的值。

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • 触发新的代码审查: 在 pull request 上评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审查评论。
  • 从审查评论生成 GitHub issue: 通过回复某条审查评论,让 Sourcery 从中创建一个 issue。你也可以在审查评论中回复 @sourcery-ai issue 来从该评论创建 issue。
  • 生成 pull request 标题: 在 pull request 标题的任意位置写入 @sourcery-ai 即可在任意时间生成标题。你也可以在 pull request 上评论 @sourcery-ai title 以(重新)生成标题。
  • 生成 pull request 摘要: 在 pull request 正文的任意位置写入 @sourcery-ai summary,即可在你想要的位置生成 PR 摘要。你也可以在 pull request 上评论 @sourcery-ai summary 以(重新)生成摘要。
  • 生成审查者指南: 在 pull request 上评论 @sourcery-ai guide,即可在任意时间(重新)生成审查者指南。
  • 一次性解决所有 Sourcery 评论: 在 pull request 上评论 @sourcery-ai resolve,即可标记解决所有 Sourcery 评论。如果你已经处理完所有评论且不想再看到它们,这会很有用。
  • 一次性忽略所有 Sourcery 审查: 在 pull request 上评论 @sourcery-ai dismiss,即可忽略所有现有的 Sourcery 审查。若你希望以一次全新的审查重新开始,这尤其有用——别忘了再评论一次 @sourcery-ai review 来触发新的审查!

Customizing Your Experience

访问你的 dashboard 以:

  • 启用或禁用诸如 Sourcery 自动生成的 pull request 摘要、审查者指南等审查功能。
  • 更改审查语言。
  • 添加、移除或编辑自定义审查指令。
  • 调整其他审查设置。

Getting Help

Original review guide in English

Reviewer's Guide

This PR tightens the Java runtime scanning logic by reducing search depth, constraining default roots (especially under %APPDATA%), and refining BFS directory traversal to only descend into directories that look Java-related or version-like, while also fixing a typo in the Java keyword constants.

Flow diagram for updated Java runtime BFS scanning

flowchart TD
    A[Start _BfsSearch] --> B[Dequeue current, depth]
    B --> C{depth > MaxSearchDepth}
    C -->|Yes| D[Skip directory]
    C -->|No| E[EnumerateDirectories current]
    E --> F[For each subDir]
    F --> G[Combine subDir + java.exe]
    G --> H{File.Exists javaExe}
    H -->|Yes| I[results.Add javaExe]
    I --> F
    H -->|No| J{_ShouldExploreDeeper subDir}
    J -->|Yes| K[queue.Enqueue subDir, depth+1]
    J -->|No| F
    D --> L[End when queue empty]
    K --> F
Loading

File-Level Changes

Change Details Files
Reduce breadth and depth of BFS-based Java executable scanning to speed up discovery while avoiding irrelevant folders.
  • Lowered maximum directory search depth from 8 to 6 in the default Java scanner.
  • Adjusted default search roots so that under ApplicationData only the .minecraft/runtime subtree is scanned, instead of the entire folder.
  • Reworked BFS directory traversal to always enumerate subdirectories, immediately record java.exe if present, and only enqueue subdirectories for deeper exploration when they pass a new filtering predicate (_ShouldExploreDeeper).
  • Introduced ShouldExploreDeeper to skip folders whose names contain excluded keywords, but allow those containing Java-related keywords or that look like version directories (e.g., names composed of digits, '.', '' and '-' with at least one digit and bounded length).
  • Added helper _IsVersionLikeDirectory that inspects directory names via ReadOnlySpan to cheaply determine if they resemble a version string.
PCL.Core/Minecraft/Java/Scanner/DefaultPathsScanner.cs
Correct and rename the aggregated Java keyword constant to be used by the new scanning logic.
  • Renamed AllKeyworkds to AllKeywords to fix a typo and align with new directory filter usage.
  • Kept the combined array as the union of PossibleKeywords and MostPossibleKeywords for use in directory name matching.
PCL.Core/Minecraft/Java/JavaConsts.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#3134 Reduce Java scanning time by narrowing and optimizing the root search directories used by DefaultPathsScanner (especially avoiding very large generic roots like the entire user profile and LocalApplicationData). The PR narrows the ApplicationData root to %AppData%.minecraft\runtime, but it still scans Environment.SpecialFolder.LocalApplicationData and Environment.SpecialFolder.UserProfile as full roots. The issue specifically identified these large directories as a major cause of slowness, so the root-directory problem is only partially mitigated, not fully addressed.
#3134 Apply keyword-based (and similar) filtering at all BFS depths so that only plausible Java-related directories are traversed, instead of enumerating all subdirectories once a match is entered.
#3134 Reduce the maximum BFS search depth for Java scanning from an overly large value to something closer to typical Java installation path depth.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai 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.

Hey - 我已经审阅了你的更改,一切看起来都很棒!


Sourcery 对开源项目是免费的——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进后续评审。
Original comment in English

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2671bb2897

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread PCL.Core/Minecraft/Java/Scanner/DefaultPathsScanner.cs

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a30bc400ed

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread PCL.Core/Minecraft/Java/JavaConsts.cs
@RyogiMutsuki

RyogiMutsuki commented Jun 16, 2026

Copy link
Copy Markdown
Member

感觉这里的性能热点应该是阻塞服务加载流程而不是搜索范围过大导致的,这样做应该算是治标不治本?

@Pigeon0v0

Copy link
Copy Markdown
Contributor Author

感觉这里的性能热点应该是阻塞服务加载流程而不是搜索范围过大导致的,这样做应该算是治标不治本?

你说的对 流程应该也得改改 但这留给另一个 PR 吧

@pcl-ce-automation pcl-ce-automation Bot added 🕑 等待合并 已处理完毕,正在等待代码合并入主分支 and removed 🛠️ 等待审查 Pull Request 已完善,等待维护者或负责人进行代码审查 labels Jun 17, 2026
@RyogiMutsuki RyogiMutsuki merged commit f606062 into dev Jun 17, 2026
3 checks passed
@pcl-ce-automation pcl-ce-automation Bot added 👌 完成 相关问题已修复或功能已实现,计划在下次版本更新时正式上线 and removed 🕑 等待合并 已处理完毕,正在等待代码合并入主分支 labels Jun 17, 2026
@RyogiMutsuki RyogiMutsuki deleted the fix/3134 branch June 17, 2026 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size: M PR 大小评估:中型 👌 完成 相关问题已修复或功能已实现,计划在下次版本更新时正式上线

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C#]: 启动器启动耗时 ~37 秒,卡在 Java 扫描阶段 (DefaultPathsScanner BFS)(AI写的issue)

3 participants