From 71df7e8e8ab7be65e6be8969fa26ead92cbb0385 Mon Sep 17 00:00:00 2001 From: Blas Rodriguez Irizar Date: Wed, 22 Nov 2023 14:47:40 -0300 Subject: [PATCH] optimization: reduce heap allocation (#970) * optimization: reduce heap allocation Prefer str::from_utf8 over String::from_utf8 to avoid unnecesary heap allocation * fmt * chore: add unclog --------- Co-authored-by: Farhad Shabani --- .../unreleased/improvements/970-reduce-heap-allocation.md | 3 +++ ibc-core/ics02-client/types/src/events.rs | 2 +- ibc-core/ics04-channel/types/src/events/packet_attributes.rs | 4 ++-- ibc-primitives/src/prelude.rs | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 .changelog/unreleased/improvements/970-reduce-heap-allocation.md diff --git a/.changelog/unreleased/improvements/970-reduce-heap-allocation.md b/.changelog/unreleased/improvements/970-reduce-heap-allocation.md new file mode 100644 index 000000000..9854416b2 --- /dev/null +++ b/.changelog/unreleased/improvements/970-reduce-heap-allocation.md @@ -0,0 +1,3 @@ +- Reduce heap allocation by using `str` instead of `String` places we convert + domain event attributes to the ABCI event attributes + ([\#970](https://github.com/cosmos/ibc-rs/issues/970)) diff --git a/ibc-core/ics02-client/types/src/events.rs b/ibc-core/ics02-client/types/src/events.rs index 31625757b..f9ae7213c 100644 --- a/ibc-core/ics02-client/types/src/events.rs +++ b/ibc-core/ics02-client/types/src/events.rs @@ -153,7 +153,7 @@ impl From for abci::EventAttribute { fn from(attr: HeaderAttribute) -> Self { ( HEADER_ATTRIBUTE_KEY, - String::from_utf8(hex::encode(attr.header)) + str::from_utf8(&hex::encode(attr.header)) .expect("Never fails because hexadecimal is valid UTF-8"), ) .into() diff --git a/ibc-core/ics04-channel/types/src/events/packet_attributes.rs b/ibc-core/ics04-channel/types/src/events/packet_attributes.rs index 44a7970a8..2366ae71e 100644 --- a/ibc-core/ics04-channel/types/src/events/packet_attributes.rs +++ b/ibc-core/ics04-channel/types/src/events/packet_attributes.rs @@ -59,7 +59,7 @@ impl TryFrom for Vec { .into(), ( PKT_DATA_HEX_ATTRIBUTE_KEY, - String::from_utf8(hex::encode(attr.packet_data)) + str::from_utf8(&hex::encode(attr.packet_data)) .expect("Never fails because hexadecimal is valid UTF8"), ) .into(), @@ -329,7 +329,7 @@ impl TryFrom for Vec { .into(), ( PKT_ACK_HEX_ATTRIBUTE_KEY, - String::from_utf8(hex::encode(attr.acknowledgement)) + str::from_utf8(&hex::encode(attr.acknowledgement)) .expect("Never fails because hexadecimal is always valid UTF-8"), ) .into(), diff --git a/ibc-primitives/src/prelude.rs b/ibc-primitives/src/prelude.rs index 1dd86e2e1..3662f7bb9 100644 --- a/ibc-primitives/src/prelude.rs +++ b/ibc-primitives/src/prelude.rs @@ -4,5 +4,5 @@ pub use alloc::borrow::ToOwned; pub use alloc::boxed::Box; pub use alloc::string::{String, ToString}; pub use alloc::vec::Vec; -pub use alloc::{format, vec}; +pub use alloc::{format, str, vec}; pub use core::prelude::v1::*;