Skip to content

Commit

Permalink
chore: refactor out the shrink_hashmap (#5492)
Browse files Browse the repository at this point in the history
Description
---
Refactor shrink map to a separate file. And also shrink it to the size
that it will not be shrink again on next call.

Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->
  • Loading branch information
Cifko authored Jun 25, 2023
1 parent 4a9f73c commit 59d1d8e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
1 change: 1 addition & 0 deletions base_layer/core/src/mempool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub use rpc::create_mempool_rpc_service;
pub use rpc::{MempoolRpcClient, MempoolRpcServer, MempoolRpcService, MempoolService};
#[cfg(feature = "base_node")]
mod metrics;
mod shrink_hashmap;
#[cfg(feature = "base_node")]
mod unconfirmed_pool;

Expand Down
17 changes: 5 additions & 12 deletions base_layer/core/src/mempool/reorg_pool/reorg_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

use std::{
collections::{HashMap, HashSet},
hash::Hash,
sync::Arc,
};

Expand All @@ -31,7 +30,11 @@ use serde::{Deserialize, Serialize};
use tari_common_types::types::{PrivateKey, Signature};
use tari_utilities::hex::Hex;

use crate::{blocks::Block, transactions::transaction_components::Transaction};
use crate::{
blocks::Block,
mempool::shrink_hashmap::shrink_hashmap,
transactions::transaction_components::Transaction,
};

pub const LOG_TARGET: &str = "c::mp::reorg_pool::reorg_pool_storage";

Expand Down Expand Up @@ -317,16 +320,6 @@ impl ReorgPool {
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_sign_loss)]
pub fn compact(&mut self) {
fn shrink_hashmap<K: Eq + Hash, V>(map: &mut HashMap<K, V>) -> (usize, usize) {
let cap = map.capacity();
let extra_cap = cap - map.len();
if extra_cap > 100 {
map.shrink_to(map.len() + (extra_cap / 2));
}

(cap, map.capacity())
}

let (old, new) = shrink_hashmap(&mut self.tx_by_key);
shrink_hashmap(&mut self.txs_by_signature);
shrink_hashmap(&mut self.txs_by_height);
Expand Down
33 changes: 33 additions & 0 deletions base_layer/core/src/mempool/shrink_hashmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2019 The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use std::{collections::HashMap, hash::Hash};

pub fn shrink_hashmap<K: Eq + Hash, V>(map: &mut HashMap<K, V>) -> (usize, usize) {
let cap = map.capacity();
let extra_cap = cap - map.len();
if extra_cap > 100 {
map.shrink_to(map.len() + 100);
}

(cap, map.capacity())
}
13 changes: 2 additions & 11 deletions base_layer/core/src/mempool/unconfirmed_pool/unconfirmed_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

use std::{
collections::{BTreeMap, HashMap, HashSet},
hash::Hash,
sync::Arc,
};

Expand All @@ -35,11 +34,13 @@ use crate::{
blocks::Block,
mempool::{
priority::{FeePriority, PrioritizedTransaction},
shrink_hashmap::shrink_hashmap,
unconfirmed_pool::UnconfirmedPoolError,
FeePerGramStat,
},
transactions::{tari_amount::MicroTari, transaction_components::Transaction, weight::TransactionWeight},
};

pub const LOG_TARGET: &str = "c::mp::unconfirmed_pool::unconfirmed_pool_storage";

type TransactionKey = usize;
Expand Down Expand Up @@ -604,16 +605,6 @@ impl UnconfirmedPool {
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_sign_loss)]
pub fn compact(&mut self) {
fn shrink_hashmap<K: Eq + Hash, V>(map: &mut HashMap<K, V>) -> (usize, usize) {
let cap = map.capacity();
let extra_cap = cap - map.len();
if extra_cap > 100 {
map.shrink_to(map.len() + (extra_cap / 2));
}

(cap, map.capacity())
}

let (old, new) = shrink_hashmap(&mut self.tx_by_key);
shrink_hashmap(&mut self.txs_by_signature);
shrink_hashmap(&mut self.txs_by_output);
Expand Down

0 comments on commit 59d1d8e

Please sign in to comment.