Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solana-ibc: avoid String allocations and multiple map lookups #40

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading