Skip to content

Commit 675be78

Browse files
Use multi_get in execute_block_inner (#4789)
## Motivation Some operations in `execute_block_inner` use for loops with each entry of the loop being an await operation. This is slow. ## Proposal Use the recently introduced `multi_get` operations. ## Test Plan The CI. ## Release Plan These kinds of changes can be easily backported to TestNet Conway. ## Links None.
1 parent 3caf645 commit 675be78

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

linera-chain/src/chain.rs

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -821,32 +821,44 @@ where
821821
}
822822

823823
let recipients = block_execution_tracker.recipients();
824-
let mut previous_message_blocks = BTreeMap::new();
825-
for recipient in recipients {
826-
if let Some(height) = previous_message_blocks_view.get(&recipient).await? {
827-
let hash = confirmed_log
828-
.get(usize::try_from(height.0).map_err(|_| ArithmeticError::Overflow)?)
829-
.await?
830-
.ok_or_else(|| {
831-
ChainError::InternalError("missing entry in confirmed_log".into())
832-
})?;
833-
previous_message_blocks.insert(recipient, (hash, height));
824+
let heights = previous_message_blocks_view.multi_get(&recipients).await?;
825+
let mut recipient_heights = Vec::new();
826+
let mut indices = Vec::new();
827+
for (height, recipient) in heights.into_iter().zip(recipients) {
828+
if let Some(height) = height {
829+
let index = usize::try_from(height.0).map_err(|_| ArithmeticError::Overflow)?;
830+
indices.push(index);
831+
recipient_heights.push((recipient, height));
834832
}
835833
}
834+
let hashes = confirmed_log.multi_get(indices).await?;
835+
let mut previous_message_blocks = BTreeMap::new();
836+
for (hash, (recipient, height)) in hashes.into_iter().zip(recipient_heights) {
837+
let hash = hash.ok_or_else(|| {
838+
ChainError::InternalError("missing entry in confirmed_log".into())
839+
})?;
840+
previous_message_blocks.insert(recipient, (hash, height));
841+
}
836842

837843
let streams = block_execution_tracker.event_streams();
838-
let mut previous_event_blocks = BTreeMap::new();
839-
for stream in streams {
840-
if let Some(height) = previous_event_blocks_view.get(&stream).await? {
841-
let hash = confirmed_log
842-
.get(usize::try_from(height.0).map_err(|_| ArithmeticError::Overflow)?)
843-
.await?
844-
.ok_or_else(|| {
845-
ChainError::InternalError("missing entry in confirmed_log".into())
846-
})?;
847-
previous_event_blocks.insert(stream, (hash, height));
844+
let heights = previous_event_blocks_view.multi_get(&streams).await?;
845+
let mut stream_heights = Vec::new();
846+
let mut indices = Vec::new();
847+
for (stream, height) in streams.into_iter().zip(heights) {
848+
if let Some(height) = height {
849+
let index = usize::try_from(height.0).map_err(|_| ArithmeticError::Overflow)?;
850+
indices.push(index);
851+
stream_heights.push((stream, height));
848852
}
849853
}
854+
let hashes = confirmed_log.multi_get(indices).await?;
855+
let mut previous_event_blocks = BTreeMap::new();
856+
for (hash, (stream, height)) in hashes.into_iter().zip(stream_heights) {
857+
let hash = hash.ok_or_else(|| {
858+
ChainError::InternalError("missing entry in confirmed_log".into())
859+
})?;
860+
previous_event_blocks.insert(stream, (hash, height));
861+
}
850862

851863
let state_hash = {
852864
#[cfg(with_metrics)]

0 commit comments

Comments
 (0)