Skip to content

Commit f37b4f0

Browse files
authored
Refactor: abstract global allocator in foundry-cli to be used across crates (#10523)
Refactor: abstract global allocator in `foundry-cli` to be used cross crates - Conditional allocator type selection with cfg-if macro - Explicit use of `std::alloc::System` if feature "jemalloc" is not enabled - Linking "jemalloc" feature in all crates to "foundry-cli/jemalloc"
1 parent 297d4e2 commit f37b4f0

File tree

12 files changed

+36
-28
lines changed

12 files changed

+36
-28
lines changed

Cargo.lock

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

crates/anvil/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ ctrlc = { version = "3", optional = true }
9898
fdlimit = { version = "0.3", optional = true }
9999
clap_complete_fig = "4"
100100

101-
[target.'cfg(unix)'.dependencies]
102-
tikv-jemallocator = { workspace = true, optional = true }
103-
104101
[dev-dependencies]
105102
alloy-provider = { workspace = true, features = ["txpool-api"] }
106103
alloy-pubsub.workspace = true
@@ -112,7 +109,7 @@ op-alloy-rpc-types.workspace = true
112109
[features]
113110
default = ["cli", "jemalloc"]
114111
asm-keccak = ["alloy-primitives/asm-keccak"]
115-
jemalloc = ["dep:tikv-jemallocator"]
112+
jemalloc = ["foundry-cli/jemalloc"]
116113
cli = ["tokio/full", "cmd"]
117114
cmd = [
118115
"clap",

crates/anvil/bin/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
33
use anvil::args::run;
44

5-
#[cfg(all(feature = "jemalloc", unix))]
65
#[global_allocator]
7-
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
6+
static ALLOC: foundry_cli::utils::Allocator = foundry_cli::utils::new_allocator();
87

98
fn main() {
109
if let Err(err) = run() {

crates/cast/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,14 @@ tracing.workspace = true
8484
yansi.workspace = true
8585
evmole.workspace = true
8686

87-
[target.'cfg(unix)'.dependencies]
88-
tikv-jemallocator = { workspace = true, optional = true }
89-
9087
[dev-dependencies]
9188
anvil.workspace = true
9289
foundry-test-utils.workspace = true
9390

9491
[features]
9592
default = ["jemalloc"]
9693
asm-keccak = ["alloy-primitives/asm-keccak"]
97-
jemalloc = ["dep:tikv-jemallocator"]
94+
jemalloc = ["foundry-cli/jemalloc"]
9895
aws-kms = ["foundry-wallets/aws-kms", "dep:aws-sdk-kms"]
9996
gcp-kms = ["foundry-wallets/gcp-kms", "dep:gcloud-sdk"]
10097
isolate-by-default = ["foundry-config/isolate-by-default"]

crates/cast/bin/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
44
use cast::args::run;
55

6-
#[cfg(all(feature = "jemalloc", unix))]
76
#[global_allocator]
8-
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
7+
static ALLOC: foundry_cli::utils::Allocator = foundry_cli::utils::new_allocator();
98

109
fn main() {
1110
if let Err(err) = run() {

crates/chisel/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,10 @@ yansi.workspace = true
5757
tracing.workspace = true
5858
walkdir.workspace = true
5959

60-
[target.'cfg(unix)'.dependencies]
61-
tikv-jemallocator = { workspace = true, optional = true }
62-
6360
[dev-dependencies]
6461
tracing-subscriber.workspace = true
6562

6663
[features]
6764
default = ["jemalloc"]
6865
asm-keccak = ["alloy-primitives/asm-keccak"]
69-
jemalloc = ["dep:tikv-jemallocator"]
66+
jemalloc = ["foundry-cli/jemalloc"]

crates/chisel/bin/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
33
use chisel::args::run;
44

5-
#[cfg(all(feature = "jemalloc", unix))]
65
#[global_allocator]
7-
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
6+
static ALLOC: foundry_cli::utils::Allocator = foundry_cli::utils::new_allocator();
87

98
fn main() {
109
if let Err(err) = run() {

crates/cli/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ alloy-provider.workspace = true
3131
alloy-rlp.workspace = true
3232
alloy-chains.workspace = true
3333

34+
cfg-if = "1.0"
3435
clap = { version = "4", features = ["derive", "env", "unicode", "wrap_help"] }
3536
color-eyre.workspace = true
3637
dotenvy = "0.15"
@@ -55,5 +56,9 @@ tracing-tracy = { version = "0.11", optional = true }
5556
[dev-dependencies]
5657
tempfile.workspace = true
5758

59+
[target.'cfg(unix)'.dependencies]
60+
tikv-jemallocator = { workspace = true, optional = true }
61+
5862
[features]
5963
tracy = ["dep:tracing-tracy"]
64+
jemalloc = ["dep:tikv-jemallocator"]

crates/cli/src/utils/allocator.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! Abstract global allocator implementation.
2+
3+
// If jemalloc feature is enabled on Unix systems, use jemalloc as the global allocator.
4+
// Otherwise, explicitly use the system allocator.
5+
cfg_if::cfg_if! {
6+
if #[cfg(all(feature = "jemalloc", unix))] {
7+
type AllocatorInner = tikv_jemallocator::Jemalloc;
8+
} else {
9+
type AllocatorInner = std::alloc::System;
10+
}
11+
}
12+
13+
pub type Allocator = AllocatorInner;
14+
15+
/// Creates a new [allocator][Allocator].
16+
pub const fn new_allocator() -> Allocator {
17+
AllocatorInner {}
18+
}

crates/cli/src/utils/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ pub use suggestions::*;
2626
mod abi;
2727
pub use abi::*;
2828

29+
mod allocator;
30+
pub use allocator::*;
31+
2932
// reexport all `foundry_config::utils`
3033
#[doc(hidden)]
3134
pub use foundry_config::utils::*;

crates/forge/Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ opener = "0.7"
9393
soldeer-commands.workspace = true
9494
quick-junit = "0.5.0"
9595

96-
[target.'cfg(unix)'.dependencies]
97-
tikv-jemallocator = { workspace = true, optional = true }
98-
9996
[dev-dependencies]
10097
anvil.workspace = true
10198
foundry-test-utils.workspace = true
@@ -115,7 +112,7 @@ alloy-signer-local.workspace = true
115112
[features]
116113
default = ["jemalloc"]
117114
asm-keccak = ["alloy-primitives/asm-keccak"]
118-
jemalloc = ["dep:tikv-jemallocator"]
115+
jemalloc = ["foundry-cli/jemalloc"]
119116
aws-kms = ["foundry-wallets/aws-kms"]
120117
gcp-kms = ["foundry-wallets/gcp-kms"]
121118
isolate-by-default = ["foundry-config/isolate-by-default"]

crates/forge/bin/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
44
use forge::args::run;
55

6-
#[cfg(all(feature = "jemalloc", unix))]
76
#[global_allocator]
8-
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
7+
static ALLOC: foundry_cli::utils::Allocator = foundry_cli::utils::new_allocator();
98

109
fn main() {
1110
if let Err(err) = run() {

0 commit comments

Comments
 (0)