Skip to content

Commit 9d0e5be

Browse files
committed
feat: introduce allocative
1 parent 02ab3a4 commit 9d0e5be

File tree

42 files changed

+248
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+248
-4
lines changed

Cargo.lock

Lines changed: 45 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ wasmparser = { version = "0.222.0", default-features = false }
113113
winnow = { version = "0.7.12", default-features = false, features = ["std", "simd"] }
114114
xxhash-rust = { version = "0.8.14", default-features = false }
115115

116+
allocative = { package = "rspack-allocative", version = "0.3.5", default-features = false, features = [
117+
"camino",
118+
"dashmap",
119+
"hashbrown",
120+
"indexmap",
121+
"tokio",
122+
"once_cell",
123+
"ustr",
124+
"atomic_refcell",
125+
] }
126+
116127
# Pinned
117128
napi = { version = "3.1.6", default-features = false }
118129
napi-build = { version = "2.2.3", default-features = false }
@@ -418,6 +429,10 @@ unused_import_braces = "warn"
418429
unused_lifetimes = "warn"
419430
unused_macro_rules = "warn"
420431

432+
[workspace.lints.rust.unexpected_cfgs]
433+
level = "warn"
434+
check-cfg = ['cfg(allocative)']
435+
421436
[workspace.lints.clippy]
422437
cargo_common_metadata = "allow"
423438
empty_docs = "allow" # there are some false positives inside biome_wasm

crates/node_binding/scripts/build.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ async function build() {
7474
features.push("sftrace-setup");
7575
rustflags.push("-Zinstrument-xray=always");
7676
}
77+
if (process.env.ALLOCATIVE) {
78+
rustflags.push("--cfg=allocative");
79+
}
7780
if (values.profile === "release") {
7881
features.push("info-level");
7982
}

crates/rspack_cacheable/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ version.workspace = true
1010
noop = []
1111

1212
[dependencies]
13+
allocative = { workspace = true }
1314
camino = { workspace = true }
1415
dashmap = { workspace = true }
1516
hashlink = { workspace = true }
@@ -27,3 +28,7 @@ smol_str = { workspace = true }
2728
swc_core = { workspace = true, features = ["ecma_ast"] }
2829
ustr = { workspace = true }
2930
xxhash-rust = { workspace = true, features = ["const_xxh64"] }
31+
32+
[lints.rust.unexpected_cfgs]
33+
level = "warn"
34+
check-cfg = ['cfg(allocative)']

crates/rspack_cacheable/src/with/as_ref_str.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ use std::sync::{Arc, LazyLock};
6969

7070
use dashmap::DashSet;
7171

72+
#[cfg_attr(allocative, allocative::root)]
7273
pub static CACHED_ARC_STR: LazyLock<DashSet<Arc<str>>> = LazyLock::new(Default::default);
74+
7375
impl AsRefStrConverter for Arc<str> {
7476
fn as_str(&self) -> &str {
7577
self.as_ref()

crates/rspack_collections/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,9 @@ rayon = { workspace = true }
1616
rspack_cacheable = { workspace = true }
1717
serde = { workspace = true, features = ["derive"] }
1818
ustr = { workspace = true, features = ["serde"] }
19+
allocative = { workspace = true }
20+
21+
[lints.rust.unexpected_cfgs]
22+
level = "warn"
23+
check-cfg = ['cfg(allocative)']
24+

crates/rspack_collections/src/identifier.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub type IdentifierLinkedSet = LinkedHashSet<Identifier, BuildHasherDefault<Iden
3636

3737
#[cacheable(hashable)]
3838
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize)]
39+
#[cfg_attr(allocative, derive(allocative::Allocative))]
3940
pub struct Identifier(#[cacheable(with=AsPreset)] Ustr);
4041

4142
impl Deref for Identifier {

crates/rspack_collections/src/ukey.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub trait ItemUkey {
3535
/// Ukey stands for Unique key
3636
#[rspack_cacheable::cacheable]
3737
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
38+
#[cfg_attr(allocative, derive(allocative::Allocative))]
3839
pub struct Ukey(u32);
3940

4041
impl Ukey {

crates/rspack_core/src/compiler/compilation.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use rspack_hook::define_hook;
2727
use rspack_paths::{ArcPath, ArcPathIndexSet, ArcPathSet};
2828
use rspack_sources::{BoxSource, CachedSource, SourceExt};
2929
use rspack_tasks::CompilerContext;
30+
#[cfg(allocative)]
31+
use rspack_util::allocative;
3032
use rspack_util::{itoa, tracing_preset::TRACING_BENCH_TARGET};
3133
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet, FxHasher};
3234
use tracing::instrument;
@@ -132,6 +134,7 @@ pub struct CompilationHooks {
132134
}
133135

134136
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd)]
137+
#[cfg_attr(allocative, derive(allocative::Allocative))]
135138
pub struct CompilationId(pub u32);
136139

137140
impl CompilationId {

crates/rspack_core/src/compiler/rebuild.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,32 @@ impl Compiler {
190190
self.compile_done().await?;
191191
self.cache.after_compile(&self.compilation).await;
192192

193+
#[cfg(allocative)]
194+
{
195+
use std::{
196+
path::PathBuf,
197+
sync::{
198+
LazyLock,
199+
atomic::{self, AtomicUsize},
200+
},
201+
};
202+
203+
use rspack_util::allocative;
204+
205+
static ENABLE: LazyLock<Option<PathBuf>> =
206+
LazyLock::new(|| std::env::var_os("RSPACK_ALLOCATIVE_DIR").map(Into::into));
207+
static COUNT: AtomicUsize = AtomicUsize::new(0);
208+
209+
if let Some(dir) = ENABLE.as_deref() {
210+
let mut builder = allocative::FlameGraphBuilder::default();
211+
builder.visit_global_roots();
212+
let buf = builder.finish_and_write_flame_graph();
213+
let count = COUNT.fetch_add(1, atomic::Ordering::Relaxed);
214+
let path = dir.join(format!("allocative.{}.flamegraph", count));
215+
std::fs::write(path, buf).expect("allocative write failed");
216+
}
217+
}
218+
193219
Ok(())
194220
}
195221
}

0 commit comments

Comments
 (0)