Crates.io | mini-alloc |
lib.rs | mini-alloc |
version | |
source | src |
created_at | 2023-12-22 20:56:14.904543 |
updated_at | 2024-12-03 18:35:14.305334 |
description | Very simple global allocator |
homepage | |
repository | https://github.com/OffchainLabs/stylus-sdk-rs |
max_upload_size | |
id | 1078665 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
mini-alloc
is a small and performant allocator optimized for wasm32
targets like Arbitrum Stylus. You can use it in your program as follows.
#[global_allocator]
static ALLOC: mini_alloc::MiniAlloc = mini_alloc::MiniAlloc::INIT;
mini-alloc
implements a minimal bump allocator strategy. It never deallocates memory -- that is, dealloc
does nothing. It's suitable for cases where binary size is at a premium and it's acceptable to leak all allocations. The simplicity of this model makes it very efficient, as seen in the following benchmarks.
MiniAlloc |
WeeAlloc |
Std Library | |
---|---|---|---|
alloc | 333 gas | 721 gas | 516 gas |
alloc_zeroed | 329 gas | 95 million gas | 48 million gas |
The benchmarks compare the performance of this crate's edge_cases
test in the Stylus VM. Normal allocations are over 2x cheaper than when using WeeAlloc
, a common WASM alternative that this crate defaults to when built for non-WASM targets.
Replacing each instance of alloc
in the test with alloc_zeroed
reveals an over 99% improvement for zero-filled allocations. Unlike WeeAlloc
and the standard library, MiniAlloc
takes advantage of the fact that WASM pages are zero-filled at initialization, and uses fewer of them due to the layout of Rust's memory.
In the above tests we disable memory expansion costs, which unfairly penelize WeeAlloc
and the standard library due to their increased resource consumption.
MiniAlloc
should not be used in wasm32
environments that enable the multithreading proposal. Although MiniAlloc
implements Sync
since Rust requires it for the global allocator, this crate should not be used in this way. This should not be a concern in Stylus
.
Also, core::arch::wasm32::memory_grow
must never be called by any code outside this crate.
© 2023-2024 Offchain Labs, Inc.
This project is licensed under either of
at your option.
The SPDX license identifier for this project is MIT OR Apache-2.0
.