Skip to content

Commit

Permalink
perf: add has_received_data and use it
Browse files Browse the repository at this point in the history
`get_received_data` has a property of returning the data read out from
the storage which can result in the data being read out multiple times
only for most of these reads to be immediately discarded. The newly
added functions enable explicitly checking for presence of a key without
reading out the corresponding value.
  • Loading branch information
nagisa committed Jan 25, 2024
1 parent 67b299e commit 3b88260
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
5 changes: 2 additions & 3 deletions core/store/src/genesis/state_applier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::flat::FlatStateChanges;
use crate::{
get_account, get_received_data, set, set_access_key, set_account, set_code,
get_account, has_received_data, set, set_access_key, set_account, set_code,
set_delayed_receipt, set_postponed_receipt, set_received_data, ShardTries, TrieUpdate,
};

Expand Down Expand Up @@ -277,9 +277,8 @@ impl GenesisStateApplier {
let mut pending_data_count: u32 = 0;
for data_id in &action_receipt.input_data_ids {
storage.modify(|state_update| {
if get_received_data(state_update, account_id, *data_id)
if !has_received_data(state_update, account_id, *data_id)
.expect("Genesis storage error")
.is_none()
{
pending_data_count += 1;
set(
Expand Down
8 changes: 8 additions & 0 deletions core/store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,14 @@ pub fn get_received_data(
get(trie, &TrieKey::ReceivedData { receiver_id: receiver_id.clone(), data_id })
}

pub fn has_received_data(
trie: &dyn TrieAccess,
receiver_id: &AccountId,
data_id: CryptoHash,
) -> Result<bool, StorageError> {
trie.contains_key(&TrieKey::ReceivedData { receiver_id: receiver_id.clone(), data_id })
}

pub fn set_postponed_receipt(state_update: &mut TrieUpdate, receipt: &Receipt) {
let key = TrieKey::PostponedReceipt {
receiver_id: receipt.receiver_id.clone(),
Expand Down
8 changes: 4 additions & 4 deletions runtime/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ use near_primitives::utils::{
};
use near_primitives::version::{ProtocolFeature, ProtocolVersion};
use near_store::{
get, get_account, get_postponed_receipt, get_received_data, remove_postponed_receipt, set,
set_account, set_delayed_receipt, set_postponed_receipt, set_received_data, PartialStorage,
StorageError, Trie, TrieChanges, TrieUpdate,
get, get_account, get_postponed_receipt, get_received_data, has_received_data,
remove_postponed_receipt, set, set_account, set_delayed_receipt, set_postponed_receipt,
set_received_data, PartialStorage, StorageError, Trie, TrieChanges, TrieUpdate,
};
use near_store::{set_access_key, set_code};
use near_vm_runner::logic::types::PromiseResult;
Expand Down Expand Up @@ -967,7 +967,7 @@ impl Runtime {
// If not, then we will postpone this receipt for later.
let mut pending_data_count: u32 = 0;
for data_id in &action_receipt.input_data_ids {
if get_received_data(state_update, account_id, *data_id)?.is_none() {
if !has_received_data(state_update, account_id, *data_id)? {
pending_data_count += 1;
// The data for a given data_id is not available, so we save a link to this
// receipt_id for the pending data_id into the state.
Expand Down

0 comments on commit 3b88260

Please sign in to comment.