Skip to content

Commit

Permalink
chore(std): Add std HashMap,HashSet (bluealloy#1041)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakita authored Feb 2, 2024
1 parent 8148063 commit 653dec1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

6 changes: 5 additions & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@ once_cell = { version = "1.19", default-features = false, optional = true }
# utility
enumn = "0.1"
derive_more = { version = "0.99", optional = true }
cfg-if = "1"

# optional
serde = { version = "1.0", default-features = false, features = ["derive", "rc"], optional = true }
serde = { version = "1.0", default-features = false, features = [
"derive",
"rc",
], optional = true }

[build-dependencies]
hex = { version = "0.4", default-features = false }
Expand Down
5 changes: 2 additions & 3 deletions crates/primitives/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{Account, AccountInfo, Address, Bytecode, B256, U256};
use crate::{Account, AccountInfo, Address, Bytecode, HashMap, B256, U256};
use auto_impl::auto_impl;
use hashbrown::HashMap as Map;

pub mod components;
pub use components::{
Expand Down Expand Up @@ -30,7 +29,7 @@ pub trait Database {
#[auto_impl(&mut, Box)]
pub trait DatabaseCommit {
/// Commit changes to the database.
fn commit(&mut self, changes: Map<Address, Account>);
fn commit(&mut self, changes: HashMap<Address, Account>);
}

/// EVM database interface.
Expand Down
12 changes: 10 additions & 2 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ pub mod result;
pub mod specification;
pub mod state;
pub mod utilities;

pub use alloy_primitives::{
self, address, b256, bytes, fixed_bytes, hex, hex_literal, ruint, uint, Address, Bytes,
FixedBytes, Log, LogData, B256, I256, U256,
Expand All @@ -29,7 +28,16 @@ pub use bitvec;
pub use bytecode::*;
pub use constants::*;
pub use env::*;
pub use hashbrown::{hash_map, hash_set, HashMap, HashSet};

cfg_if::cfg_if! {
if #[cfg(std)] {
pub use std::collections::{hash_map, hash_set, HashMap, HashSet};
use hashbrown as _;
} else {
pub use hashbrown::{hash_map, hash_set, HashMap, HashSet};
}
}

#[cfg(feature = "c-kzg")]
pub use kzg::{EnvKzgSettings, KzgSettings};
pub use precompile::*;
Expand Down
3 changes: 1 addition & 2 deletions crates/primitives/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{Address, Bytecode, B256, KECCAK_EMPTY, U256};
use crate::{Address, Bytecode, HashMap, B256, KECCAK_EMPTY, U256};
use bitflags::bitflags;
use core::hash::{Hash, Hasher};
use hashbrown::HashMap;

/// EVM State is a mapping from addresses to accounts.
pub type State = HashMap<Address, Account>;
Expand Down
14 changes: 7 additions & 7 deletions crates/revm/src/journaled_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,17 +446,17 @@ impl JournaledState {
) -> Result<SelfDestructResult, DB::Error> {
let (is_cold, target_exists) = self.load_account_exist(target, db)?;

let acc = if address != target {
if address != target {
// Both accounts are loaded before this point, `address` as we execute its contract.
// and `target` at the beginning of the function.
let [acc, target_account] = self.state.get_many_mut([&address, &target]).unwrap();
let acc_balance = self.state.get_mut(&target).unwrap().info.balance;

let target_account = self.state.get_mut(&target).unwrap();
Self::touch_account(self.journal.last_mut().unwrap(), &target, target_account);
target_account.info.balance += acc.info.balance;
acc
} else {
self.state.get_mut(&address).unwrap()
};
target_account.info.balance += acc_balance;
}

let acc = self.state.get_mut(&address).unwrap();
let balance = acc.info.balance;
let previously_destroyed = acc.is_selfdestructed();
let is_cancun_enabled = SpecId::enabled(self.spec, CANCUN);
Expand Down

0 comments on commit 653dec1

Please sign in to comment.