Skip to content

perf: small performance and safety improvements#2868

Merged
rakita merged 1 commit intomainfrom
perf/small-optimizations
Aug 8, 2025
Merged

perf: small performance and safety improvements#2868
rakita merged 1 commit intomainfrom
perf/small-optimizations

Conversation

@rakita
Copy link
Member

@rakita rakita commented Aug 8, 2025

  • Remove unwrap() in primitives::short_address by using direct array indexing
  • Use lazy allocation with or_insert_with to avoid unnecessary Vec allocations
  • Pre-allocate Vec capacity in State::increment_balances based on iterator hint
  • Simplify iterator pattern in bundle_state to use destructuring
  • Use mem::take instead of mem::replace for bool values
  • Add #[inline] attributes to small hot-path functions in state module
  • Use unwrap_or_default() instead of unwrap_or(0) for cleaner code

These are small incremental improvements that enhance performance and code safety without changing functionality.

- Remove unwrap() in primitives::short_address by using direct array indexing
- Use lazy allocation with or_insert_with to avoid unnecessary Vec allocations
- Pre-allocate Vec capacity in State::increment_balances based on iterator hint
- Simplify iterator pattern in bundle_state to use destructuring
- Use mem::take instead of mem::replace for bool values
- Add #[inline] attributes to small hot-path functions in state module
- Use unwrap_or_default() instead of unwrap_or(0) for cleaner code

These are small incremental improvements that enhance performance and code safety without changing functionality.
pub fn short_address(address: &Address) -> Option<usize> {
if address.0[..18].iter().all(|b| *b == 0) {
let short_address = u16::from_be_bytes(address.0[18..].try_into().unwrap()) as usize;
let short_address = u16::from_be_bytes([address.0[18], address.0[19]]) as usize;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting: an address is 20 bytes, so this is equivalent.

Hopefully the compiler can figure it out and avoid the bounds checks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a constant index to an array that is of fixed size, compiler has all the information needed to optimise it

Copy link
Contributor

@kevaundray kevaundray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@codspeed-hq
Copy link

codspeed-hq bot commented Aug 8, 2025

CodSpeed Performance Report

Merging #2868 will not alter performance

Comparing perf/small-optimizations (7bafb87) with main (dfe1fdb)

Summary

✅ 171 untouched benchmarks

@rakita rakita merged commit 00914e5 into main Aug 8, 2025
31 checks passed
This was referenced Aug 8, 2025
@rakita rakita deleted the perf/small-optimizations branch August 14, 2025 22:59
lwedge99 pushed a commit to sentioxyz/revm that referenced this pull request Sep 16, 2025
- Remove unwrap() in primitives::short_address by using direct array indexing
- Use lazy allocation with or_insert_with to avoid unnecessary Vec allocations
- Pre-allocate Vec capacity in State::increment_balances based on iterator hint
- Simplify iterator pattern in bundle_state to use destructuring
- Use mem::take instead of mem::replace for bool values
- Add #[inline] attributes to small hot-path functions in state module
- Use unwrap_or_default() instead of unwrap_or(0) for cleaner code

These are small incremental improvements that enhance performance and code safety without changing functionality.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants