Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use fxhash in maps when the key is small #7393

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 66 additions & 55 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -186,26 +186,26 @@ alloy-rlp = "0.3.3"
solang-parser = "=0.3.3"

## misc
base64 = "0.22"
chrono = { version = "0.4", default-features = false, features = ["clock", "std"] }
color-eyre = "0.6"
derive_more = "0.99"
evm-disassembler = "0.4"
eyre = "0.6"
hex = { package = "const-hex", version = "1.6", features = ["hex"] }
itertools = "0.11"
itertools = "0.12"
jsonpath_lib = "0.3"
k256 = "0.13"
pretty_assertions = "1.4"
protobuf = "=3.2.0"
rand = "0.8"
rustc-hash = "1.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["arbitrary_precision"] }
base64 = "0.21"
strum = "0.26"
toml = "0.8"
tracing = "0.1"
tracing-subscriber = "0.3"
evm-disassembler = "0.4"
vergen = { version = "8", default-features = false }
k256 = "0.13"

axum = "0.6"
hyper = "0.14"
Expand Down
1 change: 1 addition & 0 deletions crates/cheatcodes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ k256.workspace = true
walkdir = "2"
p256 = "0.13.2"
thiserror = "1"
rustc-hash.workspace = true
3 changes: 2 additions & 1 deletion crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use revm::{
primitives::{BlockEnv, CreateScheme, TransactTo},
EvmContext, InnerEvmContext, Inspector,
};
use rustc_hash::FxHashMap;
use serde_json::Value;
use std::{
collections::{BTreeMap, HashMap, VecDeque},
Expand Down Expand Up @@ -154,7 +155,7 @@ pub struct Cheatcodes {
pub expected_emits: VecDeque<ExpectedEmit>,

/// Map of context depths to memory offset ranges that may be written to within the call depth.
pub allowed_mem_writes: HashMap<u64, Vec<Range<u64>>>,
pub allowed_mem_writes: FxHashMap<u64, Vec<Range<u64>>>,

/// Current broadcasting information
pub broadcast: Option<Broadcast>,
Expand Down
1 change: 1 addition & 0 deletions crates/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ tracing.workspace = true
url = "2"
walkdir = "2"
yansi = "0.5"
rustc-hash.workspace = true

[dev-dependencies]
foundry-macros.workspace = true
Expand Down
3 changes: 2 additions & 1 deletion crates/common/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use foundry_compilers::{
Artifact, ArtifactId, FileFilter, Graph, Project, ProjectCompileOutput, ProjectPathsConfig,
Solc, SolcConfig,
};
use rustc_hash::FxHashMap;
use std::{
collections::{BTreeMap, HashMap},
convert::Infallible,
Expand Down Expand Up @@ -278,7 +279,7 @@ pub struct ContractSources {
/// Map over artifacts' contract names -> vector of file IDs
pub ids_by_name: HashMap<String, Vec<u32>>,
/// Map over file_id -> (source code, contract)
pub sources_by_id: HashMap<u32, (String, ContractBytecodeSome)>,
pub sources_by_id: FxHashMap<u32, (String, ContractBytecodeSome)>,
}

impl ContractSources {
Expand Down
4 changes: 2 additions & 2 deletions crates/common/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use foundry_config::{
},
Chain, Config,
};
use rustc_hash::FxHashMap;
use serde::Serialize;
use std::collections::HashMap;

/// Map keyed by breakpoints char to their location (contract address, pc)
pub type Breakpoints = HashMap<char, (Address, usize)>;
pub type Breakpoints = FxHashMap<char, (Address, usize)>;

/// `EvmArgs` and `EnvArgs` take the highest precedence in the Config/Figment hierarchy.
/// All vars are opt-in, their default values are expected to be set by the
Expand Down
3 changes: 2 additions & 1 deletion crates/evm/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ revm = { workspace = true, default-features = false, features = [
] }
revm-inspectors.workspace = true

derive_more.workspace = true
auto_impl = "1"
derive_more.workspace = true
eyre = "0.6"
futures = "0.3"
hex.workspace = true
itertools.workspace = true
once_cell = "1"
parking_lot = "0.12"
rustc-hash.workspace = true
serde = "1"
serde_json = "1"
thiserror = "1"
Expand Down
3 changes: 2 additions & 1 deletion crates/evm/core/src/fork/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use revm::{
db::DatabaseRef,
primitives::{AccountInfo, Bytecode, KECCAK_EMPTY},
};
use rustc_hash::FxHashMap;
use std::{
collections::{hash_map::Entry, HashMap, VecDeque},
pin::Pin,
Expand Down Expand Up @@ -86,7 +87,7 @@ pub struct BackendHandler<P> {
/// Listeners that wait for a `get_storage_at` response
storage_requests: HashMap<(Address, U256), Vec<StorageSender>>,
/// Listeners that wait for a `get_block` response
block_requests: HashMap<u64, Vec<BlockHashSender>>,
block_requests: FxHashMap<u64, Vec<BlockHashSender>>,
/// Incoming commands.
incoming: Receiver<BackendRequest>,
/// unprocessed queued requests
Expand Down
11 changes: 6 additions & 5 deletions crates/evm/core/src/ic.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
use revm::{
interpreter::{opcode, opcode::spec_opcode_gas},
primitives::{HashMap, SpecId},
primitives::SpecId,
};
use rustc_hash::FxHashMap;

/// Maps from program counter to instruction counter.
///
/// Inverse of [`IcPcMap`].
pub struct PcIcMap {
pub inner: HashMap<usize, usize>,
pub inner: FxHashMap<usize, usize>,
}

impl PcIcMap {
/// Creates a new `PcIcMap` for the given code.
pub fn new(spec: SpecId, code: &[u8]) -> Self {
let opcode_infos = spec_opcode_gas(spec);
let mut map = HashMap::new();
let mut map = FxHashMap::default();

let mut i = 0;
let mut cumulative_push_size = 0;
Expand Down Expand Up @@ -44,14 +45,14 @@ impl PcIcMap {
///
/// Inverse of [`PcIcMap`].
pub struct IcPcMap {
pub inner: HashMap<usize, usize>,
pub inner: FxHashMap<usize, usize>,
}

impl IcPcMap {
/// Creates a new `IcPcMap` for the given code.
pub fn new(spec: SpecId, code: &[u8]) -> Self {
let opcode_infos = spec_opcode_gas(spec);
let mut map = HashMap::new();
let mut map = FxHashMap::default();

let mut i = 0;
let mut cumulative_push_size = 0;
Expand Down
1 change: 1 addition & 0 deletions crates/evm/coverage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ eyre = "0.6"
revm.workspace = true
semver = "1"
tracing = "0.1"
rustc-hash.workspace = true
9 changes: 5 additions & 4 deletions crates/evm/coverage/src/analysis.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{ContractId, CoverageItem, CoverageItemKind, SourceLocation};
use foundry_common::TestFunctionExt;
use foundry_compilers::artifacts::ast::{self, Ast, Node, NodeType};
use rustc_hash::FxHashMap;
use semver::Version;
use std::collections::{HashMap, HashSet};

Expand Down Expand Up @@ -443,9 +444,9 @@ pub struct SourceAnalysis {
#[derive(Clone, Debug, Default)]
pub struct SourceAnalyzer {
/// A map of source IDs to their source code
sources: HashMap<usize, String>,
sources: FxHashMap<usize, String>,
/// A map of AST node IDs of contracts to their contract IDs.
contract_ids: HashMap<usize, ContractId>,
contract_ids: FxHashMap<usize, ContractId>,
/// A map of contract IDs to their AST nodes.
contracts: HashMap<ContractId, Node>,
/// A collection of coverage items.
Expand All @@ -463,8 +464,8 @@ impl SourceAnalyzer {
/// (defined by `version`).
pub fn new(
version: Version,
asts: HashMap<usize, Ast>,
sources: HashMap<usize, String>,
asts: FxHashMap<usize, Ast>,
sources: FxHashMap<usize, String>,
) -> eyre::Result<Self> {
let mut analyzer = SourceAnalyzer { sources, ..Default::default() };

Expand Down
6 changes: 1 addition & 5 deletions crates/evm/evm/src/executors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -838,11 +838,7 @@ fn convert_call_result(
..
} = call_result;

let breakpoints = if let Some(c) = call_result.cheatcodes {
c.breakpoints
} else {
std::collections::HashMap::new()
};
let breakpoints = call_result.cheatcodes.map(|c| c.breakpoints).unwrap_or_default();

match status {
return_ok!() => {
Expand Down
17 changes: 4 additions & 13 deletions crates/forge/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ name = "forge"
path = "bin/main.rs"

[build-dependencies]
vergen = { workspace = true, default-features = false, features = [
"build",
"git",
"gitcl",
] }
vergen = { workspace = true, default-features = false, features = ["build", "git", "gitcl"] }

[dependencies]
# lib
Expand Down Expand Up @@ -83,6 +79,7 @@ thiserror = "1"
tokio = { version = "1", features = ["time"] }
watchexec = "2.3.2"
evm-disassembler.workspace = true
rustc-hash.workspace = true

# doc server
axum = { workspace = true, features = ["ws"] }
Expand All @@ -99,9 +96,7 @@ globset = "0.4"
paste = "1.0"
path-slash = "0.2"
pretty_assertions.workspace = true
svm = { package = "svm-rs", version = "0.3", default-features = false, features = [
"rustls",
] }
svm = { package = "svm-rs", version = "0.3", default-features = false, features = ["rustls"] }
tempfile = "3"
tracing-subscriber = { workspace = true, features = ["env-filter", "fmt"] }

Expand All @@ -113,11 +108,7 @@ rustls = [
"reqwest/rustls-tls",
"reqwest/rustls-tls-native-roots",
]
openssl = [
"foundry-cli/openssl",
"reqwest/default-tls",
"foundry-wallets/openssl",
]
openssl = ["foundry-cli/openssl", "reqwest/default-tls", "foundry-wallets/openssl"]
asm-keccak = ["alloy-primitives/asm-keccak"]

[[bench]]
Expand Down
5 changes: 3 additions & 2 deletions crates/forge/bin/cmd/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use foundry_compilers::{
Artifact, Project, ProjectCompileOutput,
};
use foundry_config::{Config, SolcReq};
use rustc_hash::FxHashMap;
use semver::Version;
use std::{collections::HashMap, path::PathBuf, sync::mpsc::channel};
use yansi::Paint;
Expand Down Expand Up @@ -155,8 +156,8 @@ impl CoverageArgs {
let mut report = CoverageReport::default();

// Collect ASTs and sources
let mut versioned_asts: HashMap<Version, HashMap<usize, Ast>> = HashMap::new();
let mut versioned_sources: HashMap<Version, HashMap<usize, String>> = HashMap::new();
let mut versioned_asts: HashMap<Version, FxHashMap<usize, Ast>> = HashMap::new();
let mut versioned_sources: HashMap<Version, FxHashMap<usize, String>> = HashMap::new();
for (path, mut source_file, version) in sources.into_sources_with_version() {
report.add_source(version.clone(), source_file.id as usize, path.clone());

Expand Down
2 changes: 1 addition & 1 deletion crates/forge/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl<'a> ContractRunner<'a> {
err.stipend,
None,
err.state_changeset,
HashMap::new(),
Default::default(),
)
}
Err(EvmError::SkipError) => {
Expand Down
Loading