Skip to content

Commit

Permalink
use HashMap::with_capacity (#2723)
Browse files Browse the repository at this point in the history
* use HashMap::with_capacity

* use HashSet::with_capacity

* use Vec::with_capacity

* fix fmt
  • Loading branch information
nkysg authored Oct 3, 2024
1 parent 8b4e548 commit d16aa66
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion crates/rooch-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl RoochDB {
column_families.append(&mut rooch_store::StoreMeta::get_column_family_names().to_vec());
//ensure no duplicate column families
{
let mut set = HashSet::new();
let mut set = HashSet::with_capacity(column_families.len());
column_families.iter().for_each(|cf| {
if !set.insert(cf) {
panic!("Duplicate column family: {}", cf);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async fn test_multisign_account_random() {
let binding_test = binding_test::RustBindingTest::new().unwrap();

let count = rand::thread_rng().gen_range(3..10);
let mut pubkeys = Vec::new();
let mut pubkeys = Vec::with_capacity(count);
for _ in 0..count {
let kp = RoochKeyPair::generate_secp256k1();
pubkeys.push(kp.bitcoin_public_key().unwrap().to_bytes());
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-key/src/keystore/base_keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl AccountKeystore for BaseKeyStore {

fn addresses(&self) -> Vec<RoochAddress> {
// Create an empty Vec to store the addresses.
let mut addresses = Vec::new();
let mut addresses = Vec::with_capacity(self.keys.len() + self.session_keys.len());

// Iterate over the `keys` and `session_keys` BTreeMaps.
for key in self.keys.keys() {
Expand Down
3 changes: 2 additions & 1 deletion crates/rooch-key/src/keystore/file_keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ impl AccountKeystore for FileBasedKeystore {

fn addresses(&self) -> Vec<RoochAddress> {
// Create an empty Vec to store the addresses.
let mut addresses = Vec::new();
let mut addresses =
Vec::with_capacity(self.keystore.keys.len() + self.keystore.session_keys.len());

// Iterate over the `keys` and `session_keys` BTreeMaps.
for key in self.keystore.keys.keys() {
Expand Down
3 changes: 2 additions & 1 deletion crates/rooch-key/src/keystore/memory_keystore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ impl AccountKeystore for InMemKeystore {

fn addresses(&self) -> Vec<RoochAddress> {
// Create an empty Vec to store the addresses.
let mut addresses = Vec::new();
let mut addresses =
Vec::with_capacity(self.keystore.keys.len() + self.keystore.session_keys.len());

// Iterate over the `keys` and `session_keys` BTreeMaps.
for key in self.keystore.keys.keys() {
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch-open-rpc-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn open_rpc(attr: TokenStream, item: TokenStream) -> TokenStream {
let name = &method.name;
let deprecated = method.deprecated;
let doc = &method.doc;
let mut inputs = Vec::new();
let mut inputs = Vec::with_capacity(method.params.len());
for (name, ty, description) in &method.params {
let (ty, required) = extract_type_from_option(ty.clone());
let description = if let Some(description) = description {
Expand Down
3 changes: 2 additions & 1 deletion crates/rooch-rpc-api/src/jsonrpc_types/btc/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ impl UTXOView {
// reversed bytes of txid
let bitcoin_txid = Txid::from_byte_array(utxo.txid.into_bytes());

let mut seals_view: HashMap<String, Vec<ObjectIDView>> = HashMap::new();
let mut seals_view: HashMap<String, Vec<ObjectIDView>> =
HashMap::with_capacity(utxo.seals.len());
utxo.seals.data.into_iter().for_each(|element| {
seals_view.insert(
element.key.to_string(),
Expand Down
2 changes: 1 addition & 1 deletion crates/rooch/src/commands/account/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl CommandAction<Option<AccountsView>> for ListCommand {
.collect();

if self.json {
let mut accounts_view: AccountsView = HashMap::new();
let mut accounts_view: AccountsView = HashMap::with_capacity(account_views.len());
let mut i = 0;
for account in account_views {
if account.active {
Expand Down
4 changes: 2 additions & 2 deletions crates/rooch/src/commands/statedb/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ mod tests {

// all outpoints are unique
fn random_outpoints(n: usize) -> Vec<OutPoint> {
let mut outpoints = HashSet::new();
let mut outpoints = HashSet::with_capacity(n);
while outpoints.len() < n {
outpoints.insert(random_outpoint());
}
Expand All @@ -553,7 +553,7 @@ mod tests {

// all inscriptions are unique
fn random_inscriptions(n: usize) -> Vec<InscriptionID> {
let mut inscriptions = HashSet::new();
let mut inscriptions = HashSet::with_capacity(n);
while inscriptions.len() < n {
inscriptions.insert(random_inscription_id());
}
Expand Down
6 changes: 3 additions & 3 deletions frameworks/moveos-stdlib/src/natives/moveos_stdlib/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn parse_struct_value_from_json(
Err(anyhow::anyhow!("Invalid std option layout"))
} else if struct_type == &SimpleMap::<MoveString, MoveString>::struct_tag() {
let key_value_pairs = json_obj_to_key_value_pairs(json_value)?;
let mut key_values = Vec::new();
let mut key_values = Vec::with_capacity(key_value_pairs.len());
for (key, value) in key_value_pairs {
key_values.push(Value::struct_(Struct::pack(vec![
Value::struct_(Struct::pack(vec![Value::vector_u8(
Expand Down Expand Up @@ -243,7 +243,7 @@ fn parse_move_value_from_json(

fn json_obj_to_key_value_pairs(json_obj: &JsonValue) -> Result<Vec<(String, String)>> {
if let JsonValue::Object(obj) = json_obj {
let mut key_value_pairs = Vec::new();
let mut key_value_pairs = Vec::with_capacity(obj.len());
for (key, value) in obj.iter() {
let key = key.to_string();
let value = match value {
Expand Down Expand Up @@ -394,7 +394,7 @@ fn serialize_move_value_to_json(layout: &MoveTypeLayout, value: &MoveValue) -> R

JsonValue::Array(json_vec)
} else {
let mut json_vec = Vec::new();
let mut json_vec = Vec::with_capacity(vec.len());

for item in vec.iter() {
let json_value = serialize_move_value_to_json(layout, item)?;
Expand Down
2 changes: 1 addition & 1 deletion frameworks/rooch-nursery/src/natives/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ fn execute_wasm_function_inner(
gas_meter.reset();
drop(gas_meter);

let mut wasm_func_args = Vec::new();
let mut wasm_func_args = Vec::with_capacity(func_args.len());
for arg in func_args.iter() {
wasm_func_args.push(wasmer::Value::I32(*arg as i32));
}
Expand Down
4 changes: 2 additions & 2 deletions moveos/raw-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,13 @@ where
}

pub fn new_puts(kvs: Vec<(K, V)>) -> Self {
let mut rows = Vec::new();
let mut rows = Vec::with_capacity(kvs.len());
rows.extend(kvs.into_iter().map(|(k, v)| (k, WriteOp::Value(v))));
Self { rows }
}

pub fn new_deletes(ks: Vec<K>) -> Self {
let mut rows = Vec::new();
let mut rows = Vec::with_capacity(ks.len());
rows.extend(ks.into_iter().map(|k| (k, WriteOp::Deletion)));
Self { rows }
}
Expand Down

0 comments on commit d16aa66

Please sign in to comment.