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: optimize Vec push avoid realloc mem #1374

Merged
merged 5 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- [ibc-app-nft-transfer] optimize `Vec` pushes to avoid reallocation of memory
([\#1374](https://github.com/cosmos/ibc-rs/pull/1374)).
2 changes: 1 addition & 1 deletion ibc-apps/ics721-nft-transfer/src/handler/on_recv_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
};

let mut extras = ModuleExtras {
events: vec![],
events: Vec::with_capacity(data.token_ids.0.len()),

Check warning on line 82 in ibc-apps/ics721-nft-transfer/src/handler/on_recv_packet.rs

View check run for this annotation

Codecov / codecov/patch

ibc-apps/ics721-nft-transfer/src/handler/on_recv_packet.rs#L82

Added line #L82 was not covered by tests
log: Vec::new(),
};
for (i, token_id) in data.token_ids.0.iter().enumerate() {
Expand Down
18 changes: 10 additions & 8 deletions ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,11 @@
// overwrite even if they are set in MsgTransfer
if let Some(uris) = &mut packet_data.token_uris {
uris.clear();
uris.reserve_exact(token_ids.0.len());

Check warning on line 175 in ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs

View check run for this annotation

Codecov / codecov/patch

ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs#L175

Added line #L175 was not covered by tests
}
if let Some(data) = &mut packet_data.token_data {
data.clear();
data.reserve_exact(token_ids.0.len());

Check warning on line 179 in ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs

View check run for this annotation

Codecov / codecov/patch

ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs#L179

Added line #L179 was not covered by tests
}
for token_id in token_ids.as_ref() {
if is_sender_chain_source(msg.port_id_on_a.clone(), msg.chan_id_on_a.clone(), class_id) {
Expand All @@ -197,14 +199,14 @@
let nft = transfer_ctx.get_nft(class_id, token_id)?;
// Set the URI and the data if both exists
if let (Some(uri), Some(data)) = (nft.get_uri(), nft.get_data()) {
match &mut packet_data.token_uris {
Some(uris) => uris.push(uri.clone()),
None => packet_data.token_uris = Some(vec![uri.clone()]),
}
match &mut packet_data.token_data {
Some(token_data) => token_data.push(data.clone()),
None => packet_data.token_data = Some(vec![data.clone()]),
}
packet_data
.token_uris
.get_or_insert_with(|| Vec::with_capacity(token_ids.0.len()))
.push(uri.clone());
packet_data
.token_data
.get_or_insert_with(|| Vec::with_capacity(token_ids.0.len()))
.push(data.clone());

Check warning on line 209 in ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs

View check run for this annotation

Codecov / codecov/patch

ibc-apps/ics721-nft-transfer/src/handler/send_transfer.rs#L202-L209

Added lines #L202 - L209 were not covered by tests
}
}

Expand Down
1 change: 1 addition & 0 deletions ibc-derive/src/client_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ fn split_for_impl(
let mut predicates = vec![];

if let syn::PathArguments::AngleBracketed(gen) = args {
generics.reserve_exact(gen.args.len());
for arg in gen.args {
match arg.clone() {
GenericArgument::Type(_) | GenericArgument::Lifetime(_) => {
Expand Down
Loading