Skip to content

Commit d437c3f

Browse files
mablrgrandizzy
andauthored
feat(foundry-cli::utils): add support for mimalloc and tracy-allocator (#10545)
- Introduced `mimalloc` as an optional allocator. - Added `tracy-allocator` support for profiling memory allocations. - Updated global allocator logic to conditionally use `jemalloc` (default) or `mimalloc`. - Modified `anvil`, `cast`, `chisel` and `forge` crates to include new features. These new features are inspired from Reth and Solar projects. Co-authored-by: grandizzy <[email protected]>
1 parent caab7d0 commit d437c3f

File tree

8 files changed

+66
-5
lines changed

8 files changed

+66
-5
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ async-trait = "0.1"
268268
derive_more = { version = "2.0", features = ["full"] }
269269
thiserror = "2"
270270

271+
# allocators
272+
mimalloc = "0.1"
273+
tikv-jemallocator = "0.6"
274+
tracy-client = "0.18"
275+
271276
# misc
272277
auto_impl = "1"
273278
aws-config = { version = "1", default-features = true }
@@ -318,7 +323,6 @@ soldeer-commands = "=0.5.4"
318323
soldeer-core = { version = "=0.5.4", features = ["serde"] }
319324
strum = "0.27"
320325
tempfile = "3.13"
321-
tikv-jemallocator = "0.6"
322326
tokio = "1"
323327
toml = "0.8"
324328
tower = "0.5"

crates/anvil/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ op-alloy-rpc-types.workspace = true
117117
default = ["cli", "jemalloc"]
118118
asm-keccak = ["alloy-primitives/asm-keccak"]
119119
jemalloc = ["foundry-cli/jemalloc"]
120+
mimalloc = ["foundry-cli/mimalloc"]
121+
tracy-allocator = ["foundry-cli/tracy-allocator"]
120122
cli = ["tokio/full", "cmd"]
121123
cmd = [
122124
"clap",

crates/cast/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ foundry-test-utils.workspace = true
9595
default = ["jemalloc"]
9696
asm-keccak = ["alloy-primitives/asm-keccak"]
9797
jemalloc = ["foundry-cli/jemalloc"]
98+
mimalloc = ["foundry-cli/mimalloc"]
99+
tracy-allocator = ["foundry-cli/tracy-allocator"]
98100
aws-kms = ["foundry-wallets/aws-kms", "dep:aws-sdk-kms"]
99101
gcp-kms = ["foundry-wallets/gcp-kms", "dep:gcloud-sdk"]
100102
isolate-by-default = ["foundry-config/isolate-by-default"]

crates/chisel/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,5 @@ tracing-subscriber.workspace = true
6464
default = ["jemalloc"]
6565
asm-keccak = ["alloy-primitives/asm-keccak"]
6666
jemalloc = ["foundry-cli/jemalloc"]
67+
mimalloc = ["foundry-cli/mimalloc"]
68+
tracy-allocator = ["foundry-cli/tracy-allocator"]

crates/cli/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ eyre.workspace = true
3939
futures.workspace = true
4040
indicatif.workspace = true
4141
itertools.workspace = true
42+
mimalloc = { workspace = true, optional = true }
4243
rayon.workspace = true
4344
regex = { workspace = true, default-features = false }
4445
serde_json.workspace = true
@@ -48,6 +49,7 @@ strum = { workspace = true, features = ["derive"] }
4849
tokio = { workspace = true, features = ["macros"] }
4950
tracing-subscriber = { workspace = true, features = ["registry", "env-filter"] }
5051
tracing.workspace = true
52+
tracy-client = { workspace = true, optional = true, features = ["demangle"] }
5153
yansi.workspace = true
5254
rustls = { workspace = true, features = ["ring"] }
5355

@@ -61,4 +63,6 @@ tikv-jemallocator = { workspace = true, optional = true }
6163

6264
[features]
6365
tracy = ["dep:tracing-tracy"]
66+
tracy-allocator = ["dep:tracy-client", "tracy"]
6467
jemalloc = ["dep:tikv-jemallocator"]
68+
mimalloc = ["dep:mimalloc"]

crates/cli/src/utils/allocator.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
//! Abstract global allocator implementation.
22
3-
// If jemalloc feature is enabled on Unix systems, use jemalloc as the global allocator.
4-
// Otherwise, explicitly use the system allocator.
3+
#[cfg(feature = "mimalloc")]
4+
use mimalloc as _;
5+
#[cfg(all(feature = "jemalloc", unix))]
6+
use tikv_jemallocator as _;
7+
8+
// If neither jemalloc nor mimalloc are enabled, use explicitly the system allocator.
9+
// By default jemalloc is enabled on Unix systems.
510
cfg_if::cfg_if! {
611
if #[cfg(all(feature = "jemalloc", unix))] {
712
type AllocatorInner = tikv_jemallocator::Jemalloc;
13+
} else if #[cfg(feature = "mimalloc")] {
14+
type AllocatorInner = mimalloc::MiMalloc;
815
} else {
916
type AllocatorInner = std::alloc::System;
1017
}
1118
}
1219

13-
pub type Allocator = AllocatorInner;
20+
// Wrap the allocator if the `tracy-allocator` feature is enabled.
21+
cfg_if::cfg_if! {
22+
if #[cfg(feature = "tracy-allocator")] {
23+
type AllocatorWrapper = tracy_client::ProfiledAllocator<AllocatorInner>;
24+
tracy_client::register_demangler!();
25+
const fn new_allocator_wrapper() -> AllocatorWrapper {
26+
AllocatorWrapper::new(AllocatorInner {}, 100)
27+
}
28+
} else {
29+
type AllocatorWrapper = AllocatorInner;
30+
const fn new_allocator_wrapper() -> AllocatorWrapper {
31+
AllocatorInner {}
32+
}
33+
}
34+
}
35+
36+
pub type Allocator = AllocatorWrapper;
1437

1538
/// Creates a new [allocator][Allocator].
1639
pub const fn new_allocator() -> Allocator {
17-
AllocatorInner {}
40+
new_allocator_wrapper()
1841
}

crates/forge/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ alloy-signer-local.workspace = true
116116
default = ["jemalloc"]
117117
asm-keccak = ["alloy-primitives/asm-keccak"]
118118
jemalloc = ["foundry-cli/jemalloc"]
119+
mimalloc = ["foundry-cli/mimalloc"]
120+
tracy-allocator = ["foundry-cli/tracy-allocator"]
119121
aws-kms = ["foundry-wallets/aws-kms"]
120122
gcp-kms = ["foundry-wallets/gcp-kms"]
121123
isolate-by-default = ["foundry-config/isolate-by-default"]

0 commit comments

Comments
 (0)