Skip to content

Commit

Permalink
optimization: reduce heap allocation (#970)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
blasrodri and Farhad-Shabani authored Nov 22, 2023
1 parent 4596f88 commit 71df7e8
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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))
2 changes: 1 addition & 1 deletion ibc-core/ics02-client/types/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl From<HeaderAttribute> 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()
Expand Down
4 changes: 2 additions & 2 deletions ibc-core/ics04-channel/types/src/events/packet_attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl TryFrom<PacketDataAttribute> for Vec<abci::EventAttribute> {
.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(),
Expand Down Expand Up @@ -329,7 +329,7 @@ impl TryFrom<AcknowledgementAttribute> for Vec<abci::EventAttribute> {
.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(),
Expand Down
2 changes: 1 addition & 1 deletion ibc-primitives/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

0 comments on commit 71df7e8

Please sign in to comment.