Skip to content

Conversation

@Reqwey
Copy link
Collaborator

@Reqwey Reqwey commented Oct 25, 2025

Checklist

  • Changes have been tested locally and work as expected.
  • All tests in workflows pass successfully.
  • Code formatting and commit messages align with the project's conventions.
  • Comments have been added for any complex logic or functionality if possible.

This PR is a ..

  • ⚡️ Performance improvement

Related Issues

  • Describe the source of related requirements, such as links to relevant issue discussions.
  • e.g. close #xxxx, fix #xxxx

Description

  • 控制并发数,避免过度占用 CPU 和内存
  • 优化模组翻译缓存逻辑,显著提高缓存命中率

Additional Context

  • Add any other relevant information or screenshots here.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR optimizes the mod list translation retrieval process to improve performance through concurrency control and enhanced caching. The changes focus on reducing redundant translation lookups and preventing resource exhaustion.

Key changes:

  • Implements semaphore-based concurrency control to limit parallel mod processing tasks
  • Refactors cache logic to use file names as keys instead of file paths, improving cache hit rate
  • Consolidates cache persistence to a single write operation at the end of mod list retrieval

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
src-tauri/src/lib.rs Initializes and manages the mod translation cache at application startup
src-tauri/src/instance/helpers/mods/common.rs Refactors cache structure, removes async file I/O functions, switches cache key from file path to file name
src-tauri/src/instance/constants.rs Adds constants for cache expiry time and cache file name
src-tauri/src/instance/commands.rs Implements semaphore for concurrency control, consolidates cache updates to single write operation

};
let file_path = mod_info.file_path.to_string_lossy().to_string();
let cache = load_local_mod_translations_cache(app).await;
let file_name = mod_info.file_name.clone();
Copy link

Copilot AI Oct 25, 2025

Choose a reason for hiding this comment

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

Unnecessary clone operation. The file_name field is already a String and is only used for lookups and logging. Consider borrowing it instead of cloning, or use &mod_info.file_name directly in subsequent operations.

Copilot uses AI. Check for mistakes.
));
for path in mod_paths {
let task = tokio::spawn(async move { get_mod_info_from_jar(&path).await.ok() });
let permit = semaphore.clone().acquire_owned().await.unwrap();
Copy link

Copilot AI Oct 25, 2025

Choose a reason for hiding this comment

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

Using unwrap() on semaphore acquisition will panic if the semaphore is closed or if there's an error. Consider using proper error handling with ? operator or providing a meaningful error message.

Copilot uses AI. Check for mistakes.
Copy link
Owner

Choose a reason for hiding this comment

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

Copilot 说的在理,这里做一下错误返回而不是 panic

Comment on lines 509 to 510
let permit = semaphore.clone().acquire_owned().await.unwrap();
let task = tokio::spawn(async move {
Copy link

Copilot AI Oct 25, 2025

Choose a reason for hiding this comment

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

Using unwrap() on semaphore acquisition will panic if the semaphore is closed or if there's an error. Consider using proper error handling with ? operator or providing a meaningful error message.

Suggested change
let permit = semaphore.clone().acquire_owned().await.unwrap();
let task = tokio::spawn(async move {
let task = tokio::spawn(async move {
let permit = semaphore.clone().acquire_owned().await.ok()?;

Copilot uses AI. Check for mistakes.
@@ -1 +1,3 @@
pub const INSTANCE_CFG_FILE_NAME: &str = "sjmclcfg.json";
pub const CACHE_EXPIRY_HOURS: u64 = 24;
Copy link
Owner

Choose a reason for hiding this comment

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

这两个 const 放在这就有点意义混淆了,建议增加 TRANSLATION_ 前缀

));
for path in mod_paths {
let task = tokio::spawn(async move { get_mod_info_from_jar(&path).await.ok() });
let permit = semaphore.clone().acquire_owned().await.unwrap();
Copy link
Owner

Choose a reason for hiding this comment

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

Copilot 说的在理,这里做一下错误返回而不是 panic

let task = tokio::spawn(async move { get_mod_info_from_jar(&path).await.ok() });
let permit = semaphore.clone().acquire_owned().await.unwrap();
let task = tokio::spawn(async move {
info!("Processing mod: {}", path.display());
Copy link
Owner

@UNIkeEN UNIkeEN Oct 26, 2025

Choose a reason for hiding this comment

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

log 信息请改为:Load mod info from jar: {}

下面那个请改为:
log::debug!("Load mod info from dir: {}", path.display())

(可以不用 use log,直接 log::info!, log::debug!,更明显说明这是 logger 提供的方法,否则 error! 容易有误解)

@Reqwey Reqwey requested a review from UNIkeEN October 26, 2025 11:27
@UNIkeEN UNIkeEN requested a review from ToolmanP October 27, 2025 01:12
@ToolmanP
Copy link
Collaborator

话说这个translation cache json大小大吗?

@Reqwey
Copy link
Collaborator Author

Reqwey commented Oct 29, 2025

话说这个translation cache json大小大吗?

我装了五个大整合包后会有点大的,会到400KB

@ToolmanP
Copy link
Collaborator

ToolmanP commented Nov 1, 2025

话说这个translation cache json大小大吗?

我装了五个大整合包后会有点大的,会到400KB

会影响启动时间吗 有点好奇

@Reqwey
Copy link
Collaborator Author

Reqwey commented Nov 2, 2025

话说这个translation cache json大小大吗?

我装了五个大整合包后会有点大的,会到400KB

会影响启动时间吗 有点好奇

基本没有捏

@Reqwey
Copy link
Collaborator Author

Reqwey commented Nov 3, 2025

可以缓解 #1100

@UNIkeEN UNIkeEN merged commit 7b1df19 into UNIkeEN:main Nov 7, 2025
2 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.

3 participants