Skip to content

Commit

Permalink
chore: Replace HashMap and HashSet with Hashbrown versions (skip …
Browse files Browse the repository at this point in the history
…`./stackslib` and `./testnet/stacks-node`)
  • Loading branch information
jbencin committed Feb 20, 2024
1 parent 5a4b388 commit f8c6760
Show file tree
Hide file tree
Showing 45 changed files with 71 additions and 52 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ members = [

# Dependencies we want to keep the same between workspace members
[workspace.dependencies]
wsts = { version = "8.1", default-features = false }
ed25519-dalek = { version = "2.1.1", features = ["serde", "rand_core"] }
hashbrown = "0.14.3"
rand_core = "0.6"
rand = "0.8"
rand_chacha = "0.3.1"
wsts = { version = "8.1", default-features = false }

# Use a bit more than default optimization for
# dev builds to speed up test execution
Expand Down
1 change: 1 addition & 0 deletions clarity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ slog = { version = "2.5.2", features = [ "max_level_trace" ] }
stacks_common = { package = "stacks-common", path = "../stacks-common" }
rstest = "0.17.0"
rstest_reuse = "0.5.0"
hashbrown = { workspace = true }

[dependencies.serde_json]
version = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion clarity/src/vm/analysis/analysis_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::collections::{BTreeMap, BTreeSet};

use stacks_common::types::StacksEpochId;

Expand Down
2 changes: 1 addition & 1 deletion clarity/src/vm/analysis/arithmetic_checker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::HashMap;
use hashbrown::HashMap;

pub use super::errors::{
check_argument_count, check_arguments_at_least, CheckError, CheckErrors, CheckResult,
Expand Down
3 changes: 1 addition & 2 deletions clarity/src/vm/analysis/read_only_checker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::HashMap;

use hashbrown::HashMap;
use stacks_common::types::StacksEpochId;

pub use super::errors::{
Expand Down
3 changes: 1 addition & 2 deletions clarity/src/vm/analysis/trait_checker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::HashMap;

use hashbrown::HashMap;
use stacks_common::types::StacksEpochId;

use crate::vm::analysis::errors::{CheckError, CheckErrors, CheckResult};
Expand Down
3 changes: 1 addition & 2 deletions clarity/src/vm/analysis/type_checker/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::HashMap;

use hashbrown::HashMap;
use stacks_common::types::StacksEpochId;

use crate::vm::analysis::errors::{CheckError, CheckErrors, CheckResult};
Expand Down
4 changes: 3 additions & 1 deletion clarity/src/vm/analysis/type_checker/v2_05/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::{BTreeMap, HashMap, HashSet};
use std::collections::BTreeMap;

use hashbrown::{HashMap, HashSet};

use crate::vm::analysis::errors::{CheckError, CheckErrors, CheckResult};
use crate::vm::analysis::types::ContractAnalysis;
Expand Down
3 changes: 2 additions & 1 deletion clarity/src/vm/analysis/type_checker/v2_05/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ pub mod contexts;
//mod maps;
pub mod natives;

use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;
use std::convert::TryInto;

use hashbrown::HashMap;
use stacks_common::types::StacksEpochId;

use self::contexts::ContractContext;
Expand Down
4 changes: 3 additions & 1 deletion clarity/src/vm/analysis/type_checker/v2_1/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::{BTreeMap, HashMap, HashSet};
use std::collections::BTreeMap;

use hashbrown::{HashMap, HashSet};

use crate::vm::analysis::errors::{CheckError, CheckErrors, CheckResult};
use crate::vm::analysis::types::ContractAnalysis;
Expand Down
3 changes: 2 additions & 1 deletion clarity/src/vm/analysis/type_checker/v2_1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
pub mod contexts;
pub mod natives;

use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;
use std::convert::TryInto;

use hashbrown::HashMap;
use stacks_common::types::StacksEpochId;

use self::contexts::ContractContext;
Expand Down
3 changes: 2 additions & 1 deletion clarity/src/vm/analysis/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::collections::{BTreeMap, BTreeSet};

use hashbrown::HashMap;
use stacks_common::types::StacksEpochId;

use crate::vm::analysis::analysis_db::AnalysisDatabase;
Expand Down
3 changes: 2 additions & 1 deletion clarity/src/vm/ast/definition_sorter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::{HashMap, HashSet};
use std::iter::FromIterator;

use hashbrown::{HashMap, HashSet};

use crate::vm::ast::errors::{ParseError, ParseErrors, ParseResult};
use crate::vm::ast::types::{BuildASTPass, ContractAST};
use crate::vm::costs::cost_functions::ClarityCostFunction;
Expand Down
3 changes: 1 addition & 2 deletions clarity/src/vm/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,7 @@ pub fn build_ast<T: CostTracker>(

#[cfg(test)]
mod test {
use std::collections::HashMap;

use hashbrown::HashMap;
use stacks_common::types::StacksEpochId;

use crate::vm::ast::errors::ParseErrors;
Expand Down
3 changes: 2 additions & 1 deletion clarity/src/vm/ast/sugar_expander/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::{HashMap, HashSet};
use std::convert::TryInto;

use hashbrown::{HashMap, HashSet};

use crate::vm::ast::errors::{ParseError, ParseErrors, ParseResult};
use crate::vm::ast::types::{BuildASTPass, ContractAST, PreExpressionsDrain};
use crate::vm::functions::define::{DefineFunctions, DefineFunctionsParsed};
Expand Down
2 changes: 1 addition & 1 deletion clarity/src/vm/ast/traits_resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::{HashMap, HashSet};
use hashbrown::{HashMap, HashSet};

use crate::vm::analysis::AnalysisDatabase;
use crate::vm::ast::errors::{ParseError, ParseErrors, ParseResult};
Expand Down
3 changes: 2 additions & 1 deletion clarity/src/vm/ast/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::{HashMap, HashSet};
use std::vec::Drain;

use hashbrown::{HashMap, HashSet};

use crate::vm::ast::errors::ParseResult;
use crate::vm::representations::{PreSymbolicExpression, SymbolicExpression, TraitDefinition};
use crate::vm::types::signatures::FunctionSignature;
Expand Down
2 changes: 1 addition & 1 deletion clarity/src/vm/callables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;
use std::convert::TryInto;
use std::fmt;
use std::iter::FromIterator;
Expand Down
7 changes: 4 additions & 3 deletions clarity/src/vm/contexts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::collections::{BTreeMap, BTreeSet};
use std::convert::TryInto;
use std::fmt;
use std::mem::replace;

use hashbrown::{HashMap, HashSet};
use serde::Serialize;
use serde_json::json;
use stacks_common::consts::CHAIN_ID_TESTNET;
Expand Down Expand Up @@ -277,7 +278,7 @@ impl AssetMap {
amount: u128,
) -> Result<u128> {
let current_amount = match self.token_map.get(principal) {
Some(principal_map) => *principal_map.get(&asset).unwrap_or(&0),
Some(principal_map) => *principal_map.get(asset).unwrap_or(&0),
None => 0,
};

Expand Down Expand Up @@ -1948,7 +1949,7 @@ impl CallStack {
)
.into());
}
if tracked && !self.set.remove(&function) {
if tracked && !self.set.remove(function) {
return Err(InterpreterError::InterpreterError(
"Tried to remove tracked function from call stack, but could not find in current context.".into()
)
Expand Down
5 changes: 3 additions & 2 deletions clarity/src/vm/costs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;
use std::convert::{TryFrom, TryInto};
use std::{cmp, fmt};

use hashbrown::HashMap;
use lazy_static::lazy_static;
use rusqlite::types::{FromSql, FromSqlResult, ToSql, ToSqlOutput, ValueRef};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -814,7 +815,7 @@ impl TrackerData {
let mut cost_contracts = HashMap::new();
let mut m = HashMap::new();
for f in ClarityCostFunction::ALL.iter() {
let cost_function_ref = cost_function_references.remove(&f).unwrap_or_else(|| {
let cost_function_ref = cost_function_references.remove(f).unwrap_or_else(|| {
ClarityCostFunctionReference::new(boot_costs_id.clone(), f.get_name())
});
if !cost_contracts.contains_key(&cost_function_ref.contract_id) {
Expand Down
3 changes: 2 additions & 1 deletion clarity/src/vm/coverage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::collections::{BTreeMap, HashMap, HashSet};
use std::collections::BTreeMap;
use std::fs::File;
use std::io::Write;

use hashbrown::{HashMap, HashSet};
use serde_json::Value as JsonValue;

use super::functions::define::DefineFunctionsParsed;
Expand Down
1 change: 0 additions & 1 deletion clarity/src/vm/database/clarity_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::{HashMap, VecDeque};
use std::convert::{TryFrom, TryInto};

use serde_json;
Expand Down
2 changes: 1 addition & 1 deletion clarity/src/vm/database/key_value_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

use std::clone::Clone;
use std::cmp::Eq;
use std::collections::HashMap;
use std::hash::Hash;

use hashbrown::HashMap;
use stacks_common::types::chainstate::StacksBlockId;
use stacks_common::types::StacksEpochId;
use stacks_common::util::hash::Sha512Trunc256Sum;
Expand Down
2 changes: 1 addition & 1 deletion clarity/src/vm/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::HashMap;
use hashbrown::HashMap;

pub use self::clarity_db::{
BurnStateDB, ClarityDatabase, HeadersDB, StoreType, NULL_BURN_STATE_DB, NULL_HEADER_DB,
Expand Down
3 changes: 2 additions & 1 deletion clarity/src/vm/docs/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::{BTreeMap, HashMap, HashSet};
use std::collections::BTreeMap;
use std::iter::FromIterator;

use hashbrown::{HashMap, HashSet};
use stacks_common::consts::CHAIN_ID_TESTNET;
use stacks_common::types::StacksEpochId;

Expand Down
2 changes: 1 addition & 1 deletion clarity/src/vm/functions/define.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;

use crate::vm::callables::{DefineType, DefinedFunction};
use crate::vm::contexts::{ContractContext, Environment, LocalContext};
Expand Down
3 changes: 1 addition & 2 deletions clarity/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,7 @@ pub fn execute_v2(program: &str) -> Result<Option<Value>> {

#[cfg(test)]
mod test {
use std::collections::HashMap;

use hashbrown::HashMap;
use stacks_common::consts::CHAIN_ID_TESTNET;
use stacks_common::types::StacksEpochId;

Expand Down
2 changes: 1 addition & 1 deletion clarity/src/vm/tests/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ fn test_native_stx_ops(epoch: StacksEpochId, mut env_factory: TopLevelMemoryEnvi

let table = asset_map.to_table();

let contract_principal = token_contract_id.clone().into();
let contract_principal = PrincipalData::from(token_contract_id.clone());

assert_eq!(
table[&contract_principal][&AssetIdentifier::STX()],
Expand Down
3 changes: 1 addition & 2 deletions clarity/src/vm/tests/principals.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;

use hashbrown::HashMap;
use stacks_common::types::StacksEpochId;
use stacks_common::util::hash::hex_bytes;

Expand Down
2 changes: 1 addition & 1 deletion clarity/src/vm/types/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::borrow::Borrow;
use std::collections::HashMap;
use std::convert::{TryFrom, TryInto};
use std::io::{Read, Write};
use std::{cmp, error, fmt, str};

use hashbrown::HashMap;
use lazy_static::lazy_static;
use serde_json::Value as JSONValue;
use stacks_common::codec::{Error as codec_error, StacksMessageCodec};
Expand Down
5 changes: 3 additions & 2 deletions clarity/src/vm/types/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>.

use std::collections::btree_map::Entry;
// TypeSignatures
use std::collections::{BTreeMap, HashMap, HashSet};
use std::collections::BTreeMap;
use std::convert::{TryFrom, TryInto};
use std::hash::{Hash, Hasher};
use std::{cmp, fmt};

// TypeSignatures
use hashbrown::HashSet;
use lazy_static::lazy_static;
use stacks_common::address::c32;
use stacks_common::types::StacksEpochId;
Expand Down
1 change: 1 addition & 0 deletions contrib/tools/relay-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ name = "relay-server"
path = "src/main.rs"

[dependencies]
hashbrown = { workspace = true }
3 changes: 2 additions & 1 deletion contrib/tools/relay-server/src/http.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::io::{Error, Read};

use hashbrown::HashMap;

use crate::to_io_result::ToIoResult;

#[derive(Debug)]
Expand Down
Loading

0 comments on commit f8c6760

Please sign in to comment.