diff --git a/README.md b/README.md index f401896b..88edfb7a 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,15 @@ RUSTFLAGS=-Ctarget-cpu=native cargo run --release --example bench_recursion -- - ## Jemalloc -By default, Plonky2 uses the [Jemalloc](http://jemalloc.net) memory allocator due to its superior performance. Currently, it changes the default allocator of any binary to which it is linked. You can disable this behavior by removing the corresponding lines in [`plonky2/src/lib.rs`](https://github.com/mir-protocol/plonky2/blob/main/plonky2/src/lib.rs). +Plonky2 prefers the [Jemalloc](http://jemalloc.net) memory allocator due to its superior performance. To use it, include `jemallocator = "0.3.2"` in`Cargo.toml`and add the following lines +to your `main.rs`: + +```rust +use jemallocator::Jemalloc; + +#[global_allocator] +static GLOBAL: Jemalloc = Jemalloc; +``` Jemalloc is known to cause crashes when a binary compiled for x86 is run on an Apple silicon-based Mac under [Rosetta 2](https://support.apple.com/en-us/HT211861). If you are experiencing crashes on your Apple silicon Mac, run `rustc --print target-libdir`. The output should contain `aarch64-apple-darwin`. If the output contains `x86_64-apple-darwin`, then you are running the Rust toolchain for x86; we recommend switching to the native ARM version. diff --git a/plonky2/Cargo.toml b/plonky2/Cargo.toml index c873bb0f..9c019640 100644 --- a/plonky2/Cargo.toml +++ b/plonky2/Cargo.toml @@ -27,15 +27,15 @@ serde_cbor = "0.11.1" keccak-hash = "0.8.0" static_assertions = "1.1.0" -[target.'cfg(not(target_env = "msvc"))'.dependencies] -jemallocator = "0.3.2" - [dev-dependencies] criterion = "0.3.5" tynm = "0.1.6" structopt = "0.3.26" num_cpus = "1.13.1" +[target.'cfg(not(target_env = "msvc"))'.dev-dependencies] +jemallocator = "0.3.2" + [[bench]] name = "field_arithmetic" harness = false diff --git a/plonky2/benches/allocator/mod.rs b/plonky2/benches/allocator/mod.rs new file mode 100644 index 00000000..441e5dc5 --- /dev/null +++ b/plonky2/benches/allocator/mod.rs @@ -0,0 +1,7 @@ +// Set up Jemalloc +#[cfg(not(target_env = "msvc"))] +use jemallocator::Jemalloc; + +#[cfg(not(target_env = "msvc"))] +#[global_allocator] +static GLOBAL: Jemalloc = Jemalloc; diff --git a/plonky2/benches/ffts.rs b/plonky2/benches/ffts.rs index 3d63f6e1..c9320537 100644 --- a/plonky2/benches/ffts.rs +++ b/plonky2/benches/ffts.rs @@ -1,3 +1,5 @@ +mod allocator; + use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use plonky2::field::field_types::Field; use plonky2::field::goldilocks_field::GoldilocksField; diff --git a/plonky2/benches/field_arithmetic.rs b/plonky2/benches/field_arithmetic.rs index 7b74ae52..62d1c2c5 100644 --- a/plonky2/benches/field_arithmetic.rs +++ b/plonky2/benches/field_arithmetic.rs @@ -1,3 +1,5 @@ +mod allocator; + use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; use plonky2::field::extension_field::quadratic::QuadraticExtension; use plonky2::field::extension_field::quartic::QuarticExtension; diff --git a/plonky2/benches/hashing.rs b/plonky2/benches/hashing.rs index a968d957..4632c4c4 100644 --- a/plonky2/benches/hashing.rs +++ b/plonky2/benches/hashing.rs @@ -1,6 +1,8 @@ #![allow(incomplete_features)] #![feature(generic_const_exprs)] +mod allocator; + use criterion::{criterion_group, criterion_main, BatchSize, Criterion}; use plonky2::field::goldilocks_field::GoldilocksField; use plonky2::hash::hash_types::{BytesHash, RichField}; diff --git a/plonky2/benches/merkle.rs b/plonky2/benches/merkle.rs index 8bc43730..88302ae9 100644 --- a/plonky2/benches/merkle.rs +++ b/plonky2/benches/merkle.rs @@ -1,5 +1,7 @@ #![feature(generic_const_exprs)] +mod allocator; + use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use plonky2::field::goldilocks_field::GoldilocksField; use plonky2::hash::hash_types::RichField; diff --git a/plonky2/benches/reverse_index_bits.rs b/plonky2/benches/reverse_index_bits.rs index 90f1e285..fb0b0d52 100644 --- a/plonky2/benches/reverse_index_bits.rs +++ b/plonky2/benches/reverse_index_bits.rs @@ -1,3 +1,5 @@ +mod allocator; + use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use plonky2::field::field_types::Field; use plonky2::field::goldilocks_field::GoldilocksField; diff --git a/plonky2/benches/transpose.rs b/plonky2/benches/transpose.rs index 0f20ca0f..dee020cb 100644 --- a/plonky2/benches/transpose.rs +++ b/plonky2/benches/transpose.rs @@ -1,3 +1,5 @@ +mod allocator; + use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use plonky2::field::field_types::Field; use plonky2::field::goldilocks_field::GoldilocksField; diff --git a/plonky2/src/lib.rs b/plonky2/src/lib.rs index 1502cea9..64acfe12 100644 --- a/plonky2/src/lib.rs +++ b/plonky2/src/lib.rs @@ -19,11 +19,3 @@ pub mod hash; pub mod iop; pub mod plonk; pub mod util; - -// Set up Jemalloc -#[cfg(not(target_env = "msvc"))] -use jemallocator::Jemalloc; - -#[cfg(not(target_env = "msvc"))] -#[global_allocator] -static GLOBAL: Jemalloc = Jemalloc; diff --git a/system_zero/Cargo.toml b/system_zero/Cargo.toml index a9029dad..f1cb5729 100644 --- a/system_zero/Cargo.toml +++ b/system_zero/Cargo.toml @@ -21,3 +21,6 @@ criterion = "0.3.5" [[bench]] name = "lookup_permuted_cols" harness = false + +[target.'cfg(not(target_env = "msvc"))'.dev-dependencies] +jemallocator = "0.3.2" diff --git a/system_zero/benches/allocator/mod.rs b/system_zero/benches/allocator/mod.rs new file mode 100644 index 00000000..441e5dc5 --- /dev/null +++ b/system_zero/benches/allocator/mod.rs @@ -0,0 +1,7 @@ +// Set up Jemalloc +#[cfg(not(target_env = "msvc"))] +use jemallocator::Jemalloc; + +#[cfg(not(target_env = "msvc"))] +#[global_allocator] +static GLOBAL: Jemalloc = Jemalloc; diff --git a/system_zero/benches/lookup_permuted_cols.rs b/system_zero/benches/lookup_permuted_cols.rs index 371b3470..e44e9fc8 100644 --- a/system_zero/benches/lookup_permuted_cols.rs +++ b/system_zero/benches/lookup_permuted_cols.rs @@ -1,3 +1,5 @@ +mod allocator; + use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use itertools::Itertools; use plonky2::field::field_types::Field;