Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ wasmparser = { version = "0.222.0", default-features = false }
winnow = { version = "0.7.12", default-features = false, features = ["std", "simd"] }
xxhash-rust = { version = "0.8.14", default-features = false }

allocative = { package = "rspack-allocative", version = "0.3.5", default-features = false, features = [
"camino",
"dashmap",
"hashbrown",
"indexmap",
"tokio",
"once_cell",
"ustr",
"atomic_refcell",
] }

# Pinned
napi = { version = "3.1.6", default-features = false }
napi-build = { version = "2.2.3", default-features = false }
Expand Down Expand Up @@ -418,6 +429,10 @@ unused_import_braces = "warn"
unused_lifetimes = "warn"
unused_macro_rules = "warn"

[workspace.lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = ['cfg(allocative)']

[workspace.lints.clippy]
cargo_common_metadata = "allow"
empty_docs = "allow" # there are some false positives inside biome_wasm
Expand Down
1 change: 1 addition & 0 deletions crates/node_binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ debug_tool = ["rspack_binding_api/debug_tool"]
info-level = ["tracing/release_max_level_info"]
plugin = ["rspack_binding_api/plugin"]
sftrace-setup = ["rspack_binding_api/sftrace-setup"]
allocative = ["rspack_binding_api/allocative"]

[package.metadata.cargo-shear]
# Adding napi-derive as a dependency to workaround an issue where `dts` will no longer work without it.
Expand Down
4 changes: 4 additions & 0 deletions crates/node_binding/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ async function build() {
features.push("sftrace-setup");
rustflags.push("-Zinstrument-xray=always");
}
if (process.env.ALLOCATIVE) {
features.push("allocative");
rustflags.push("--cfg=allocative");
}
if (values.profile === "release") {
features.push("info-level");
}
Expand Down
11 changes: 9 additions & 2 deletions crates/rspack_binding_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ color-backtrace = ["dep:color-backtrace"]
debug_tool = ["rspack_core/debug_tool"]
plugin = ["rspack_loader_swc/plugin", "rspack_util/plugin"]
sftrace-setup = ["dep:sftrace-setup", "rspack_allocator/sftrace-setup"]
allocative = [
"rspack_util/allocative",
"rspack_collections/allocative",
"rspack_cacheable/allocative",
"rspack_tracing_perfetto/allocative",
]

[dependencies]
anyhow = { workspace = true }
Expand All @@ -41,7 +47,8 @@ rspack_tasks = { workspace = true }
rspack_util = { workspace = true }
rspack_workspace = { workspace = true }

rspack_tracing = { workspace = true }
rspack_tracing = { workspace = true }
rspack_tracing_perfetto = { workspace = true }

async-trait = { workspace = true }
cow-utils = { workspace = true }
Expand Down Expand Up @@ -120,7 +127,7 @@ tokio = { workspace = true, features = ["rt", "
ustr = { workspace = true }

[package.metadata.cargo-shear]
ignored = ["parking_lot"]
ignored = ["parking_lot", "rspack_tracing_perfetto"]

[target.'cfg(target_family = "wasm")'.dependencies]
# Pin parking_lot version to the same version within the workspace
Expand Down
5 changes: 5 additions & 0 deletions crates/rspack_cacheable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ version.workspace = true
noop = []

[dependencies]
allocative = { workspace = true, optional = true }
camino = { workspace = true }
dashmap = { workspace = true }
hashlink = { workspace = true }
Expand All @@ -27,3 +28,7 @@ smol_str = { workspace = true }
swc_core = { workspace = true, features = ["ecma_ast"] }
ustr = { workspace = true }
xxhash-rust = { workspace = true, features = ["const_xxh64"] }

[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = ['cfg(allocative)']
2 changes: 2 additions & 0 deletions crates/rspack_cacheable/src/with/as_ref_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ use std::sync::{Arc, LazyLock};

use dashmap::DashSet;

#[cfg_attr(allocative, allocative::root)]
pub static CACHED_ARC_STR: LazyLock<DashSet<Arc<str>>> = LazyLock::new(Default::default);

impl AsRefStrConverter for Arc<str> {
fn as_str(&self) -> &str {
self.as_ref()
Expand Down
6 changes: 6 additions & 0 deletions crates/rspack_collections/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ rayon = { workspace = true }
rspack_cacheable = { workspace = true }
serde = { workspace = true, features = ["derive"] }
ustr = { workspace = true, features = ["serde"] }
allocative = { workspace = true, optional = true }

[lints.rust.unexpected_cfgs]
level = "warn"
check-cfg = ['cfg(allocative)']

1 change: 1 addition & 0 deletions crates/rspack_collections/src/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub type IdentifierLinkedSet = LinkedHashSet<Identifier, BuildHasherDefault<Iden

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

impl Deref for Identifier {
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_collections/src/ukey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub trait ItemUkey {
/// Ukey stands for Unique key
#[rspack_cacheable::cacheable]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[cfg_attr(allocative, derive(allocative::Allocative))]
pub struct Ukey(u32);

impl Ukey {
Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ use rspack_hook::define_hook;
use rspack_paths::{ArcPath, ArcPathIndexSet, ArcPathSet};
use rspack_sources::{BoxSource, CachedSource, SourceExt};
use rspack_tasks::CompilerContext;
#[cfg(allocative)]
use rspack_util::allocative;
use rspack_util::{itoa, tracing_preset::TRACING_BENCH_TARGET};
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet, FxHasher};
use tracing::instrument;
Expand Down Expand Up @@ -132,6 +134,7 @@ pub struct CompilationHooks {
}

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

impl CompilationId {
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ impl Compiler {
self.old_cache.begin_idle();
self.compile_done().await?;
self.cache.after_compile(&self.compilation).await;

#[cfg(allocative)]
crate::utils::snapshot_allocative("build");

Ok(())
}

Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_core/src/compiler/rebuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ impl Compiler {
self.compile_done().await?;
self.cache.after_compile(&self.compilation).await;

#[cfg(allocative)]
crate::utils::snapshot_allocative("rebuild");

Ok(())
}
}
Expand Down
5 changes: 5 additions & 0 deletions crates/rspack_core/src/debug_info.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::{fmt::Display, sync::Mutex};

#[cfg(allocative)]
use rspack_util::allocative;

/// Debug info used when programs panics
/// Only works with #[cfg(debug_assertions)]
#[cfg_attr(allocative, derive(allocative::Allocative))]
pub struct DebugInfo {
/// The base directory. See [options.context](https://webpack.js.org/configuration/entry-context/#context)
pub(crate) context: Option<String>,
Expand Down Expand Up @@ -40,4 +44,5 @@ impl Display for DebugInfo {
}
}

#[cfg_attr(allocative, allocative::root)]
pub static DEBUG_INFO: Mutex<DebugInfo> = Mutex::new(DebugInfo::new());
3 changes: 3 additions & 0 deletions crates/rspack_core/src/options/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use rspack_hash::RspackHash;
pub use rspack_hash::{HashDigest, HashFunction, HashSalt};
use rspack_macros::MergeFrom;
use rspack_paths::Utf8PathBuf;
#[cfg(allocative)]
use rspack_util::allocative;

use super::CleanOptions;
use crate::{Chunk, ChunkGroupByUkey, ChunkKind, Compilation, Filename};
Expand Down Expand Up @@ -192,6 +194,7 @@ impl From<&str> for WasmLoadingType {
}

#[derive(Debug, Clone)]
#[cfg_attr(allocative, derive(allocative::Allocative))]
pub enum CrossOriginLoading {
Disable,
Enable(String),
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ use rspack_cacheable::{
cacheable,
with::{AsRefStr, AsVec},
};
#[cfg(allocative)]
use rspack_util::allocative;
use rustc_hash::FxHashMap;
use ustr::{Ustr, UstrSet};

use crate::{EntryOptions, EntryRuntime};

#[cacheable]
#[derive(Debug, Default, Clone)]
#[cfg_attr(allocative, derive(allocative::Allocative))]
pub struct RuntimeSpec {
#[cacheable(with=AsVec<AsRefStr>)]
inner: UstrSet,
Expand Down Expand Up @@ -157,6 +160,7 @@ pub fn is_runtime_equal(a: &RuntimeSpec, b: &RuntimeSpec) -> bool {
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(allocative, derive(allocative::Allocative))]
pub enum RuntimeCondition {
Boolean(bool),
Spec(RuntimeSpec),
Expand Down
3 changes: 3 additions & 0 deletions crates/rspack_core/src/ukey.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::sync::atomic::AtomicU32;

use rspack_collections::{Ukey, impl_item_ukey};
#[cfg(allocative)]
use rspack_util::allocative;

use crate::{Chunk, ChunkGroup};

Expand Down Expand Up @@ -42,6 +44,7 @@ impl From<u32> for ChunkUkey {
static NEXT_CHUNK_GROUP_UKEY: AtomicU32 = AtomicU32::new(0);

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(allocative, derive(allocative::Allocative))]
pub struct ChunkGroupUkey(Ukey, std::marker::PhantomData<ChunkGroup>);

impl_item_ukey!(ChunkGroupUkey);
Expand Down
32 changes: 32 additions & 0 deletions crates/rspack_core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,35 @@ pub fn compare_chunks_with_graph(
.collect();
compare_module_iterables(&modules_a, &modules_b)
}

#[cfg(allocative)]
pub fn snapshot_allocative(name: &str) {
use std::{
path::PathBuf,
sync::{
LazyLock,
atomic::{self, AtomicUsize},
},
};

use rspack_util::allocative;

static ENABLE: LazyLock<Option<PathBuf>> = LazyLock::new(|| {
std::env::var_os("RSPACK_ALLOCATIVE_DIR")
.map(|dir| {
let _ = std::fs::create_dir_all(&dir);
dir
})
.map(Into::into)
});
static COUNT: AtomicUsize = AtomicUsize::new(0);

if let Some(dir) = ENABLE.as_deref() {
let mut builder = allocative::FlameGraphBuilder::default();
builder.visit_global_roots();
let buf = builder.finish_and_write_flame_graph();
let count = COUNT.fetch_add(1, atomic::Ordering::Relaxed);
let path = dir.join(format!("{}-{}.allocative", count, name));
std::fs::write(path, buf).expect("allocative write failed");
}
}
Loading
Loading