Skip to content

Commit

Permalink
Cache empty slots (#4874)
Browse files Browse the repository at this point in the history
* Cache empty slots.

Signed-off-by: Ameziane H <[email protected]>

* clear after each block and copy during clone

Signed-off-by: Karim TAAM <[email protected]>

* add changelog

Signed-off-by: Ameziane H <[email protected]>

* Avoid triggering a calculate root hash when empty slot cache is not empty.

Signed-off-by: Ameziane H <[email protected]>

Signed-off-by: Ameziane H <[email protected]>
Signed-off-by: Karim TAAM <[email protected]>
Signed-off-by: ahamlat <[email protected]>
Co-authored-by: Karim TAAM <[email protected]>
  • Loading branch information
ahamlat and matkt authored Jan 11, 2023
1 parent 192dcd0 commit d23a187
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
- Default configurations for the deprecated Ropsten, Kiln, Shandong, and Astor networks have been removed from the CLI network list. These networks can currently be accessed but will require a user-provided genesis configuration. [#4869](https://github.com/hyperledger/besu/pull/4869)

### Additions and Improvements

- Improve SLOAD and SSTORE performance by caching empty slots [#4874](https://github.com/hyperledger/besu/pull/4874)
- RPC methods that lookup block by hash will now return an error response if no block found [#4582](https://github.com/hyperledger/besu/pull/4582)

### Bug Fixes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class BonsaiWorldStateUpdater extends AbstractWorldUpdater<BonsaiWorldVie
private final Consumer<Hash> storagePreloader;
private final Map<Address, BonsaiValue<Bytes>> codeToUpdate = new ConcurrentHashMap<>();
private final Set<Address> storageToClear = Collections.synchronizedSet(new HashSet<>());
private final Set<Bytes> emptySlot = Collections.synchronizedSet(new HashSet<>());

// storage sub mapped by _hashed_ key. This is because in self_destruct calls we need to
// enumerate the old storage and delete it. Those are trie stored by hashed key by spec and the
Expand Down Expand Up @@ -87,6 +88,7 @@ void cloneFromUpdater(final BonsaiWorldStateUpdater source) {
storageToUpdate.putAll(source.storageToUpdate);
updatedAccounts.putAll(source.updatedAccounts);
deletedAccounts.addAll(source.deletedAccounts);
emptySlot.addAll(source.emptySlot);
}

@Override
Expand Down Expand Up @@ -349,18 +351,26 @@ public Optional<UInt256> getStorageValueBySlotHash(final Address address, final
return Optional.ofNullable(value.getUpdated());
}
}
final Optional<UInt256> valueUInt =
wrappedWorldView().getStorageValueBySlotHash(address, slotHash);
valueUInt.ifPresent(
v ->
storageToUpdate
.computeIfAbsent(
address,
key ->
new StorageConsumingMap<>(
address, new ConcurrentHashMap<>(), storagePreloader))
.put(slotHash, new BonsaiValue<>(v, v)));
return valueUInt;
final Bytes slot = Bytes.concatenate(Hash.hash(address), slotHash);
if (emptySlot.contains(slot)) {
return Optional.empty();
} else {
final Optional<UInt256> valueUInt =
wrappedWorldView().getStorageValueBySlotHash(address, slotHash);
valueUInt.ifPresentOrElse(
v ->
storageToUpdate
.computeIfAbsent(
address,
key ->
new StorageConsumingMap<>(
address, new ConcurrentHashMap<>(), storagePreloader))
.put(slotHash, new BonsaiValue<>(v, v)),
() -> {
emptySlot.add(Bytes.concatenate(Hash.hash(address), slotHash));
});
return valueUInt;
}
}

@Override
Expand Down Expand Up @@ -705,6 +715,7 @@ public void reset() {
storageToUpdate.clear();
codeToUpdate.clear();
accountsToUpdate.clear();
emptySlot.clear();
super.reset();
}

Expand Down

0 comments on commit d23a187

Please sign in to comment.