Skip to content

Commit

Permalink
Merge pull request #429 from near/austin/custom_alloc
Browse files Browse the repository at this point in the history
feat: configure global allocator by default
  • Loading branch information
mikedotexe authored Jun 28, 2021
2 parents c1ac1ec + c929a93 commit f69d375
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* expose `cur_block` and `genesis_config` from `RuntimeStandalone` to configure simulation tests. [PR 390](https://github.com/near/near-sdk-rs/pull/390).
* fix(simulator): failing with long chains. [PR 385](https://github.com/near/near-sdk-rs/pull/385).
* Make block time configurable to sim contract tests. [PR 378](https://github.com/near/near-sdk-rs/pull/378).
* Set up global allocator by default for WASM architectures. [PR 429](https://github.com/near/near-sdk-rs/pull/429).
* This removes the re-export of `wee_alloc` because if this feature is enabled, the allocator will already be set.
* Deprecates `setup_alloc!` macro as this will be setup by default, as long as the `wee_alloc` feature is not specifically disabled. In this case, the allocator can be overriden to a custom one or set manually if intended.
* Update `TreeMap` iterator implementation to avoid unnecessary storage reads. [PR 428](https://github.com/near/near-sdk-rs/pull/428).

## `3.1.0` [04-06-2021]
Expand Down
3 changes: 2 additions & 1 deletion near-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bs58 = "0.4"
near-vm-logic = "=4.0.0-pre.1"
near-primitives-core = "=0.4.0"
# Export dependencies for contracts
wee_alloc = { version = "0.4.5", default-features = false, features = [] }
wee_alloc = { version = "0.4.5", default-features = false, optional = true }

# Used for caching, might be worth porting only functionality needed.
once_cell = { version = "1.7.2", optional = true, default-features = false }
Expand All @@ -40,5 +40,6 @@ rand_xorshift = "0.2.0"
quickcheck = "0.9.2"

[features]
default = ["wee_alloc"]
expensive-debug = []
unstable = ["once_cell"]
8 changes: 5 additions & 3 deletions near-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ pub use environment::blockchain_interface::BlockchainInterface;

pub mod test_utils;

// Set up global allocator by default if custom-allocator feature is not set in wasm32 architecture.
#[cfg(all(feature = "wee_alloc", target_arch = "wasm32"))]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

// Exporting common crates

#[doc(hidden)]
Expand All @@ -52,6 +57,3 @@ pub use serde;

#[doc(hidden)]
pub use serde_json;

#[doc(hidden)]
pub use wee_alloc;
12 changes: 7 additions & 5 deletions near-sdk/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,15 @@ impl PendingContractTx {
/// Sets up the [GlobalAllocator] with [`WeeAlloc`](crate::wee_alloc::WeeAlloc).
///
/// [GlobalAllocator]: std::alloc::GlobalAlloc
#[deprecated(
since = "4.0.0",
note = "Allocator is already initialized with the default `wee_alloc` feature set. \
Please make sure you don't disable default features on the SDK or set the global \
allocator manually."
)]
#[macro_export]
macro_rules! setup_alloc {
() => {
#[cfg(target_arch = "wasm32")]
#[global_allocator]
static ALLOC: near_sdk::wee_alloc::WeeAlloc<'_> = near_sdk::wee_alloc::WeeAlloc::INIT;
};
() => {};
}

#[cfg(test)]
Expand Down

0 comments on commit f69d375

Please sign in to comment.