Skip to content

Commit

Permalink
refactor: use ahash where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Jun 11, 2024
1 parent c379d71 commit f4b5066
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 65 deletions.
10 changes: 5 additions & 5 deletions cli/src/tools/gen_zerostate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
use std::io::IsTerminal;
use std::path::PathBuf;
use std::sync::OnceLock;
Expand All @@ -10,6 +9,7 @@ use everscale_types::num::Tokens;
use everscale_types::prelude::*;
use serde::{Deserialize, Serialize};
use sha2::Digest;
use tycho_util::FastHashMap;

use crate::util::compute_storage_used;
use crate::util::error::ResultExt;
Expand Down Expand Up @@ -127,7 +127,7 @@ struct ZerostateConfig {
elector_balance: Tokens,

#[serde(with = "serde_account_states")]
accounts: HashMap<HashBytes, OptionalAccount>,
accounts: FastHashMap<HashBytes, OptionalAccount>,

validators: Vec<ed25519::PublicKey>,

Expand Down Expand Up @@ -801,7 +801,7 @@ mod serde_account_states {
use super::*;

pub fn serialize<S>(
value: &HashMap<HashBytes, OptionalAccount>,
value: &FastHashMap<HashBytes, OptionalAccount>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
Expand All @@ -820,15 +820,15 @@ mod serde_account_states {

pub fn deserialize<'de, D>(
deserializer: D,
) -> Result<HashMap<HashBytes, OptionalAccount>, D::Error>
) -> Result<FastHashMap<HashBytes, OptionalAccount>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[repr(transparent)]
struct WrappedValue(#[serde(with = "BocRepr")] OptionalAccount);

<HashMap<HashBytes, WrappedValue>>::deserialize(deserializer)
<FastHashMap<HashBytes, WrappedValue>>::deserialize(deserializer)
.map(|map| map.into_iter().map(|(k, v)| (k, v.0)).collect())
}
}
7 changes: 4 additions & 3 deletions collator/src/collator/do_collate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{HashMap, VecDeque};
use std::collections::VecDeque;
use std::ops::Deref;
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -9,6 +9,7 @@ use everscale_types::num::Tokens;
use everscale_types::prelude::*;
use humantime::format_duration;
use sha2::Digest;
use tycho_util::FastHashMap;

use super::types::CachedMempoolAnchor;
use super::CollatorStdImpl;
Expand Down Expand Up @@ -93,7 +94,7 @@ impl CollatorStdImpl {
.ok()
.map(|(shard_id, shard_descr)| (shard_id, Box::new(shard_descr)))
})
.collect::<HashMap<_, _>>();
.collect::<FastHashMap<_, _>>();

collation_data.set_shards(shards);

