Skip to content

Commit

Permalink
feat: support chunkGroup.getModulePreOrderIndex (#8588)
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng authored Jan 8, 2025
1 parent f8e703c commit ce275d6
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 9 deletions.
2 changes: 2 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export declare class JsChunkGroup {
getParents(): JsChunkGroup[]
getRuntimeChunk(): JsChunk
getFiles(): Array<string>
getModulePreOrderIndex(module: JsModule): number | null
getModulePostOrderIndex(module: JsModule): number | null
}

export declare class JsCompilation {
Expand Down
39 changes: 30 additions & 9 deletions crates/rspack_binding_values/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rspack_core::{ChunkGroup, ChunkGroupUkey, Compilation, CompilationId};
use rspack_napi::OneShotRef;
use rustc_hash::FxHashMap as HashMap;

use crate::{JsChunkWrapper, JsModuleWrapper};
use crate::{JsChunkWrapper, JsModule, JsModuleWrapper};

#[napi]
pub struct JsChunkGroup {
Expand Down Expand Up @@ -89,15 +89,15 @@ impl JsChunkGroup {
impl JsChunkGroup {
#[napi]
pub fn is_initial(&self) -> napi::Result<bool> {
let (_, chunk_graph) = self.as_ref()?;
Ok(chunk_graph.is_initial())
let (_, chunk_group) = self.as_ref()?;
Ok(chunk_group.is_initial())
}

#[napi(ts_return_type = "JsChunkGroup[]")]
pub fn get_parents(&self) -> napi::Result<Vec<JsChunkGroupWrapper>> {
let (compilation, chunk_graph) = self.as_ref()?;
let (compilation, chunk_group) = self.as_ref()?;
Ok(
chunk_graph
chunk_group
.parents
.iter()
.map(|ukey| JsChunkGroupWrapper::new(*ukey, compilation))
Expand All @@ -107,16 +107,16 @@ impl JsChunkGroup {

#[napi(ts_return_type = "JsChunk")]
pub fn get_runtime_chunk(&self) -> napi::Result<JsChunkWrapper> {
let (compilation, chunk_graph) = self.as_ref()?;
let chunk_ukey = chunk_graph.get_runtime_chunk(&compilation.chunk_group_by_ukey);
let (compilation, chunk_group) = self.as_ref()?;
let chunk_ukey = chunk_group.get_runtime_chunk(&compilation.chunk_group_by_ukey);
Ok(JsChunkWrapper::new(chunk_ukey, compilation))
}

#[napi]
pub fn get_files(&self) -> napi::Result<Vec<&String>> {
let (compilation, chunk_graph) = self.as_ref()?;
let (compilation, chunk_group) = self.as_ref()?;
Ok(
chunk_graph
chunk_group
.chunks
.iter()
.filter_map(|chunk_ukey| {
Expand All @@ -129,6 +129,27 @@ impl JsChunkGroup {
.collect::<Vec<_>>(),
)
}

#[napi]
pub fn get_module_pre_order_index(&self, module: &JsModule) -> napi::Result<Option<u32>> {
let (_, chunk_group) = self.as_ref()?;
Ok(
chunk_group
.module_pre_order_index(&module.identifier)
.map(|v| v as u32),
)
}

#[napi]
pub fn get_module_post_order_index(&self, module: &JsModule) -> napi::Result<Option<u32>> {
let (_, chunk_group) = self.as_ref()?;

Ok(
chunk_group
.module_post_order_index(&module.identifier)
.map(|v| v as u32),
)
}
}

thread_local! {
Expand Down
8 changes: 8 additions & 0 deletions crates/rspack_core/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ impl ChunkGroup {
self.parents.iter()
}

pub fn module_pre_order_index(&self, module_identifier: &ModuleIdentifier) -> Option<usize> {
// A module could split into another ChunkGroup, which doesn't have the module_post_order_indices of the module
self
.module_pre_order_indices
.get(module_identifier)
.copied()
}

pub fn module_post_order_index(&self, module_identifier: &ModuleIdentifier) -> Option<usize> {
// A module could split into another ChunkGroup, which doesn't have the module_post_order_indices of the module
self
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it("should compile", function (done) {
done()
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/** @type {import("@rspack/core").Configuration} */
module.exports = {
target: "node",
entry: {
'main': './index.js'
},
plugins: [
{
/**@param {import("@rspack/core").Compiler} compiler */
apply(compiler) {
compiler.hooks.thisCompilation.tap('test', (compilation) => {
compilation.hooks.afterSeal.tap('test', () => {
let entrypoint = compilation.entrypoints.get('main')

compilation.chunkGraph.getChunkModules(entrypoint.chunks[0]).forEach((m) => {
expect(entrypoint.getModulePreOrderIndex(m)).toBeDefined();
expect(entrypoint.getModulePostOrderIndex(m)).toBeDefined();
})
})
})
}
}
]
};
4 changes: 4 additions & 0 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ export class ChunkGroup {
// (undocumented)
getFiles(): ReadonlyArray<string>;
// (undocumented)
getModulePostOrderIndex(module: Module): number | null;
// (undocumented)
getModulePreOrderIndex(module: Module): number | null;
// (undocumented)
getParents(): ReadonlyArray<ChunkGroup>;
// (undocumented)
readonly index?: number;
Expand Down
8 changes: 8 additions & 0 deletions packages/rspack/src/ChunkGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ export class ChunkGroup {
isInitial(): boolean {
return this.#inner.isInitial();
}

getModulePreOrderIndex(module: Module) {
return this.#inner.getModulePreOrderIndex(Module.__to_binding(module));
}

getModulePostOrderIndex(module: Module) {
return this.#inner.getModulePostOrderIndex(Module.__to_binding(module));
}
}

interface ChunkGroupOrigin {
Expand Down

2 comments on commit ce275d6

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on ce275d6 Jan 8, 2025

Choose a reason for hiding this comment

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

📝 Ecosystem CI detail: Open

suite result
modernjs ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
rsdoctor ✅ success
examples ✅ success
devserver ✅ success
nuxt ✅ success

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented on ce275d6 Jan 8, 2025

Choose a reason for hiding this comment

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

📝 Benchmark detail: Open

Name Base (2025-01-08 0dbc9ac) Current Change
10000_big_production-mode_disable-minimize + exec 37.8 s ± 463 ms 37.8 s ± 463 ms +0.21 %
10000_development-mode + exec 1.86 s ± 18 ms 1.81 s ± 25 ms -2.47 %
10000_development-mode_hmr + exec 675 ms ± 5.4 ms 670 ms ± 3.6 ms -0.67 %
10000_production-mode + exec 2.5 s ± 38 ms 2.41 s ± 42 ms -3.61 %
arco-pro_development-mode + exec 1.78 s ± 148 ms 1.72 s ± 64 ms -3.38 %
arco-pro_development-mode_hmr + exec 376 ms ± 0.77 ms 377 ms ± 1.6 ms +0.09 %
arco-pro_production-mode + exec 3.63 s ± 72 ms 3.49 s ± 63 ms -3.80 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.65 s ± 113 ms 3.56 s ± 99 ms -2.27 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.6 s ± 53 ms 3.57 s ± 71 ms -0.98 %
large-dyn-imports_development-mode + exec 2.08 s ± 16 ms 2.05 s ± 28 ms -1.38 %
large-dyn-imports_production-mode + exec 2.11 s ± 41 ms 2.06 s ± 39 ms -2.39 %
threejs_development-mode_10x + exec 1.5 s ± 20 ms 1.49 s ± 20 ms -0.89 %
threejs_development-mode_10x_hmr + exec 779 ms ± 27 ms 786 ms ± 26 ms +0.92 %
threejs_production-mode_10x + exec 5.32 s ± 29 ms 5.35 s ± 127 ms +0.56 %
10000_big_production-mode_disable-minimize + rss memory 9502 MiB ± 255 MiB 9546 MiB ± 32.7 MiB +0.46 %
10000_development-mode + rss memory 642 MiB ± 7.83 MiB 722 MiB ± 36.6 MiB +12.38 %
10000_development-mode_hmr + rss memory 1398 MiB ± 340 MiB 1496 MiB ± 320 MiB +7.01 %
10000_production-mode + rss memory 626 MiB ± 26.9 MiB 716 MiB ± 25.5 MiB +14.34 %
arco-pro_development-mode + rss memory 586 MiB ± 36.2 MiB 636 MiB ± 36.9 MiB +8.57 %
arco-pro_development-mode_hmr + rss memory 617 MiB ± 18.3 MiB 661 MiB ± 54 MiB +7.09 %
arco-pro_production-mode + rss memory 735 MiB ± 36.7 MiB 771 MiB ± 84.6 MiB +4.84 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 751 MiB ± 86.5 MiB 765 MiB ± 23.7 MiB +1.88 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 738 MiB ± 41.8 MiB 774 MiB ± 44.5 MiB +4.98 %
large-dyn-imports_development-mode + rss memory 666 MiB ± 12.4 MiB 680 MiB ± 7.18 MiB +2.08 %
large-dyn-imports_production-mode + rss memory 559 MiB ± 9 MiB 581 MiB ± 6.27 MiB +4.01 %
threejs_development-mode_10x + rss memory 637 MiB ± 33.9 MiB 667 MiB ± 27.4 MiB +4.64 %
threejs_development-mode_10x_hmr + rss memory 1103 MiB ± 187 MiB 1132 MiB ± 290 MiB +2.65 %
threejs_production-mode_10x + rss memory 918 MiB ± 47 MiB 954 MiB ± 40.2 MiB +3.96 %

Please sign in to comment.