Skip to content

Commit 902ceff

Browse files
authored
refactor: rspack module profiler (#11712)
* refactor: rspack module profiler * refactor: Option<Box<ModuleProfile>> to Option<ModuleProfile> * fix typo
1 parent 78c0e36 commit 902ceff

File tree

14 files changed

+72
-142
lines changed

14 files changed

+72
-142
lines changed

crates/node_binding/napi-binding.d.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,11 +1353,6 @@ export interface JsStatsLogging {
13531353
trace?: Array<string>
13541354
}
13551355

1356-
export interface JsStatsMillisecond {
1357-
secs: number
1358-
subsecMillis: number
1359-
}
1360-
13611356
export interface JsStatsModule {
13621357
commonAttributes: JsStatsModuleCommonAttributes
13631358
dependent?: boolean
@@ -1402,8 +1397,8 @@ export interface JsStatsModuleIssuer {
14021397
}
14031398

14041399
export interface JsStatsModuleProfile {
1405-
factory: JsStatsMillisecond
1406-
building: JsStatsMillisecond
1400+
factory: number
1401+
building: number
14071402
}
14081403

14091404
export interface JsStatsModuleReason {

crates/rspack_binding_api/src/stats.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -680,30 +680,17 @@ impl<'a> TryFrom<StatsModule<'a>> for JsStatsModule<'a> {
680680

681681
#[napi(object, object_from_js = false)]
682682
pub struct JsStatsModuleProfile {
683-
pub factory: JsStatsMillisecond,
684-
pub building: JsStatsMillisecond,
683+
// use f64 to make js side as a number type
684+
pub factory: f64,
685+
pub building: f64,
685686
}
686687

687688
impl From<rspack_core::StatsModuleProfile> for JsStatsModuleProfile {
688689
fn from(value: rspack_core::StatsModuleProfile) -> Self {
689690
Self {
690-
factory: value.factory.into(),
691-
building: value.building.into(),
692-
}
693-
}
694-
}
695-
696-
#[napi(object, object_from_js = false)]
697-
pub struct JsStatsMillisecond {
698-
pub secs: u32,
699-
pub subsec_millis: u32,
700-
}
701-
702-
impl From<rspack_core::StatsMillisecond> for JsStatsMillisecond {
703-
fn from(value: rspack_core::StatsMillisecond) -> Self {
704-
Self {
705-
secs: value.secs as u32,
706-
subsec_millis: value.subsec_millis,
691+
// The time is short and no data will be lost when converting from u64 to f64
692+
factory: value.factory as f64,
693+
building: value.building as f64,
707694
}
708695
}
709696
}

crates/rspack_core/src/compilation/make/graph_updater/repair/add.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct AddTask {
1414
pub module: Box<dyn Module>,
1515
pub module_graph_module: Box<ModuleGraphModule>,
1616
pub dependencies: Vec<BoxDependency>,
17-
pub current_profile: Option<Box<ModuleProfile>>,
17+
pub current_profile: Option<ModuleProfile>,
1818
pub from_unlazy: bool,
1919
}
2020

crates/rspack_core/src/compilation/make/graph_updater/repair/build.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct BuildTask {
1818
pub compiler_id: CompilerId,
1919
pub compilation_id: CompilationId,
2020
pub module: Box<dyn Module>,
21-
pub current_profile: Option<Box<ModuleProfile>>,
21+
pub current_profile: Option<ModuleProfile>,
2222
pub resolver_factory: Arc<ResolverFactory>,
2323
pub compiler_options: Arc<CompilerOptions>,
2424
pub plugin_driver: SharedPluginDriver,
@@ -38,12 +38,12 @@ impl Task<TaskContext> for BuildTask {
3838
compiler_options,
3939
resolver_factory,
4040
plugin_driver,
41-
current_profile,
41+
mut current_profile,
4242
mut module,
4343
fs,
4444
forwarded_ids,
4545
} = *self;
46-
if let Some(current_profile) = &current_profile {
46+
if let Some(current_profile) = &mut current_profile {
4747
current_profile.mark_building_start();
4848
}
4949

@@ -67,7 +67,7 @@ impl Task<TaskContext> for BuildTask {
6767
)
6868
.await;
6969

70-
if let Some(current_profile) = &current_profile {
70+
if let Some(current_profile) = &mut current_profile {
7171
current_profile.mark_building_end();
7272
}
7373

@@ -88,7 +88,7 @@ struct BuildResultTask {
8888
pub module: Box<dyn Module>,
8989
pub build_result: Box<BuildResult>,
9090
pub plugin_driver: SharedPluginDriver,
91-
pub current_profile: Option<Box<ModuleProfile>>,
91+
pub current_profile: Option<ModuleProfile>,
9292
pub forwarded_ids: ForwardedIdSet,
9393
}
9494

crates/rspack_core/src/compilation/make/graph_updater/repair/factorize.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub struct FactorizeTask {
2525
pub dependencies: Vec<BoxDependency>,
2626
pub resolve_options: Option<Arc<Resolve>>,
2727
pub options: Arc<CompilerOptions>,
28-
pub current_profile: Option<Box<ModuleProfile>>,
28+
pub current_profile: Option<ModuleProfile>,
2929
pub resolver_factory: Arc<ResolverFactory>,
3030
pub from_unlazy: bool,
3131
}
@@ -35,8 +35,8 @@ impl Task<TaskContext> for FactorizeTask {
3535
fn get_task_type(&self) -> TaskType {
3636
TaskType::Background
3737
}
38-
async fn background_run(self: Box<Self>) -> TaskResult<TaskContext> {
39-
if let Some(current_profile) = &self.current_profile {
38+
async fn background_run(mut self: Box<Self>) -> TaskResult<TaskContext> {
39+
if let Some(current_profile) = &mut self.current_profile {
4040
current_profile.mark_factory_start();
4141
}
4242
let dependency = &self.dependencies[0];
@@ -110,7 +110,7 @@ impl Task<TaskContext> for FactorizeTask {
110110
}
111111
};
112112

113-
if let Some(current_profile) = &self.current_profile {
113+
if let Some(current_profile) = &mut self.current_profile {
114114
current_profile.mark_factory_end();
115115
}
116116

@@ -145,7 +145,7 @@ pub struct FactorizeResultTask {
145145
/// Result will be available if [crate::ModuleFactory::create] returns `Ok`.
146146
pub factory_result: Option<ModuleFactoryResult>,
147147
pub dependencies: Vec<BoxDependency>,
148-
pub current_profile: Option<Box<ModuleProfile>>,
148+
pub current_profile: Option<ModuleProfile>,
149149
pub exports_info_related: ExportsInfoData,
150150
pub factorize_info: FactorizeInfo,
151151
pub from_unlazy: bool,

crates/rspack_core/src/compilation/make/graph_updater/repair/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ pub async fn repair(
4545
let dependency = module_graph
4646
.dependency_by_id(&dep_id)
4747
.expect("dependency not found");
48-
let current_profile = compilation
49-
.options
50-
.profile
51-
.then(Box::<ModuleProfile>::default);
48+
let current_profile = compilation.options.profile.then(ModuleProfile::default);
5249
Box::new(factorize::FactorizeTask {
5350
compiler_id: compilation.compiler_id(),
5451
compilation_id: compilation.id(),

crates/rspack_core/src/compilation/make/graph_updater/repair/process_dependencies.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl Task<TaskContext> for ProcessDependenciesTask {
7373
let current_profile = context
7474
.compiler_options
7575
.profile
76-
.then(Box::<ModuleProfile>::default);
76+
.then(ModuleProfile::default);
7777
let original_module_source = module_graph
7878
.module_by_identifier(&original_module_identifier)
7979
.and_then(|m| m.as_normal_module())

crates/rspack_core/src/compilation/make/module_executor/entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl Task<ExecutorTaskContext> for EntryTask {
7878
current_profile: origin_context
7979
.compiler_options
8080
.profile
81-
.then(Box::<ModuleProfile>::default),
81+
.then(ModuleProfile::default),
8282
resolver_factory: origin_context.resolver_factory.clone(),
8383
from_unlazy: false,
8484
})]));

crates/rspack_core/src/module_graph/module.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct ModuleGraphModule {
2323
pub post_order_index: Option<u32>,
2424
pub exports: ExportsInfo,
2525
#[cacheable(with=Skip)]
26-
pub profile: Option<Box<ModuleProfile>>,
26+
pub profile: Option<ModuleProfile>,
2727
pub depth: Option<usize>,
2828
pub optimization_bailout: Vec<String>,
2929
}
@@ -70,12 +70,12 @@ impl ModuleGraphModule {
7070
&self.outgoing_connections
7171
}
7272

73-
pub fn set_profile(&mut self, profile: Box<ModuleProfile>) {
73+
pub fn set_profile(&mut self, profile: ModuleProfile) {
7474
self.profile = Some(profile);
7575
}
7676

7777
pub fn profile(&self) -> Option<&ModuleProfile> {
78-
self.profile.as_deref()
78+
self.profile.as_ref()
7979
}
8080

8181
pub fn set_issuer_if_unset(&mut self, issuer: Option<ModuleIdentifier>) {
Lines changed: 40 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,34 @@
1-
use std::time::{Duration, Instant};
2-
3-
use once_cell::sync::OnceCell;
1+
use std::time::Instant;
42

53
#[derive(Debug, Default, Clone)]
6-
pub struct TimeRange {
7-
start: OnceCell<Instant>,
8-
end: OnceCell<Instant>,
4+
enum ProfileState {
5+
#[default]
6+
Pending,
7+
Started(Instant),
8+
// u64 is enough to store the time consumption
9+
Finish(u64),
910
}
1011

11-
impl TimeRange {
12-
pub fn with_value(start: Instant, end: Instant) -> Self {
13-
Self {
14-
start: OnceCell::with_value(start),
15-
end: OnceCell::with_value(end),
16-
}
12+
impl ProfileState {
13+
fn start(&mut self) {
14+
*self = Self::Started(Instant::now())
1715
}
1816

19-
pub fn duration(&self) -> Option<Duration> {
20-
if let Some(end) = self.end.get()
21-
&& let Some(start) = self.start.get()
22-
{
23-
Some(end.duration_since(*start))
24-
} else {
25-
None
17+
fn end(&mut self) {
18+
match self {
19+
Self::Started(i) => {
20+
let time = Instant::now().duration_since(*i);
21+
*self = Self::Finish(time.as_millis() as u64)
22+
}
23+
_ => panic!("Unable to end an unstarted profiler"),
2624
}
2725
}
28-
}
2926

30-
#[derive(Debug, Default, Clone)]
31-
pub struct ModulePhaseProfile {
32-
range: TimeRange,
33-
parallelism_factor: OnceCell<u16>,
34-
}
35-
36-
impl ModulePhaseProfile {
37-
pub fn duration(&self) -> Option<Duration> {
38-
self.range.duration()
39-
}
40-
41-
pub fn set_parallelism_factor(&self, factor: u16) {
42-
self
43-
.parallelism_factor
44-
.set(factor)
45-
.expect("should only call once");
27+
fn duration(&self) -> Option<u64> {
28+
match self {
29+
Self::Finish(time) => Some(*time),
30+
_ => None,
31+
}
4632
}
4733
}
4834

@@ -51,44 +37,32 @@ impl ModulePhaseProfile {
5137

5238
#[derive(Debug, Default, Clone)]
5339
pub struct ModuleProfile {
54-
pub factory: ModulePhaseProfile,
55-
pub building: ModulePhaseProfile,
40+
factory: ProfileState,
41+
building: ProfileState,
5642
}
5743

5844
impl ModuleProfile {
59-
pub fn mark_factory_start(&self) {
60-
self
61-
.factory
62-
.range
63-
.start
64-
.set(Instant::now())
65-
.expect("should only call once");
45+
pub fn mark_factory_start(&mut self) {
46+
self.factory.start();
47+
}
48+
49+
pub fn mark_factory_end(&mut self) {
50+
self.factory.end();
51+
}
52+
53+
pub fn mark_building_start(&mut self) {
54+
self.building.start();
6655
}
6756

68-
pub fn mark_factory_end(&self) {
69-
self
70-
.factory
71-
.range
72-
.end
73-
.set(Instant::now())
74-
.expect("should only call once");
57+
pub fn mark_building_end(&mut self) {
58+
self.building.end();
7559
}
7660

77-
pub fn mark_building_start(&self) {
78-
self
79-
.building
80-
.range
81-
.start
82-
.set(Instant::now())
83-
.expect("should only call once");
61+
pub fn factory_duration(&self) -> Option<u64> {
62+
self.factory.duration()
8463
}
8564

86-
pub fn mark_building_end(&self) {
87-
self
88-
.building
89-
.range
90-
.end
91-
.set(Instant::now())
92-
.expect("should only call once");
65+
pub fn building_duration(&self) -> Option<u64> {
66+
self.building.duration()
9367
}
9468
}

0 commit comments

Comments
 (0)