Skip to content

Refactor: abstract global allocator in foundry-cli to be used across crates #10523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
6 changes: 2 additions & 4 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions crates/anvil/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ ctrlc = { version = "3", optional = true }
fdlimit = { version = "0.3", optional = true }
clap_complete_fig = "4"

[target.'cfg(unix)'.dependencies]
tikv-jemallocator = { workspace = true, optional = true }

[dev-dependencies]
alloy-provider = { workspace = true, features = ["txpool-api"] }
alloy-pubsub.workspace = true
Expand All @@ -112,7 +109,7 @@ op-alloy-rpc-types.workspace = true
[features]
default = ["cli", "jemalloc"]
asm-keccak = ["alloy-primitives/asm-keccak"]
jemalloc = ["dep:tikv-jemallocator"]
jemalloc = ["foundry-cli/jemalloc"]
cli = ["tokio/full", "cmd"]
cmd = [
"clap",
Expand Down
3 changes: 1 addition & 2 deletions crates/anvil/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

use anvil::args::run;

#[cfg(all(feature = "jemalloc", unix))]
#[global_allocator]
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
static ALLOC: foundry_cli::utils::Allocator = foundry_cli::utils::new_allocator();

fn main() {
if let Err(err) = run() {
Expand Down
5 changes: 1 addition & 4 deletions crates/cast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,14 @@ tracing.workspace = true
yansi.workspace = true
evmole.workspace = true

[target.'cfg(unix)'.dependencies]
tikv-jemallocator = { workspace = true, optional = true }

[dev-dependencies]
anvil.workspace = true
foundry-test-utils.workspace = true

[features]
default = ["jemalloc"]
asm-keccak = ["alloy-primitives/asm-keccak"]
jemalloc = ["dep:tikv-jemallocator"]
jemalloc = ["foundry-cli/jemalloc"]
aws-kms = ["foundry-wallets/aws-kms", "dep:aws-sdk-kms"]
gcp-kms = ["foundry-wallets/gcp-kms", "dep:gcloud-sdk"]
isolate-by-default = ["foundry-config/isolate-by-default"]
3 changes: 1 addition & 2 deletions crates/cast/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

use cast::args::run;

#[cfg(all(feature = "jemalloc", unix))]
#[global_allocator]
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
static ALLOC: foundry_cli::utils::Allocator = foundry_cli::utils::new_allocator();

fn main() {
if let Err(err) = run() {
Expand Down
5 changes: 1 addition & 4 deletions crates/chisel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,10 @@ yansi.workspace = true
tracing.workspace = true
walkdir.workspace = true

[target.'cfg(unix)'.dependencies]
tikv-jemallocator = { workspace = true, optional = true }

[dev-dependencies]
tracing-subscriber.workspace = true

[features]
default = ["jemalloc"]
asm-keccak = ["alloy-primitives/asm-keccak"]
jemalloc = ["dep:tikv-jemallocator"]
jemalloc = ["foundry-cli/jemalloc"]
3 changes: 1 addition & 2 deletions crates/chisel/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

use chisel::args::run;

#[cfg(all(feature = "jemalloc", unix))]
#[global_allocator]
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
static ALLOC: foundry_cli::utils::Allocator = foundry_cli::utils::new_allocator();

fn main() {
if let Err(err) = run() {
Expand Down
5 changes: 5 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ alloy-provider.workspace = true
alloy-rlp.workspace = true
alloy-chains.workspace = true

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

[target.'cfg(unix)'.dependencies]
tikv-jemallocator = { workspace = true, optional = true }

[features]
tracy = ["dep:tracing-tracy"]
jemalloc = ["dep:tikv-jemallocator"]
18 changes: 18 additions & 0 deletions crates/cli/src/utils/allocator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Abstract global allocator implementation.

// If jemalloc feature is enabled on Unix systems, use jemalloc as the global allocator.
// Otherwise, explicitly use the system allocator.
cfg_if::cfg_if! {
if #[cfg(all(feature = "jemalloc", unix))] {
type AllocatorInner = tikv_jemallocator::Jemalloc;
} else {
type AllocatorInner = std::alloc::System;
}
}

pub type Allocator = AllocatorInner;

/// Creates a new [allocator][Allocator].
pub const fn new_allocator() -> Allocator {
AllocatorInner {}
}
3 changes: 3 additions & 0 deletions crates/cli/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub use suggestions::*;
mod abi;
pub use abi::*;

mod allocator;
pub use allocator::*;

// reexport all `foundry_config::utils`
#[doc(hidden)]
pub use foundry_config::utils::*;
Expand Down
5 changes: 1 addition & 4 deletions crates/forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ opener = "0.7"
soldeer-commands.workspace = true
quick-junit = "0.5.0"

[target.'cfg(unix)'.dependencies]
tikv-jemallocator = { workspace = true, optional = true }

[dev-dependencies]
anvil.workspace = true
foundry-test-utils.workspace = true
Expand All @@ -115,7 +112,7 @@ alloy-signer-local.workspace = true
[features]
default = ["jemalloc"]
asm-keccak = ["alloy-primitives/asm-keccak"]
jemalloc = ["dep:tikv-jemallocator"]
jemalloc = ["foundry-cli/jemalloc"]
aws-kms = ["foundry-wallets/aws-kms"]
gcp-kms = ["foundry-wallets/gcp-kms"]
isolate-by-default = ["foundry-config/isolate-by-default"]
3 changes: 1 addition & 2 deletions crates/forge/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@

use forge::args::run;

#[cfg(all(feature = "jemalloc", unix))]
#[global_allocator]
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
static ALLOC: foundry_cli::utils::Allocator = foundry_cli::utils::new_allocator();

fn main() {
if let Err(err) = run() {
Expand Down
Loading