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

refactor(memory): memory usage improvements #2098

Merged
merged 4 commits into from
May 7, 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
7 changes: 4 additions & 3 deletions mm2src/adex_cli/src/scenarios/mm2_proc_mng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,11 @@ pub(crate) fn get_status() {
.filter(|line| line.contains("PID"))
.last()
{
let pid = found
.trim()
let chars = found.trim();

let pid = chars
.matches(char::is_numeric)
.fold(String::default(), |mut pid, ch| {
.fold(String::with_capacity(chars.len()), |mut pid, ch| {
pid.push_str(ch);
pid
});
Expand Down
13 changes: 9 additions & 4 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,10 @@ impl EthCoinImpl {

/// The id used to differentiate payments on Etomic swap smart contract
pub(crate) fn etomic_swap_id(&self, time_lock: u32, secret_hash: &[u8]) -> Vec<u8> {
let mut input = vec![];
input.extend_from_slice(&time_lock.to_le_bytes());
let timelock_bytes = time_lock.to_le_bytes();

let mut input = Vec::with_capacity(timelock_bytes.len() + secret_hash.len());
input.extend_from_slice(&timelock_bytes);
input.extend_from_slice(secret_hash);
sha256(&input).to_vec()
}
Expand Down Expand Up @@ -4082,8 +4084,11 @@ impl EthCoin {
address: Address,
) -> Result<CoinBalanceMap, MmError<BalanceError>> {
let coin = || self;
let mut requests = Vec::new();
for (token_ticker, info) in self.get_erc_tokens_infos() {

let tokens = self.get_erc_tokens_infos();
let mut requests = Vec::with_capacity(tokens.len());

for (token_ticker, info) in tokens {
let fut = async move {
let balance_as_u256 = coin()
.get_token_balance_for_address(address, info.token_address)
Expand Down
4 changes: 1 addition & 3 deletions mm2src/coins/lightning/ln_p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ fn netaddress_from_ipaddr(addr: IpAddr, port: u16) -> Vec<NetAddress> {
if addr == Ipv4Addr::new(0, 0, 0, 0) || addr == Ipv4Addr::new(127, 0, 0, 1) {
return Vec::new();
}
let mut addresses = Vec::new();
let address = match addr {
IpAddr::V4(addr) => NetAddress::IPv4 {
addr: u32::from(addr).to_be_bytes(),
Expand All @@ -121,8 +120,7 @@ fn netaddress_from_ipaddr(addr: IpAddr, port: u16) -> Vec<NetAddress> {
port,
},
};
addresses.push(address);
addresses
vec![address]
}

pub async fn ln_node_announcement_loop(
Expand Down
4 changes: 2 additions & 2 deletions mm2src/coins/lightning/ln_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ mod tests {

fn generate_random_channels(num: u64) -> Vec<DBChannelDetails> {
let mut rng = rand::thread_rng();
let mut channels = vec![];
let mut channels = Vec::with_capacity(num.try_into().expect("Shouldn't overflow."));
let s = Secp256k1::new();
let mut bytes = [0; 32];
for _i in 0..num {
Expand Down Expand Up @@ -1108,7 +1108,7 @@ mod tests {

fn generate_random_payments(num: u64) -> Vec<PaymentInfo> {
let mut rng = rand::thread_rng();
let mut payments = vec![];
let mut payments = Vec::with_capacity(num.try_into().expect("Shouldn't overflow."));
let s = Secp256k1::new();
let mut bytes = [0; 32];
for _ in 0..num {
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/lightning/ln_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub async fn init_channel_manager(
let (channel_manager_blockhash, channel_manager, channelmonitors) = async_blocking(move || {
let mut manager_file = File::open(persister.manager_path())?;

let mut channel_monitor_mut_references = Vec::new();
let mut channel_monitor_mut_references = Vec::with_capacity(channelmonitors.len());
for (_, channel_monitor) in channelmonitors.iter_mut() {
channel_monitor_mut_references.push(channel_monitor);
}
Expand Down
4 changes: 3 additions & 1 deletion mm2src/coins/nft/storage/wasm/wasm_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ where
I: Iterator<Item = NftListTable>,
{
let mut filtered_nfts = Vec::new();

for nft_table in nfts {
let nft = nft_details_from_item(nft_table)?;
match filters {
Expand Down Expand Up @@ -82,6 +83,7 @@ where
I: Iterator<Item = NftTransferHistoryTable>,
{
let mut filtered_transfers = Vec::new();

for transfers_table in transfers {
let transfer = transfer_details_from_item(transfers_table)?;
match filters {
Expand Down Expand Up @@ -705,7 +707,7 @@ impl NftTransferHistoryStorageOps for NftCacheIDBLocked<'_> {
let table = db_transaction.table::<NftTransferHistoryTable>().await?;

let items = table.get_items("chain", chain.to_string()).await?;
let mut token_addresses = HashSet::new();
let mut token_addresses = HashSet::with_capacity(items.len());
for (_item_id, item) in items.into_iter() {
let transfer = transfer_details_from_item(item)?;
token_addresses.insert(transfer.common.token_address);
Expand Down
6 changes: 4 additions & 2 deletions mm2src/coins/qrc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,8 +1503,10 @@ impl MmCoin for Qrc20Coin {
}

pub fn qrc20_swap_id(time_lock: u32, secret_hash: &[u8]) -> Vec<u8> {
let mut input = vec![];
input.extend_from_slice(&time_lock.to_le_bytes());
let timelock_bytes = time_lock.to_le_bytes();
let mut input = Vec::with_capacity(timelock_bytes.len() + secret_hash.len());

input.extend_from_slice(&timelock_bytes);
input.extend_from_slice(secret_hash);
sha256(&input).to_vec()
}
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/tendermint/tendermint_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ impl TendermintCommons for TendermintCoin {
let platform_balance = big_decimal_from_sat_unsigned(platform_balance_denom, self.decimals);
let ibc_assets_info = self.tokens_info.lock().clone();

let mut requests = Vec::new();
let mut requests = Vec::with_capacity(ibc_assets_info.len());
for (denom, info) in ibc_assets_info {
let fut = async move {
let balance_denom = self
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo/rpc_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2172,7 +2172,7 @@ impl ElectrumClient {
Ok(headers) => headers,
Err(e) => return MmError::err(UtxoRpcError::InvalidResponse(format!("{:?}", e))),
};
let mut block_registry: HashMap<u64, BlockHeader> = HashMap::new();
let mut block_registry: HashMap<u64, BlockHeader> = HashMap::with_capacity(block_headers.len());
let mut starting_height = from_height;
for block_header in &block_headers {
block_registry.insert(starting_height, block_header.clone());
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo/utxo_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2706,7 +2706,7 @@ async fn get_unspents_for_inputs(
.map_err(|err| RawTransactionError::InvalidParam(err.to_string()))?;

let mut prev_script = None;
let mut unspents_loaded = vec![];
let mut unspents_loaded = Vec::with_capacity(inputs.len());

for input in inputs {
let prev_tx = prev_txns_loaded
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/utxo_signer/src/with_key_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn sign_tx(
signature_version: SignatureVersion,
fork_id: u32,
) -> UtxoSignWithKeyPairResult<UtxoTx> {
let mut signed_inputs = vec![];
let mut signed_inputs = Vec::with_capacity(unsigned.inputs.len());
match signature_version {
SignatureVersion::WitnessV0 => {
for (i, _) in unsigned.inputs.iter().enumerate() {
Expand Down
4 changes: 2 additions & 2 deletions mm2src/coins/z_coin/storage/walletdb/wasm/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ impl WalletRead for WalletIndexedDb {
let accounts_table = db_transaction.table::<WalletDbAccountsTable>().await?;
let maybe_accounts = accounts_table.get_items("ticker", &self.ticker).await?;

let mut res_accounts: HashMap<AccountId, ExtendedFullViewingKey> = HashMap::new();
let mut res_accounts: HashMap<AccountId, ExtendedFullViewingKey> = HashMap::with_capacity(maybe_accounts.len());
for (_, account) in maybe_accounts {
let extfvk =
decode_extended_full_viewing_key(self.params.hrp_sapling_extended_full_viewing_key(), &account.extfvk)
Expand Down Expand Up @@ -1053,7 +1053,7 @@ impl WalletRead for WalletIndexedDb {

// Retrieves a list of transaction IDs (id_tx) from the transactions table
// that match the provided account ID and have not been spent (spent IS NULL).
let mut witnesses = vec![];
let mut witnesses = Vec::with_capacity(maybe_witnesses.len());
for (_, witness) in maybe_witnesses {
let id_note = witness.note.to_i64().unwrap();
let id_note = NoteId::ReceivedNoteId(id_note.to_i64().expect("invalid value"));
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/z_coin/z_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ impl ZRpcOps for NativeClient {
for height in start_block..=last_block {
let block = self.get_block_by_height(height).await?;
debug!("Got block {:?}", block);
let mut compact_txs = Vec::new();
let mut compact_txs = Vec::with_capacity(block.tx.len());
// By default, CompactBlocks only contain CompactTxs for transactions that contain Sapling spends or outputs.
// Create and push compact_tx during iteration.
for (tx_id, hash_tx) in block.tx.iter().enumerate() {
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/z_coin/z_tx_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub(crate) async fn fetch_tx_history_from_db(
.await?;

// Process transactions and construct tx_details
let mut tx_details = vec![];
let mut tx_details = Vec::new();
for (tx_id, tx) in txs {
if let Some((_, WalletDbBlocksTable { height, time, .. })) = blocks
.iter()
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins_activation/src/eth_with_token_activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl TokenInitializer for Erc20Initializer {
&self,
activation_params: Vec<TokenActivationParams<Erc20TokenActivationRequest, Erc20Protocol>>,
) -> Result<Vec<EthCoin>, MmError<EthTokenActivationError>> {
let mut tokens = vec![];
let mut tokens = Vec::with_capacity(activation_params.len());
for param in activation_params {
let token: EthCoin = self
.platform_coin
Expand Down
3 changes: 1 addition & 2 deletions mm2src/coins_activation/src/slp_token_activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ impl TokenActivationOps for SlpToken {
)?;
let balance = token.my_coin_balance().await.mm_err(EnableSlpError::GetBalanceError)?;
let my_address = token.my_address()?;
let mut balances = HashMap::new();
balances.insert(my_address, balance);
let balances = HashMap::from([(my_address, balance)]);
let init_result = SlpInitResult {
balances,
token_id: (*token.token_id()).into(),
Expand Down
3 changes: 1 addition & 2 deletions mm2src/coins_activation/src/spl_token_activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ impl TokenActivationOps for SplToken {
.await
.map_err(|e| SplInitError::GetBalanceError(e.into_inner()))?;
let my_address = token.my_address()?;
let mut balances = HashMap::new();
balances.insert(my_address, balance);
let balances = HashMap::from([(my_address, balance)]);
let init_result = SplInitResult {
balances,
token_contract_address: token.conf.token_contract_address.to_string(),
Expand Down
3 changes: 1 addition & 2 deletions mm2src/coins_activation/src/tendermint_token_activation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ impl TokenActivationOps for TendermintToken {
.mm_err(|e| TendermintTokenInitError::CouldNotFetchBalance(e.to_string()))?;

let my_address = token.my_address()?;
let mut balances = HashMap::new();
balances.insert(my_address, balance);
let balances = HashMap::from([(my_address, balance)]);

let init_result = TendermintTokenInitResult {
balances,
Expand Down
3 changes: 1 addition & 2 deletions mm2src/common/shared_ref_counter/src/enable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ impl<T> SharedRc<T> {
#[track_caller]
pub fn new(inner: T) -> Self {
let index = 0;
let mut existing_pointers = HashMap::new();
existing_pointers.insert(index, Location::caller());
let existing_pointers = HashMap::from([(index, Location::caller())]);

Self {
inner: Arc::new(inner),
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_bitcoin/keys/src/cashaddress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ mod base32 {
/// `encode` converts an input byte array into a base32 string.
/// It expects the byte array to be 5-bit packed.
pub fn encode(input: &[u8]) -> Result<String, String> {
let mut output = String::new();
let mut output = String::with_capacity(input.len());

for i in input {
let i = *i as usize;
Expand All @@ -380,7 +380,7 @@ mod base32 {
/// `decode` takes a string in base32 format and returns a byte array that is
/// 5-bit packed.
pub fn decode(input: &str) -> Result<Vec<u8>, String> {
let mut output = Vec::new();
let mut output = Vec::with_capacity(input.len());
for c in input.chars() {
let cpos = c as usize;
if cpos >= CHARSET_REV.len() {
Expand Down
9 changes: 3 additions & 6 deletions mm2src/mm2_bitcoin/spv_validation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub(crate) mod test_utils {

use self::serde::Deserialize;

use std::{fs::File, io::Read, panic, string::String, vec, vec::Vec};
use std::{panic, vec, vec::Vec};

#[derive(Deserialize)]
pub(crate) struct TestCase {
Expand All @@ -40,11 +40,8 @@ pub(crate) mod test_utils {
}

fn setup() -> serde_json::Value {
let mut file = File::open("./src/for_tests/spvTestVectors.json").unwrap();
let mut data = String::new();
file.read_to_string(&mut data).unwrap();

serde_json::from_str(&data).unwrap()
let data = include_str!("./for_tests/spvTestVectors.json");
serde_json::from_str(data).unwrap()
}

fn to_test_case(val: &serde_json::Value) -> TestCase {
Expand Down
8 changes: 2 additions & 6 deletions mm2src/mm2_main/src/lp_ordermatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2275,16 +2275,12 @@ fn broadcast_keep_alive_for_pub(ctx: &MmArc, pubkey: &str, orderbook: &Orderbook
};

for (alb_pair, root) in state.trie_roots.iter() {
let mut trie_roots = HashMap::new();

if *root == H64::default() && *root == hashed_null_node::<Layout>() {
continue;
}

trie_roots.insert(alb_pair.clone(), *root);

let message = new_protocol::PubkeyKeepAlive {
trie_roots,
trie_roots: HashMap::from([(alb_pair.clone(), *root)]),
timestamp: now_sec(),
};

Expand Down Expand Up @@ -5383,12 +5379,12 @@ pub struct HistoricalOrder {
}

pub async fn orders_kick_start(ctx: &MmArc) -> Result<HashSet<String>, String> {
let mut coins = HashSet::new();
let ordermatch_ctx = try_s!(OrdermatchContext::from_ctx(ctx));

let storage = MyOrdersStorage::new(ctx.clone());
let saved_maker_orders = try_s!(storage.load_active_maker_orders().await);
let saved_taker_orders = try_s!(storage.load_active_taker_orders().await);
let mut coins = HashSet::with_capacity((saved_maker_orders.len() * 2) + (saved_taker_orders.len() * 2));

{
let mut maker_orders_ctx = ordermatch_ctx.maker_orders_ctx.lock();
Expand Down
6 changes: 3 additions & 3 deletions mm2src/mm2_main/src/lp_ordermatch/orderbook_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub async fn orderbook_rpc(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>, S

let mut asks = match orderbook.unordered.get(&(base_ticker.clone(), rel_ticker.clone())) {
Some(uuids) => {
let mut orderbook_entries = Vec::new();
let mut orderbook_entries = Vec::with_capacity(uuids.len());
for uuid in uuids {
let ask = orderbook.order_set.get(uuid).ok_or(ERRL!(
"Orderbook::unordered contains {:?} uuid that is not in Orderbook::order_set",
Expand All @@ -114,15 +114,15 @@ pub async fn orderbook_rpc(ctx: MmArc, req: Json) -> Result<Response<Vec<u8>>, S
}
orderbook_entries
},
None => Vec::new(),
None => vec![],
};
asks.sort_unstable_by(|ask1, ask2| ask1.price_rat.cmp(&ask2.price_rat));
let (mut asks, total_asks_base_vol, total_asks_rel_vol) = build_aggregated_entries(asks);
asks.reverse();

let mut bids = match orderbook.unordered.get(&(rel_ticker, base_ticker)) {
Some(uuids) => {
let mut orderbook_entries = vec![];
let mut orderbook_entries = Vec::with_capacity(uuids.len());
for uuid in uuids {
let bid = orderbook.order_set.get(uuid).ok_or(ERRL!(
"Orderbook::unordered contains {:?} uuid that is not in Orderbook::order_set",
Expand Down
3 changes: 1 addition & 2 deletions mm2src/mm2_main/src/lp_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ pub async fn start_version_stat_collection(ctx: MmArc, req: Json) -> NodeVersion
let relay_addr = RelayAddress::from_str(&address)?;
let multi_address = relay_addr.try_to_multiaddr(network_info)?;

let mut addresses = HashSet::new();
addresses.insert(multi_address);
let addresses = HashSet::from([multi_address]);
add_reserved_peer_addresses(&ctx, peer_id, addresses);
}

Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_main/src/lp_swap/maker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ impl MakerSwap {
};
drop(send_abort_handle);

let mut swap_events = vec![];
let mut swap_events = Vec::with_capacity(2);
let instructions = match payload.instructions() {
Some(instructions) => {
let maker_lock_duration =
Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_main/src/lp_swap/taker_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ impl TakerSwap {
};
drop(abort_send_handle);

let mut swap_events = vec![];
let mut swap_events = Vec::with_capacity(3);
let instructions = match payload.instructions() {
Some(instructions) => {
match self
Expand Down
Loading
Loading