Skip to content

Commit

Permalink
Avoid unnecessary String allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
mina86 committed Apr 23, 2024
1 parent 46588f5 commit 3542745
Show file tree
Hide file tree
Showing 21 changed files with 155 additions and 184 deletions.
8 changes: 4 additions & 4 deletions contracts/pallet-ibc/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ impl From<Packet> for PacketInfo {
fn from(packet: Packet) -> Self {
Self {
sequence: packet.sequence.into(),
source_port: packet.source_port.to_string().as_bytes().to_vec(),
source_channel: packet.source_channel.to_string().as_bytes().to_vec(),
destination_port: packet.destination_port.to_string().as_bytes().to_vec(),
destination_channel: packet.destination_channel.to_string().as_bytes().to_vec(),
source_port: packet.source_port.to_string().into_bytes(),
source_channel: packet.source_channel.to_string().into_bytes(),
destination_port: packet.destination_port.to_string().into_bytes(),
destination_channel: packet.destination_channel.to_string().into_bytes(),
data: packet.data,
timeout_height: (
packet.timeout_height.revision_number,
Expand Down
12 changes: 6 additions & 6 deletions contracts/pallet-ibc/rpc/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ where
let packets: Vec<ibc_primitives::PacketInfo> = api
.query_send_packet_info(
at,
channel_id.to_string().as_bytes().to_vec(),
channel_id.to_string().into_bytes(),
port_id.as_bytes().to_vec(),
vec![sequence],
)
Expand All @@ -45,7 +45,7 @@ where
let packets: Vec<ibc_primitives::PacketInfo> = api
.query_recv_packet_info(
at,
channel_id.to_string().as_bytes().to_vec(),
channel_id.to_string().into_bytes(),
port_id.as_bytes().to_vec(),
vec![sequence],
)
Expand All @@ -63,7 +63,7 @@ where
let packets: Vec<ibc_primitives::PacketInfo> = api
.query_recv_packet_info(
at,
channel_id.to_string().as_bytes().to_vec(),
channel_id.to_string().into_bytes(),
port_id.as_bytes().to_vec(),
vec![sequence],
)
Expand All @@ -81,7 +81,7 @@ where
let packets: Vec<ibc_primitives::PacketInfo> = api
.query_send_packet_info(
at,
channel_id.to_string().as_bytes().to_vec(),
channel_id.to_string().into_bytes(),
port_id.as_bytes().to_vec(),
vec![sequence],
)
Expand All @@ -99,7 +99,7 @@ where
let packets: Vec<ibc_primitives::PacketInfo> = api
.query_send_packet_info(
at,
channel_id.to_string().as_bytes().to_vec(),
channel_id.to_string().into_bytes(),
port_id.as_bytes().to_vec(),
vec![sequence],
)
Expand All @@ -117,7 +117,7 @@ where
let packets: Vec<ibc_primitives::PacketInfo> = api
.query_send_packet_info(
at,
channel_id.to_string().as_bytes().to_vec(),
channel_id.to_string().into_bytes(),
port_id.as_bytes().to_vec(),
vec![sequence],
)
Expand Down
8 changes: 4 additions & 4 deletions contracts/pallet-ibc/src/benchmarks/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ benchmarks! {
let timeout = Timeout::Offset { timestamp: Some(1690894363), height: Some(2000) };

let transfer_params = TransferParams {
to: MultiAddress::Raw("bob".to_string().as_bytes().to_vec()),
to: MultiAddress::Raw("bob".to_string().into_bytes()),
source_channel: channel_id.sequence(),
timeout,
};
Expand Down Expand Up @@ -988,7 +988,7 @@ benchmarks! {
let mut output = HandlerOutputBuilder::new();
let port_id = PortId::transfer();
let channel_id = ChannelId::new(0);
let channel_ids = vec![channel_id.to_string().as_bytes().to_vec()];
let channel_ids = vec![channel_id.to_string().into_bytes()];
ChannelIds::<T>::put(channel_ids);
let mut handler = IbcModule::<T>::default();
}:{
Expand All @@ -1003,7 +1003,7 @@ benchmarks! {
let mut output = HandlerOutputBuilder::new();
let port_id = PortId::transfer();
let channel_id = ChannelId::new(0);
let channel_ids = vec![channel_id.to_string().as_bytes().to_vec()];
let channel_ids = vec![channel_id.to_string().into_bytes()];
ChannelIds::<T>::put(channel_ids);
let mut handler = IbcModule::<T>::default();
}:{
Expand Down Expand Up @@ -1351,7 +1351,7 @@ benchmarks! {
}
}

let channel_id_bytes = channel_id.to_string().as_bytes().to_vec();
let channel_id_bytes = channel_id.to_string().into_bytes();
let port_id_bytes = port_id.as_bytes().to_vec();

let (send_seq_set, _) =
Expand Down
10 changes: 4 additions & 6 deletions contracts/pallet-ibc/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ where
packet: ibc::core::ics04_channel::packet::Packet,
) -> Result<(), ICS04Error> {
// store packet offchain
let channel_id = key.1.to_string().as_bytes().to_vec();
let channel_id = key.1.to_string().into_bytes();
let port_id = key.0.as_bytes().to_vec();
let seq = u64::from(key.2);
let channel_end = ChannelReader::channel_end(self, &(key.0, key.1))?;
Expand All @@ -306,7 +306,7 @@ where
packet: ibc::core::ics04_channel::packet::Packet,
) -> Result<(), ICS04Error> {
// Store packet offchain
let channel_id = key.1.to_string().as_bytes().to_vec();
let channel_id = key.1.to_string().into_bytes();
let port_id = key.0.as_bytes().to_vec();
let seq = u64::from(key.2);
let channel_end = ChannelReader::channel_end(self, &(key.0, key.1))?;
Expand Down Expand Up @@ -396,10 +396,8 @@ where
) -> Result<(), ICS04Error> {
let conn_id = conn_id.as_bytes().to_vec();

let port_channel_id = (
port_channel_id.0.as_bytes().to_vec(),
port_channel_id.1.to_string().as_bytes().to_vec(),
);
let port_channel_id =
(port_channel_id.0.as_bytes().to_vec(), port_channel_id.1.to_string().into_bytes());

if <ChannelsConnection<T>>::contains_key(conn_id.clone()) {
log::trace!(target: "pallet_ibc", "in channel: [store_connection_channels] >> insert port_channel_id");
Expand Down
54 changes: 22 additions & 32 deletions contracts/pallet-ibc/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,132 +311,122 @@ impl From<RawIbcEvent> for IbcEvent {
RawIbcEvent::OpenInitChannel(ev) => IbcEvent::OpenInitChannel {
revision_height: ev.height().revision_height,
revision_number: ev.height().revision_number,
channel_id: ev
.channel_id()
.map(|channel_id| channel_id.to_string().as_bytes().to_vec()),
channel_id: ev.channel_id().map(|channel_id| channel_id.to_string().into_bytes()),
port_id: ev.port_id().as_bytes().to_vec(),
connection_id: ev.connection_id.as_bytes().to_vec(),
counterparty_port_id: ev.counterparty_port_id.as_bytes().to_vec(),
counterparty_channel_id: ev
.counterparty_channel_id
.map(|val| val.to_string().as_bytes().to_vec()),
.map(|val| val.to_string().into_bytes()),
},
RawIbcEvent::OpenTryChannel(ev) => IbcEvent::OpenTryChannel {
revision_height: ev.height().revision_height,
revision_number: ev.height().revision_number,
channel_id: ev
.channel_id()
.map(|channel_id| channel_id.to_string().as_bytes().to_vec()),
channel_id: ev.channel_id().map(|channel_id| channel_id.to_string().into_bytes()),
port_id: ev.port_id().as_bytes().to_vec(),
connection_id: ev.connection_id.as_bytes().to_vec(),
counterparty_port_id: ev.counterparty_port_id.as_bytes().to_vec(),
counterparty_channel_id: ev
.counterparty_channel_id
.map(|val| val.to_string().as_bytes().to_vec()),
.map(|val| val.to_string().into_bytes()),
},
RawIbcEvent::OpenAckChannel(ev) => IbcEvent::OpenAckChannel {
revision_height: ev.height().revision_height,
revision_number: ev.height().revision_number,
channel_id: ev
.channel_id()
.map(|channel_id| channel_id.to_string().as_bytes().to_vec()),
channel_id: ev.channel_id().map(|channel_id| channel_id.to_string().into_bytes()),
port_id: ev.port_id().as_bytes().to_vec(),
connection_id: ev.connection_id.as_bytes().to_vec(),
counterparty_port_id: ev.counterparty_port_id.as_bytes().to_vec(),
counterparty_channel_id: ev
.counterparty_channel_id
.map(|val| val.to_string().as_bytes().to_vec()),
.map(|val| val.to_string().into_bytes()),
},
RawIbcEvent::OpenConfirmChannel(ev) => IbcEvent::OpenConfirmChannel {
revision_height: ev.height().revision_height,
revision_number: ev.height().revision_number,
channel_id: ev
.channel_id()
.map(|channel_id| channel_id.to_string().as_bytes().to_vec()),
channel_id: ev.channel_id().map(|channel_id| channel_id.to_string().into_bytes()),
port_id: ev.port_id().as_bytes().to_vec(),
connection_id: ev.connection_id.as_bytes().to_vec(),
counterparty_port_id: ev.counterparty_port_id.as_bytes().to_vec(),
counterparty_channel_id: ev
.counterparty_channel_id
.map(|val| val.to_string().as_bytes().to_vec()),
.map(|val| val.to_string().into_bytes()),
},
RawIbcEvent::CloseInitChannel(ev) => IbcEvent::CloseInitChannel {
revision_height: ev.height().revision_height,
revision_number: ev.height().revision_number,
channel_id: ev.channel_id().to_string().as_bytes().to_vec(),
channel_id: ev.channel_id().to_string().into_bytes(),
port_id: ev.port_id().as_bytes().to_vec(),
connection_id: ev.connection_id.as_bytes().to_vec(),
counterparty_port_id: ev.counterparty_port_id.as_bytes().to_vec(),
counterparty_channel_id: ev
.counterparty_channel_id
.map(|val| val.to_string().as_bytes().to_vec()),
.map(|val| val.to_string().into_bytes()),
},
RawIbcEvent::CloseConfirmChannel(ev) => IbcEvent::CloseConfirmChannel {
revision_height: ev.height().revision_height,
revision_number: ev.height().revision_number,
port_id: ev.port_id.as_bytes().to_vec(),
channel_id: ev
.channel_id()
.map(|channel_id| channel_id.to_string().as_bytes().to_vec()),
channel_id: ev.channel_id().map(|channel_id| channel_id.to_string().into_bytes()),
connection_id: ev.connection_id.as_bytes().to_vec(),
counterparty_port_id: ev.counterparty_port_id.as_bytes().to_vec(),
counterparty_channel_id: ev
.counterparty_channel_id
.map(|val| val.to_string().as_bytes().to_vec()),
.map(|val| val.to_string().into_bytes()),
},
RawIbcEvent::SendPacket(ev) => IbcEvent::SendPacket {
revision_height: ev.height().revision_height,
revision_number: ev.height().revision_number,
channel_id: ev.src_channel_id().to_string().as_bytes().to_vec(),
channel_id: ev.src_channel_id().to_string().into_bytes(),
port_id: ev.src_port_id().as_bytes().to_vec(),
dest_port: ev.dst_port_id().as_bytes().to_vec(),
dest_channel: ev.dst_channel_id().to_string().as_bytes().to_vec(),
dest_channel: ev.dst_channel_id().to_string().into_bytes(),
sequence: ev.packet.sequence.into(),
},
RawIbcEvent::ReceivePacket(ev) => IbcEvent::ReceivePacket {
revision_height: ev.height().revision_height,
revision_number: ev.height().revision_number,
channel_id: ev.src_channel_id().to_string().as_bytes().to_vec(),
channel_id: ev.src_channel_id().to_string().into_bytes(),
port_id: ev.src_port_id().as_bytes().to_vec(),
dest_port: ev.dst_port_id().as_bytes().to_vec(),
dest_channel: ev.dst_channel_id().to_string().as_bytes().to_vec(),
dest_channel: ev.dst_channel_id().to_string().into_bytes(),
sequence: ev.packet.sequence.into(),
},
RawIbcEvent::WriteAcknowledgement(ev) => IbcEvent::WriteAcknowledgement {
revision_height: ev.height().revision_height,
revision_number: ev.height().revision_number,
channel_id: ev.src_channel_id().to_string().as_bytes().to_vec(),
channel_id: ev.src_channel_id().to_string().into_bytes(),
port_id: ev.src_port_id().as_bytes().to_vec(),
dest_port: ev.dst_port_id().as_bytes().to_vec(),
dest_channel: ev.dst_channel_id().to_string().as_bytes().to_vec(),
dest_channel: ev.dst_channel_id().to_string().into_bytes(),
sequence: ev.packet.sequence.into(),
},
RawIbcEvent::AcknowledgePacket(ev) => IbcEvent::AcknowledgePacket {
revision_height: ev.height().revision_height,
revision_number: ev.height().revision_number,
channel_id: ev.src_channel_id().to_string().as_bytes().to_vec(),
channel_id: ev.src_channel_id().to_string().into_bytes(),
port_id: ev.src_port_id().as_bytes().to_vec(),
sequence: ev.packet.sequence.into(),
},
RawIbcEvent::TimeoutPacket(ev) => IbcEvent::TimeoutPacket {
revision_height: ev.height().revision_height,
revision_number: ev.height().revision_number,
channel_id: ev.src_channel_id().to_string().as_bytes().to_vec(),
channel_id: ev.src_channel_id().to_string().into_bytes(),
port_id: ev.src_port_id().as_bytes().to_vec(),
sequence: ev.packet.sequence.into(),
},
RawIbcEvent::TimeoutOnClosePacket(ev) => IbcEvent::TimeoutOnClosePacket {
revision_height: ev.height().revision_height,
revision_number: ev.height().revision_number,
channel_id: ev.src_channel_id().to_string().as_bytes().to_vec(),
channel_id: ev.src_channel_id().to_string().into_bytes(),
port_id: ev.src_port_id().as_bytes().to_vec(),
sequence: ev.packet.sequence.into(),
},
RawIbcEvent::Empty(_) => IbcEvent::Empty,
RawIbcEvent::ChainError(_) => IbcEvent::ChainError,
RawIbcEvent::AppModule(ev) => IbcEvent::AppModule {
kind: ev.kind.as_bytes().to_vec(),
module_id: ev.module_name.to_string().as_bytes().to_vec(),
module_id: ev.module_name.to_string().into_bytes(),
},
RawIbcEvent::PushWasmCode(ev) => {
let wasm_checksum = ev.0;
Expand Down
Loading

0 comments on commit 3542745

Please sign in to comment.