Skip to content

Commit 71df7e8

Browse files
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 <[email protected]>
1 parent 4596f88 commit 71df7e8

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Reduce heap allocation by using `str` instead of `String` places we convert
2+
domain event attributes to the ABCI event attributes
3+
([\#970](https://github.com/cosmos/ibc-rs/issues/970))

ibc-core/ics02-client/types/src/events.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl From<HeaderAttribute> for abci::EventAttribute {
153153
fn from(attr: HeaderAttribute) -> Self {
154154
(
155155
HEADER_ATTRIBUTE_KEY,
156-
String::from_utf8(hex::encode(attr.header))
156+
str::from_utf8(&hex::encode(attr.header))
157157
.expect("Never fails because hexadecimal is valid UTF-8"),
158158
)
159159
.into()

ibc-core/ics04-channel/types/src/events/packet_attributes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl TryFrom<PacketDataAttribute> for Vec<abci::EventAttribute> {
5959
.into(),
6060
(
6161
PKT_DATA_HEX_ATTRIBUTE_KEY,
62-
String::from_utf8(hex::encode(attr.packet_data))
62+
str::from_utf8(&hex::encode(attr.packet_data))
6363
.expect("Never fails because hexadecimal is valid UTF8"),
6464
)
6565
.into(),
@@ -329,7 +329,7 @@ impl TryFrom<AcknowledgementAttribute> for Vec<abci::EventAttribute> {
329329
.into(),
330330
(
331331
PKT_ACK_HEX_ATTRIBUTE_KEY,
332-
String::from_utf8(hex::encode(attr.acknowledgement))
332+
str::from_utf8(&hex::encode(attr.acknowledgement))
333333
.expect("Never fails because hexadecimal is always valid UTF-8"),
334334
)
335335
.into(),

ibc-primitives/src/prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ pub use alloc::borrow::ToOwned;
44
pub use alloc::boxed::Box;
55
pub use alloc::string::{String, ToString};
66
pub use alloc::vec::Vec;
7-
pub use alloc::{format, vec};
7+
pub use alloc::{format, str, vec};
88
pub use core::prelude::v1::*;

0 commit comments

Comments
 (0)