Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(experimental): dev simple speed up #1186

Merged
merged 25 commits into from
May 31, 2024
Merged

feat(experimental): dev simple speed up #1186

merged 25 commits into from
May 31, 2024

Conversation

stormslowly
Copy link
Member

@stormslowly stormslowly commented May 21, 2024

SSU experimental implement

enabled only when --watch mode is on and env variable set SSU=true
alpha testing with a few internal teams

then main process

  1. use modify_config event to change entry to SSU's entry, to set chunkMap patch, external Patch and HMR
  2. use new event next_build to store all the dependencies and defer dependence module build.
  3. use new event after_build to determine whether continue build dependence or just use SSU cached chunk files
  4. use new event after_generate_chunk_files to gather all the dependences's chunk, this info will used in SSU entry and runtime

future plan

  1. better chunk group strategy, to reduce init chunks size in page load; currently a huge node_modules chunk will loaded。
  2. remove virtual:C:/node_modules/css/css.css hack
  3. more accurate cache hash and integrity checking of cached chunk files

Summary by CodeRabbit

  • 新功能

    • Cargo.toml 文件中添加了 dashmap 依赖项。
    • 在各种结构体中添加了 HashEq 实现。
    • 明确实现了 HashPartialEq 用于 File
    • 添加了 fn parse_path(path: &str) -> Result<(PathName, Search, Params, Fragment)> {
  • 优化

    • 修改了 Compiler 实现,引入了基于 plugin_driver 的条件逻辑。
  • 插件

    • plugin 模块中添加了 NextBuildParam
    • 修改了 BuildTasksError 的错误处理。
    • 更新了 Compiler 实现,引入了基于 plugin_driver 的条件逻辑。
  • 性能优化

    • 修改了 Compiler 实现中关于日志记录的部分。
  • 配置

    • 为多个结构体的序列化和反序列化添加了 Hash 特性。
  • 其他

    • DevServer 实现中更改了对 port 值的处理。

Copy link
Contributor

coderabbitai bot commented May 21, 2024

Walkthrough

总体变更概述:在 mako crate 和其相关模块中进行了多项修改,包括添加 Hash trait、调整结构体实现、更新逻辑以及引入新功能。这些变更涉及到模块间的依赖关系、优化、插件处理等方面,旨在改进构建过程和性能。

Changes

文件路径 变更概要
crates/mako/Cargo.toml 添加 dashmap 依赖版本 4.0.1
crates/mako/src/ast/file.rs 实现 HashEq trait,添加 parse_path 函数。
crates/mako/src/build/mod.rs 引入 NextBuildParam,修改 BuildTasksError 错误处理,调整 Compiler 实现。
crates/mako/src/compiler.rs 导入 debug 用于日志记录,根据环境变量推送插件,添加构建过程调试日志。
crates/mako/src/config/config.rs 为多个结构体添加 Hash trait,以加强序列化和反序列化过程中的哈希功能。
crates/mako/src/dev/mod.rs 修改 port 处理逻辑,根据文件扩展名确定 content_type,处理缓存依赖。
crates/mako/src/generate/chunk.rs 修改处理 info 字段的逻辑,增强对其存在性的检查。
crates/mako/src/generate/chunk_pot/ast_impl.rs 修改 render_css_chunk 函数逻辑。
crates/mako/src/generate/chunk_pot/mod.rs 更新处理 module_map 为空和跳过缺失信息的逻辑。
crates/mako/src/generate/mod.rs 更新 Compiler 实现,生成 chunk 文件后调用特定函数。
crates/mako/src/generate/optimize_chunk.rs 修改 optimize_info 访问和更新逻辑。
crates/mako/src/generate/transform.rs 添加 ModuleType 导入,基于模块类型过滤,调整错误处理。
crates/mako/src/module.rs 添加 ModuleType::PlaceHolder 变体,修改方法以处理新类型。
crates/mako/src/module_graph.rs 修改获取模块信息中 is_async 标志的逻辑。
crates/mako/src/plugin.rs 添加 CompilerPlugin trait,替换 ModuleAstModuleId,引入 ChunkFileResolverResource,新增方法和结构体。
crates/mako/src/plugins/async_runtime/mod.rs 修改迭代器中的条件检查逻辑。
crates/mako/src/plugins/farm_tree_shake/shake.rs 添加对模块类型和占位符状态的条件检查。
crates/mako/src/plugins/ssu.rs 引入 SUPlus 插件,优化构建性能,管理依赖和缓存状态。
crates/mako/src/visitors/async_module.rs 更新标记模块为异步的逻辑。
examples/with-antd/mako.config.json mako.config.json 中添加 "clean": false
packages/bundler-mako/index.js 处理 SSU 缓存块的条件块。
packages/mako/binding.d.ts 添加 TransformOutput 接口。

Poem

小兔码农忙修改,结构体添哈希诀窍。
编译优化新思路,日志记录调试酷。
插件功能添新貌,异步模块独具秀。
构建性能提升多,开发者欣喜呼。 🐇📦🌟


Note

Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://coderabbit.ai

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 9

Outside diff range and nitpick comments (1)
crates/mako/src/plugins/ssu.rs (1)

66-73: SUPlus 结构体包含多个 Arc<Mutex<>> 类型的字段,这可能会导致运行时的性能问题,特别是在多线程环境中。考虑是否有可能通过改进设计来减少锁的使用。

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between c24e72b and c11f369.
Files selected for processing (1)
  • crates/mako/src/plugins/ssu.rs (1 hunks)

crates/mako/src/plugins/ssu.rs Show resolved Hide resolved
Comment on lines 167 to 235
fn load(&self, param: &PluginLoadParam, context: &Arc<Context>) -> Result<Option<Content>> {
if param.file.path.starts_with(SSU_ENTRY_PREFIX) {
let path_string = param.file.path.to_string_lossy().to_string();
let start = SSU_ENTRY_PREFIX.len();
let path = PathBuf::from(path_string.as_str()[start..].to_string());

let mut require_externals = context
.config
.externals
.iter()
.map(|ext| format!("require('{}');", ext.0))
.collect::<Vec<_>>();

require_externals.sort();

let mut reverse_require = self
.cached_state
.lock()
.unwrap()
.reversed_required_files
.iter()
.map(|f| format!("require('{}')", f))
.collect::<Vec<_>>();
reverse_require.sort();

let port = &context.config.hmr.as_ref().unwrap().port.to_string();
let host = &context.config.hmr.as_ref().unwrap().host.to_string();
let host = if host == "0.0.0.0" { "127.0.0.1" } else { host };
let hmr_runtime = include_str!("../runtime/runtime_hmr_entry.js")
.to_string()
.replace("__PORT__", port)
.replace("__HOST__", host);

let content = format!(
r#"
require("virtual:C:/node_modules/css/css.css");
let patch = require._su_patch();
console.log(patch);
{}
module.export = Promise.all(
patch.map((d)=>__mako_require__.ensure(d))
).then(()=>{{
{}
{}
return require("{}");
}}, console.log);
"#,
require_externals.join("\n"),
hmr_runtime,
reverse_require.join("\n"),
path.to_string_lossy()
);

debug!("entry content:\n{}", content);

return Ok(Some(Content::Js(JsContent {
content,
is_jsx: false,
})));
}

if param
.file
.path
.starts_with("virtual:C:/node_modules/css/css.css")
{
return Ok(Some(Content::Css("._mako_mock_css { }".to_string())));
}
Ok(None)
Copy link
Contributor

Choose a reason for hiding this comment

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

load 方法中,处理了特定前缀的文件路径。这种硬编码的路径可能会导致在不同环境下的兼容性问题。建议使用配置文件或环境变量来管理这些路径。

Comment on lines +238 to +310
fn next_build(&self, next_build_param: &NextBuildParam) -> bool {
let from: CodeType = next_build_param
.current_module
.id
.contains("/node_modules/")
.into();
let to = next_build_param.next_file.is_under_node_modules.into();

debug!(
"{} -> {}",
next_build_param.current_module.id,
next_build_param
.next_file
.pathname
.to_string_lossy()
.to_string()
);

match (from, to) {
(CodeType::SourceCode, CodeType::Dependency) => {
if let ResolverResource::Resolved(resolved) = &next_build_param.resource {
self.dependence_node_module_files
.insert(next_build_param.next_file.clone());

let path_name = next_build_param
.next_file
.path
.to_string_lossy()
.to_string();

let version = resolved
.0
.package_json()
.and_then(|p| p.raw_json().get("version"))
.map_or("0.0.0".to_string(), |v| {
v.as_str().unwrap_or("0.0.0").to_string()
});

self.current_state
.lock()
.unwrap()
.cached_boundaries
.insert(path_name, version);

let scanning = *self.scanning.lock().unwrap();
!scanning
} else {
true
}
}
(CodeType::Dependency, CodeType::SourceCode) => {
debug!(
"{} -> {}",
next_build_param.current_module.id,
next_build_param.next_file.pathname.to_string_lossy()
);

self.current_state
.lock()
.unwrap()
.reversed_required_files
.insert(
next_build_param
.next_file
.pathname
.to_string_lossy()
.to_string(),
);
true
}
_ => true,
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

next_build 方法中的逻辑较为复杂,涉及多个条件判断和状态转换。建议添加详细的日志记录和错误处理,以便于问题追踪和调试。

crates/mako/src/plugins/ssu.rs Outdated Show resolved Hide resolved
Comment on lines +426 to +488
fn build_start(&self, context: &Arc<Context>) -> Result<Option<()>> {
if let Some(content) = self.load_cached_state(context) {
let mut state = self.cached_state.lock().unwrap();
*state = content;
}

self.current_state.lock().unwrap().config_hash = Self::config_hash(&context.config);

Ok(None)
}

fn runtime_plugins(&self, _context: &Arc<Context>) -> Result<Vec<String>> {
if *self.enabled.lock().unwrap() {
let cache = self.cached_state.lock().unwrap();

let code = format!(
r#"
requireModule._su_patch = function(){{
var js_patch = {};
var css_patch = {};
for(var key in js_patch) {{
chunksIdToUrlMap[key] = js_patch[key];
}}
for(var key in js_patch) {{
cssChunksIdToUrlMap[key] = css_patch[key];
}}
return Object.keys(js_patch).sort();
}}
"#,
serde_json::to_string(&cache.js_patch_map).unwrap(),
serde_json::to_string(&cache.css_patch_map).unwrap(),
);

Ok(vec![code])
} else {
let cg = _context.chunk_graph.read().unwrap();

cg.get_chunks()
.into_iter()
.filter(|c| c.chunk_type == ChunkType::Sync)
.for_each(|c| {
println!("chunk: {}", c.filename());
});

Ok(vec![r#"
requireModule._su_patch = function(){
var js_patch = {
"node_modules": "node_modules.js"
};
var css_patch = {
"node_modules": "node_modules.css"
};
for(var key in js_patch) {
chunksIdToUrlMap[key] = js_patch[key];
}
for(var key in js_patch) {
cssChunksIdToUrlMap[key] = css_patch[key];
}
return ["node_modules"];
}"#
.to_string()])
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

runtime_plugins 方法中,生成的代码片段使用了大量的硬编码字符串。这可能会导致维护困难和错误。建议将这些字符串提取为常量或配置项。

crates/mako/src/plugins/ssu.rs Show resolved Hide resolved
Comment on lines 105 to 109
fn write_current_cache_state(&self, context: &Arc<Context>) {
let cache_file = context.root.join("node_modules/.cache_mako/meta.json");
let cache = self.current_state.lock().unwrap();
fs::write(cache_file, serde_json::to_string(&*cache).unwrap()).unwrap();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

write_current_cache_state 方法中直接使用了 unwrap(),这在生产环境中可能导致程序意外崩溃。建议改为返回错误,让调用者处理。

- fs::write(cache_file, serde_json::to_string(&*cache).unwrap()).unwrap();
+ let cache_data = serde_json::to_string(&*cache)?;
+ fs::write(cache_file, cache_data)?;

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
fn write_current_cache_state(&self, context: &Arc<Context>) {
let cache_file = context.root.join("node_modules/.cache_mako/meta.json");
let cache = self.current_state.lock().unwrap();
fs::write(cache_file, serde_json::to_string(&*cache).unwrap()).unwrap();
}
fn write_current_cache_state(&self, context: &Arc<Context>) -> Result<(), Box<dyn std::error::Error>> {
let cache_file = context.root.join("node_modules/.cache_mako/meta.json");
let cache = self.current_state.lock().unwrap();
let cache_data = serde_json::to_string(&*cache)?;
fs::write(cache_file, cache_data)?;
Ok(())
}

crates/mako/src/plugins/ssu.rs Outdated Show resolved Hide resolved
Comment on lines +135 to +162
fn modify_config(&self, config: &mut Config, _root: &Path, _args: &Args) -> Result<()> {
for p in config.entry.values_mut() {
*p = PathBuf::from(format!("{SSU_ENTRY_PREFIX}{}", p.to_string_lossy()));
}

config.code_splitting = Some(CodeSplittingStrategy::Advanced(OptimizeChunkOptions {
min_size: 0,
groups: vec![
OptimizeChunkGroup {
name: "node_modules".to_string(),
allow_chunks: OptimizeAllowChunks::All,
min_chunks: 0,
min_size: 0,
max_size: usize::MAX,
priority: 10,
test: Regex::new(r"[/\\]node_modules[/\\]").ok(),
},
OptimizeChunkGroup {
name: "common".to_string(),
min_chunks: 0,
// always split, to avoid multi-instance risk
min_size: 1,
max_size: usize::MAX,
priority: 0,
..Default::default()
},
],
}));
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

optimize_hot_update_chunkget_optimize_chunk_options 方法中,都有检查 config.code_splitting 是否为 None 的逻辑。如果 config.code_splittingNone,这些方法将不会继续执行相关的优化逻辑。

然而,在 modify_config 方法中,config.code_splitting 被设置为一个特定的策略(CodeSplittingStrategy::Advanced),这意味着在调用 modify_config 后,config.code_splitting 不会是 None

因此,这种修改可能会影响非 SSU 模式下的配置,因为 config.code_splitting 被强制设置为一个特定的策略,而不是保持原来的状态。

需要注意的代码位置:

  • crates/mako/src/plugins/ssu.rs 中的 modify_config 方法。
Analysis chain

modify_config 方法中修改了配置以支持 SSU。请确保这些更改不会影响到非 SSU 模式下的构建配置。


根据 rg 的输出,config.code_splitting 在以下文件和位置被使用:

  1. crates/mako/src/generate/optimize_chunk.rs 中的 optimize_hot_update_chunk 方法。
  2. crates/mako/src/generate/optimize_chunk.rs 中的 get_optimize_chunk_options 方法。

我们需要进一步检查这些方法的上下文,以确定 modify_config 方法中的更改是否会影响非 SSU 模式下的配置。

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# 检查非 SSU 模式下的配置是否受到影响
rg --type rust 'config.code_splitting' --context 5

Length of output: 2825



Script:

#!/bin/bash
# 检查 optimize_hot_update_chunk 方法的上下文
rg --type rust 'fn optimize_hot_update_chunk' --context 10 crates/mako/src/generate/optimize_chunk.rs

# 检查 get_optimize_chunk_options 方法的上下文
rg --type rust 'fn get_optimize_chunk_options' --context 10 crates/mako/src/generate/optimize_chunk.rs

Length of output: 1831

crates/mako/src/dev/mod.rs Outdated Show resolved Hide resolved
@sorrycc sorrycc merged commit 6d787c7 into master May 31, 2024
8 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.

2 participants