Skip to content

Commit

Permalink
solana-ibc: avoid String allocations and multiple map lookups (#40)
Browse files Browse the repository at this point in the history
Firstly, use BTreeMap::entry to avoid doing lookups in maps multiple
times.  With entry method we only need to traverse the map once.

Secondly, use `foo.as_str()` rather than `&foo.to_string()` for client
and connection ids so we avoid String allocations.
  • Loading branch information
mina86 authored Oct 19, 2023
1 parent 9688148 commit da43fd5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 49 deletions.
60 changes: 15 additions & 45 deletions solana/solana-ibc/programs/solana-ibc/src/execution_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ use crate::client_state::AnyClientState;
use crate::consensus_state::AnyConsensusState;
use crate::trie_key::TrieKey;
use crate::{
EmitIBCEvent, HostHeight, InnerChannelId, InnerHeight, InnerPortId,
InnerSequence, SolanaIbcStorage, SolanaTimestamp,
EmitIBCEvent, InnerChannelId, InnerPortId, InnerSequence, SolanaIbcStorage,
};

type Result<T = (), E = ibc::core::ContextError> = core::result::Result<T, E>;
Expand Down Expand Up @@ -116,33 +115,19 @@ impl ExecutionContext for SolanaIbcStorage<'_, '_> {
height: Height,
timestamp: Timestamp,
) -> Result {
msg!("I am here inside update time");
msg!(
"store_update_time - client_id: {}, height: {}, timestamp: {}",
client_id,
height,
timestamp
);
let mut new_map: BTreeMap<InnerHeight, SolanaTimestamp> =
BTreeMap::new();
BTreeMap::insert(
&mut new_map,
(height.revision_number(), height.revision_height()),
timestamp.nanoseconds(),
);
if !self.client_processed_times.contains_key(&client_id.to_string()) {
self.client_processed_times
.insert(client_id.to_string().clone(), new_map);
}
self.client_processed_times.get_mut(&client_id.to_string()).map(
|processed_times| {
BTreeMap::insert(
processed_times,
(height.revision_number(), height.revision_height()),
timestamp.nanoseconds(),
)
},
);
self.client_processed_times
.entry(client_id.to_string())
.or_default()
.insert(
(height.revision_number(), height.revision_height()),
timestamp.nanoseconds(),
);
Ok(())
}

Expand All @@ -159,28 +144,13 @@ impl ExecutionContext for SolanaIbcStorage<'_, '_> {
height,
host_height
);
let mut new_map: BTreeMap<InnerHeight, HostHeight> = BTreeMap::new();
BTreeMap::insert(
&mut new_map,
(height.revision_number(), height.revision_height()),
(host_height.revision_number(), host_height.revision_height()),
);
if !self.client_processed_heights.contains_key(&client_id.to_string()) {
self.client_processed_heights
.insert(client_id.to_string().clone(), new_map);
}
self.client_processed_heights.get_mut(&client_id.to_string()).map(
|processed_heights| {
BTreeMap::insert(
processed_heights,
(height.revision_number(), height.revision_height()),
(
host_height.revision_number(),
host_height.revision_height(),
),
)
},
);
self.client_processed_heights
.entry(client_id.to_string())
.or_default()
.insert(
(height.revision_number(), height.revision_height()),
(host_height.revision_number(), host_height.revision_height()),
);
Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl ValidationContext for SolanaIbcStorage<'_, '_> {
&self,
client_id: &ClientId,
) -> std::result::Result<Self::AnyClientState, ContextError> {
match self.clients.get(&client_id.to_string()) {
match self.clients.get(client_id.as_str()) {
Some(data) => {
let client: AnyClientState =
serde_json::from_str(data).unwrap();
Expand Down Expand Up @@ -112,7 +112,7 @@ impl ValidationContext for SolanaIbcStorage<'_, '_> {
&self,
conn_id: &ConnectionId,
) -> std::result::Result<ConnectionEnd, ContextError> {
match self.connections.get(&conn_id.to_string()) {
match self.connections.get(conn_id.as_str()) {
Some(data) => {
let connection: ConnectionEnd =
serde_json::from_str(data).unwrap();
Expand Down Expand Up @@ -282,7 +282,7 @@ impl ValidationContext for SolanaIbcStorage<'_, '_> {
height: &Height,
) -> std::result::Result<Timestamp, ContextError> {
self.client_processed_times
.get(&client_id.to_string())
.get(client_id.as_str())
.and_then(|processed_times| {
processed_times
.get(&(height.revision_number(), height.revision_height()))
Expand All @@ -305,7 +305,7 @@ impl ValidationContext for SolanaIbcStorage<'_, '_> {
height: &Height,
) -> std::result::Result<Height, ContextError> {
self.client_processed_heights
.get(&client_id.to_string())
.get(client_id.as_str())
.and_then(|processed_heights| {
processed_heights
.get(&(height.revision_number(), height.revision_height()))
Expand Down

0 comments on commit da43fd5

Please sign in to comment.