Skip to content

Commit

Permalink
chore: remove some excessive allocs in hot path (paradigmxyz#13176)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Dec 6, 2024
1 parent fdff4f1 commit cb3e9f8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 21 deletions.
11 changes: 2 additions & 9 deletions crates/net/network/src/transactions/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ impl<N: NetworkPrimitives> TransactionFetcher<N> {
+ IntoIterator<Item = (TxHash, Option<(u8, usize)>)>,
) -> RequestTxHashes {
let mut acc_size_response = 0;
let hashes_from_announcement_len = hashes_from_announcement.len();

let mut hashes_from_announcement_iter = hashes_from_announcement.into_iter();

Expand All @@ -292,7 +291,7 @@ impl<N: NetworkPrimitives> TransactionFetcher<N> {
acc_size_response = size;
}

let mut surplus_hashes = RequestTxHashes::with_capacity(hashes_from_announcement_len - 1);
let mut surplus_hashes = RequestTxHashes::default();

// folds size based on expected response size and adds selected hashes to the request
// list and the other hashes to the surplus list
Expand Down Expand Up @@ -326,8 +325,6 @@ impl<N: NetworkPrimitives> TransactionFetcher<N> {
}

surplus_hashes.extend(hashes_from_announcement_iter.map(|(hash, _metadata)| hash));
surplus_hashes.shrink_to_fit();
hashes_to_request.shrink_to_fit();

surplus_hashes
}
Expand Down Expand Up @@ -432,8 +429,7 @@ impl<N: NetworkPrimitives> TransactionFetcher<N> {
peers: &HashMap<PeerId, PeerMetadata<N>>,
has_capacity_wrt_pending_pool_imports: impl Fn(usize) -> bool,
) {
let init_capacity_req = approx_capacity_get_pooled_transactions_req_eth68(&self.info);
let mut hashes_to_request = RequestTxHashes::with_capacity(init_capacity_req);
let mut hashes_to_request = RequestTxHashes::default();
let is_session_active = |peer_id: &PeerId| peers.contains_key(peer_id);

let mut search_durations = TxFetcherSearchDurations::default();
Expand Down Expand Up @@ -482,9 +478,6 @@ impl<N: NetworkPrimitives> TransactionFetcher<N> {
search_durations.fill_request
);

// free unused memory
hashes_to_request.shrink_to_fit();

self.update_pending_fetch_cache_search_metrics(search_durations);

trace!(target: "net::tx",
Expand Down
14 changes: 2 additions & 12 deletions crates/net/network/src/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,24 +640,15 @@ where
return
}

// load message version before announcement data type is destructed in packing
let msg_version = valid_announcement_data.msg_version();
//
// demand recommended soft limit on response, however the peer may enforce an arbitrary
// limit on the response (2MB)
//
// request buffer is shrunk via call to pack request!
let init_capacity_req =
self.transaction_fetcher.approx_capacity_get_pooled_transactions_req(msg_version);
let mut hashes_to_request = RequestTxHashes::with_capacity(init_capacity_req);
let mut hashes_to_request =
RequestTxHashes::with_capacity(valid_announcement_data.len() / 4);
let surplus_hashes =
self.transaction_fetcher.pack_request(&mut hashes_to_request, valid_announcement_data);

if !surplus_hashes.is_empty() {
trace!(target: "net::tx",
peer_id=format!("{peer_id:#}"),
surplus_hashes=?*surplus_hashes,
%msg_version,
%client,
"some hashes in announcement from peer didn't fit in `GetPooledTransactions` request, buffering surplus hashes"
);
Expand All @@ -668,7 +659,6 @@ where
trace!(target: "net::tx",
peer_id=format!("{peer_id:#}"),
hashes=?*hashes_to_request,
%msg_version,
%client,
"sending hashes in `GetPooledTransactions` request to peer's session"
);
Expand Down

0 comments on commit cb3e9f8

Please sign in to comment.