Expand Down Expand Up @@ -1154,7 +1155,7 @@ impl CollatorStdImpl {

pub fn update_shard_block_info(
&self,
shardes: &mut HashMap<ShardIdent, Box<ShardDescription>>,
shardes: &mut FastHashMap<ShardIdent, Box<ShardDescription>>,
shard_id: ShardIdent,
shard_description: Box<ShardDescription>,
) -> Result<()> {
Expand Down
15 changes: 7 additions & 8 deletions collator/src/collator/execution_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::cmp;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::sync::OnceLock;

use anyhow::Result;
Expand Down Expand Up @@ -44,7 +43,7 @@ pub(super) struct ExecutionManager {
/// block version
block_version: u32,
/// messages groups
messages_groups: HashMap<u32, HashMap<HashBytes, Vec<AsyncMessage>>>,
messages_groups: FastHashMap<u32, FastHashMap<HashBytes, Vec<AsyncMessage>>>,
/// group limit
group_limit: usize,
/// group vertical size
Expand Down Expand Up @@ -81,7 +80,7 @@ impl ExecutionManager {
group_limit,
group_vert_size,
total_trans_duration: 0,
messages_groups: HashMap::new(),
messages_groups: FastHashMap::default(),
shard_accounts,
changed_accounts: FastHashMap::default(),
}
Expand Down Expand Up @@ -400,13 +399,13 @@ pub fn calculate_group(
messages_set: &[AsyncMessage],
group_limit: u32,
offset: u32,
) -> (u32, HashMap<AccountId, AsyncMessage>) {
) -> (u32, FastHashMap<AccountId, AsyncMessage>) {
let mut new_offset = offset;
let mut holes_group: HashMap<AccountId, u32> = HashMap::new();
let mut holes_group: FastHashMap<AccountId, u32> = FastHashMap::default();
let mut max_account_count = 0;
let mut holes_max_count: i32 = 0;
let mut holes_count: i32 = 0;
let mut group = HashMap::new();
let mut group = FastHashMap::default();
for (i, msg) in messages_set.iter().enumerate() {
let account_id = match msg {
AsyncMessage::Ext(MsgInfo::ExtIn(ExtInMsgInfo { ref dst, .. }), _) => {
Expand Down Expand Up @@ -489,8 +488,8 @@ pub fn pre_calculate_groups(
messages_set: Vec<AsyncMessage>,
group_limit: usize,
group_vert_size: usize,
) -> HashMap<u32, HashMap<AccountId, Vec<AsyncMessage>>> {
let mut res: HashMap<u32, HashMap<AccountId, Vec<AsyncMessage>>> = HashMap::new();
) -> FastHashMap<u32, FastHashMap<AccountId, Vec<AsyncMessage>>> {
let mut res: FastHashMap<u32, FastHashMap<AccountId, Vec<AsyncMessage>>> = Default::default();
for msg in messages_set {
let account_id = match msg {
AsyncMessage::Ext(MsgInfo::ExtIn(ExtInMsgInfo { ref dst, .. }), _) => {
Expand Down
13 changes: 7 additions & 6 deletions collator/src/collator/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;
use std::sync::Arc;

use anyhow::{anyhow, bail, Result};
Expand All @@ -12,6 +12,7 @@ use everscale_types::models::{
Transaction, ValueFlow,
};
use tycho_block_util::state::{MinRefMcStateTracker, ShardStateStuff};
use tycho_util::FastHashMap;

use crate::mempool::MempoolAnchor;
use crate::types::ProofFunds;
Expand Down Expand Up @@ -335,7 +336,7 @@ pub(super) struct BlockCollationData {
/// Ids of top blocks from shards that be included in the master block
pub top_shard_blocks_ids: Vec<BlockId>,

shards: Option<HashMap<ShardIdent, Box<ShardDescription>>>,
shards: Option<FastHashMap<ShardIdent, Box<ShardDescription>>>,
shards_max_end_lt: u64,

// TODO: setup update logic when ShardFees would be implemented
Expand All @@ -350,22 +351,22 @@ pub(super) struct BlockCollationData {

pub rand_seed: HashBytes,

pub block_create_count: HashMap<HashBytes, u64>,
pub block_create_count: FastHashMap<HashBytes, u64>,

// TODO: set from anchor
pub created_by: HashBytes,
}

impl BlockCollationData {
pub fn shards(&self) -> Result<&HashMap<ShardIdent, Box<ShardDescription>>> {
pub fn shards(&self) -> Result<&FastHashMap<ShardIdent, Box<ShardDescription>>> {
self.shards
.as_ref()
.ok_or_else(|| anyhow!("`shards` is not initialized yet"))
}
pub fn set_shards(&mut self, shards: HashMap<ShardIdent, Box<ShardDescription>>) {
pub fn set_shards(&mut self, shards: FastHashMap<ShardIdent, Box<ShardDescription>>) {
self.shards = Some(shards);
}
pub fn shards_mut(&mut self) -> Result<&mut HashMap<ShardIdent, Box<ShardDescription>>> {
pub fn shards_mut(&mut self) -> Result<&mut FastHashMap<ShardIdent, Box<ShardDescription>>> {
self.shards
.as_mut()
.ok_or_else(|| anyhow!("`shards` is not initialized yet"))
Expand Down
16 changes: 8 additions & 8 deletions collator/src/internal_queue/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cmp::Reverse;
use std::collections::{BinaryHeap, HashMap};
use std::collections::BinaryHeap;
use std::sync::Arc;

use anyhow::{bail, Result};
Expand Down Expand Up @@ -28,10 +28,10 @@ pub trait QueueIterator: Send {

pub struct QueueIteratorImpl {
for_shard: ShardIdent,
current_position: HashMap<ShardIdent, InternalMessageKey>,
commited_current_position: HashMap<ShardIdent, InternalMessageKey>,
current_position: FastHashMap<ShardIdent, InternalMessageKey>,
commited_current_position: FastHashMap<ShardIdent, InternalMessageKey>,
messages_for_current_shard: BinaryHeap<Reverse<Arc<MessageWithSource>>>,
new_messages: HashMap<InternalMessageKey, Arc<EnqueuedMessage>>,
new_messages: FastHashMap<InternalMessageKey, Arc<EnqueuedMessage>>,
snapshot_manager: StatesIteratorsManager,
}

Expand Down Expand Up @@ -59,7 +59,7 @@ pub struct IterItem {
}

fn update_shard_range(
touched_shards: &mut HashMap<ShardIdent, ShardRange>,
touched_shards: &mut FastHashMap<ShardIdent, ShardRange>,
shard_id: ShardIdent,
from_lt: Option<Lt>,
to_lt: Option<Lt>,
Expand Down Expand Up @@ -205,8 +205,8 @@ impl QueueIteratorExt {
pub fn collect_ranges(
shards_from: FastHashMap<ShardIdent, u64>,
shards_to: FastHashMap<ShardIdent, u64>,
) -> HashMap<ShardIdent, ShardRange> {
let mut shards_with_ranges = HashMap::new();
) -> FastHashMap<ShardIdent, ShardRange> {
let mut shards_with_ranges = FastHashMap::default();
for from in shards_from {
for to in &shards_to {
let iter_range_from = IterRange {
Expand All @@ -229,7 +229,7 @@ impl QueueIteratorExt {
}

pub fn traverse_and_collect_ranges(
touched_shards: &mut HashMap<ShardIdent, ShardRange>,
touched_shards: &mut FastHashMap<ShardIdent, ShardRange>,
from_range: &IterRange,
to_range: &IterRange,
) {
Expand Down
6 changes: 3 additions & 3 deletions collator/src/internal_queue/queue.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashMap;
use std::sync::Arc;

use everscale_types::models::{BlockIdShort, ShardIdent};
use tokio::sync::{Mutex, RwLock};
use tycho_util::FastHashMap;

use crate::internal_queue::error::QueueError;
use crate::internal_queue::state::persistent::persistent_state::{
Expand Down Expand Up @@ -49,7 +49,7 @@ pub struct QueueFactoryStdImpl {
pub trait LocalQueue {
async fn iterator(
&self,
ranges: &HashMap<ShardIdent, ShardRange>,
ranges: &FastHashMap<ShardIdent, ShardRange>,
for_shard_id: ShardIdent,
) -> Vec<Box<dyn StateIterator>>;
async fn split_shard(&self, shard_id: &ShardIdent) -> Result<(), QueueError>;
Expand Down Expand Up @@ -101,7 +101,7 @@ where
{
async fn iterator(
&self,
ranges: &HashMap<ShardIdent, ShardRange>,
ranges: &FastHashMap<ShardIdent, ShardRange>,
for_shard_id: ShardIdent,
) -> Vec<Box<dyn StateIterator>> {
let session_iter = {
Expand Down
12 changes: 6 additions & 6 deletions collator/src/internal_queue/state/session/session_state.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::collections::HashMap;
use std::sync::Arc;

use everscale_types::models::{BlockIdShort, ShardIdent};
use tokio::sync::RwLock;
use tycho_util::FastHashMap;

use crate::internal_queue::error::QueueError;
use crate::internal_queue::shard::Shard;
Expand Down Expand Up @@ -54,7 +54,7 @@ pub trait LocalSessionState {
fn new(shards: &[ShardIdent]) -> Self;
async fn iterator(
&self,
ranges: &HashMap<ShardIdent, ShardRange>,
ranges: &FastHashMap<ShardIdent, ShardRange>,
for_shard_id: ShardIdent,
) -> Box<dyn StateIterator>;
async fn split_shard(&self, shard_ident: &ShardIdent) -> Result<(), QueueError>;
Expand All @@ -78,12 +78,12 @@ pub trait LocalSessionState {
// IMPLEMENTATION

pub struct SessionStateStdImpl {
shards_flat: RwLock<HashMap<ShardIdent, Arc<RwLock<Shard>>>>,
shards_flat: RwLock<FastHashMap<ShardIdent, Arc<RwLock<Shard>>>>,
}

impl SessionState for SessionStateStdImpl {
fn new(shards: &[ShardIdent]) -> Self {
let mut shards_flat = HashMap::new();
let mut shards_flat = FastHashMap::default();
for &shard in shards {
shards_flat.insert(shard, Arc::new(RwLock::new(Shard::default())));
}
Expand All @@ -94,11 +94,11 @@ impl SessionState for SessionStateStdImpl {

async fn iterator(
&self,
ranges: &HashMap<ShardIdent, ShardRange>,
ranges: &FastHashMap<ShardIdent, ShardRange>,
for_shard_id: ShardIdent,
) -> Box<dyn StateIterator> {
let shards_flat_read = self.shards_flat.read().await;
let mut flat_shards = HashMap::new();
let mut flat_shards = FastHashMap::default();
for (shard_ident, shard_lock) in shards_flat_read.iter() {
let shard = shard_lock.read().await;
flat_shards.insert(*shard_ident, shard.clone());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::collections::{HashMap, VecDeque};
use std::collections::VecDeque;
use std::sync::Arc;

use anyhow::Result;
use everscale_types::cell::HashBytes;
use everscale_types::models::ShardIdent;
use tycho_util::FastHashMap;

use crate::internal_queue::shard::Shard;
use crate::internal_queue::state::state_iterator::{MessageWithSource, ShardRange, StateIterator};
Expand All @@ -16,8 +17,8 @@ pub struct SessionStateIterator {

impl SessionStateIterator {
pub fn new(
flat_shards: HashMap<ShardIdent, Shard>,
shard_ranges: &HashMap<ShardIdent, ShardRange>,
flat_shards: FastHashMap<ShardIdent, Shard>,
shard_ranges: &FastHashMap<ShardIdent, ShardRange>,
shard_id: &ShardIdent,
) -> Self {
let queue = Self::prepare_queue(flat_shards, shard_ranges, shard_id);
Expand All @@ -27,8 +28,8 @@ impl SessionStateIterator {
}

fn prepare_queue(
flat_shards: HashMap<ShardIdent, Shard>,
shard_ranges: &HashMap<ShardIdent, ShardRange>,
flat_shards: FastHashMap<ShardIdent, Shard>,
shard_ranges: &FastHashMap<ShardIdent, ShardRange>,
shard_id: &ShardIdent,
) -> VecDeque<Arc<MessageWithSource>> {
let mut message_queue = VecDeque::new();
Expand Down
4 changes: 2 additions & 2 deletions collator/src/internal_queue/types.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::cmp::Ordering;
use std::collections::HashMap;
use std::sync::Arc;

use anyhow::{bail, Result};
use everscale_types::cell::{Cell, HashBytes};
use everscale_types::models::{IntAddr, IntMsgInfo, ShardIdent};
use tycho_util::FastHashMap;

pub type Lt = u64;

#[derive(Default, Debug, Clone)]
pub struct QueueDiff {
pub messages: Vec<Arc<EnqueuedMessage>>,
pub processed_upto: HashMap<ShardIdent, InternalMessageKey>,
pub processed_upto: FastHashMap<ShardIdent, InternalMessageKey>,
}

#[derive(Debug, Clone)]
Expand Down
Loading

0 comments on commit f4b5066

Please sign in to comment.