Skip to content

Commit

Permalink
chore: add sload cache on read
Browse files Browse the repository at this point in the history
  • Loading branch information
renancloudwalk committed Mar 8, 2024
1 parent 64c54cd commit 92b74ec
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/eth/storage/postgres/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,27 @@ impl PermanentStorage for Postgres {
tracing::debug!(%address, %slot_index, "reading slot");

// TODO: improve this conversion
let slot_index: [u8; 32] = slot_index.clone().into();
let slot_index_u8: [u8; 32] = slot_index.clone().into();

let slot = match point_in_time {
StoragePointInTime::Present =>
sqlx::query_file_as!(Slot, "src/eth/storage/postgres/queries/select_slot.sql", address.as_ref(), slot_index.as_ref())
.fetch_optional(&self.connection_pool)
.await?,
StoragePointInTime::Present => {
let sload_cache = self.sload_cache.read().await;
if let Some((value, _)) = sload_cache.get(&(address.clone(), slot_index.clone())) {
Some(Slot {
index: slot_index.clone(),
value: value.clone(),
})
} else {
None
}
}
StoragePointInTime::Past(number) => {
let block_number: i64 = (*number).try_into()?;
sqlx::query_file_as!(
Slot,
"src/eth/storage/postgres/queries/select_slot_at_block.sql",
address.as_ref(),
slot_index.as_ref(),
slot_index_u8.as_ref(),
block_number as _,
)
.fetch_optional(&self.connection_pool)
Expand Down Expand Up @@ -516,6 +523,7 @@ impl PermanentStorage for Postgres {
}
}

let mut sload_batch = vec![];
for change in account_changes {
// for change in transaction.execution.changes {
let (original_nonce, new_nonce) = change.nonce.take_both();
Expand Down Expand Up @@ -560,7 +568,9 @@ impl PermanentStorage for Postgres {
let original_value = original_value.unwrap_or_default().value;

slot_batch.push(change.address.clone(), slot_idx.clone(), new_value.clone(), block.header.number, original_value);
historical_slot_batch.push(change.address.clone(), slot_idx, new_value, block.header.number);
historical_slot_batch.push(change.address.clone(), slot_idx.clone(), new_value.clone(), block.header.number);

sload_batch.push((change.address.clone(), slot_idx.clone(), new_value.clone(), block.header.number));
}
}

Expand Down Expand Up @@ -664,6 +674,13 @@ impl PermanentStorage for Postgres {

tx.commit().await.context("failed to commit transaction")?;

{
let mut sload_cache = self.sload_cache.write().await;
for sload_tuple in sload_batch {
sload_cache.insert((sload_tuple.0, sload_tuple.1), (sload_tuple.2, sload_tuple.3));
}
}

Ok(())
}

Expand Down

0 comments on commit 92b74ec

Please sign in to comment.