Skip to content

Commit 10ff66d

Browse files
authored
perf: resize short addresses bitvec instead of reallocating (#3083)
1 parent 0ce9fc5 commit 10ff66d

3 files changed

Lines changed: 11 additions & 8 deletions

File tree

crates/context/src/journal/warm_addresses.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! It is used to optimize access to precompile addresses.
44
5-
use bitvec::{bitvec, order::Lsb0, vec::BitVec};
5+
use bitvec::{bitvec, vec::BitVec};
66
use primitives::{short_address, Address, HashSet, SHORT_ADDRESS_CAP};
77

88
/// Stores addresses that are warm loaded. Contains precompiles and coinbase address.
@@ -32,7 +32,7 @@ impl WarmAddresses {
3232
pub fn new() -> Self {
3333
Self {
3434
precompile_set: HashSet::default(),
35-
precompile_short_addresses: BitVec::new(),
35+
precompile_short_addresses: bitvec![0; SHORT_ADDRESS_CAP],
3636
all_short_addresses: true,
3737
coinbase: None,
3838
}
@@ -53,8 +53,7 @@ impl WarmAddresses {
5353
/// Set the precompile addresses and short addresses.
5454
#[inline]
5555
pub fn set_precompile_addresses(&mut self, addresses: HashSet<Address>) {
56-
// short address is always smaller than SHORT_ADDRESS_CAP
57-
self.precompile_short_addresses = bitvec![usize, Lsb0; 0; SHORT_ADDRESS_CAP];
56+
self.precompile_short_addresses.fill(false);
5857

5958
let mut all_short_addresses = true;
6059
for address in addresses.iter() {
@@ -124,7 +123,11 @@ mod tests {
124123
fn test_initialization() {
125124
let warm_addresses = WarmAddresses::new();
126125
assert!(warm_addresses.precompile_set.is_empty());
127-
assert!(warm_addresses.precompile_short_addresses.is_empty());
126+
assert_eq!(
127+
warm_addresses.precompile_short_addresses.len(),
128+
SHORT_ADDRESS_CAP
129+
);
130+
assert!(!warm_addresses.precompile_short_addresses.any());
128131
assert!(warm_addresses.coinbase.is_none());
129132

130133
// Test Default trait

crates/handler/src/precompile_provider.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub trait PrecompileProvider<CTX: ContextTr> {
1616

1717
/// Sets the spec id and returns true if the spec id was changed. Initial call to set_spec will always return true.
1818
///
19-
/// Returned booling will determine if precompile addresses should be injected into the journal.
19+
/// Returns `true` if precompile addresses should be injected into the journal.
2020
fn set_spec(&mut self, spec: <CTX::Cfg as Cfg>::Spec) -> bool;
2121

2222
/// Run the precompile.

crates/primitives/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ pub const SHORT_ADDRESS_CAP: usize = 300;
5454
/// and last two bytes are less than [`SHORT_ADDRESS_CAP`].
5555
#[inline]
5656
pub fn short_address(address: &Address) -> Option<usize> {
57-
if address.0[..18].iter().all(|b| *b == 0) {
58-
let short_address = u16::from_be_bytes([address.0[18], address.0[19]]) as usize;
57+
if address[..18].iter().all(|b| *b == 0) {
58+
let short_address = u16::from_be_bytes([address[18], address[19]]) as usize;
5959
if short_address < SHORT_ADDRESS_CAP {
6060
return Some(short_address);
6161
}

0 commit comments

Comments
 (0)