Skip to content

Commit

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

Signed-off-by: Ameziane H <ameziane.hamlat@consensys.net>

* clear after each block and copy during clone

Signed-off-by: Karim TAAM <karim.t2am@gmail.com>

* add changelog

Signed-off-by: Ameziane H <ameziane.hamlat@consensys.net>

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

Signed-off-by: Ameziane H <ameziane.hamlat@consensys.net>

Signed-off-by: Ameziane H <ameziane.hamlat@consensys.net>
Signed-off-by: Karim TAAM <karim.t2am@gmail.com>
Signed-off-by: ahamlat <ameziane.hamlat@consensys.net>
Co-authored-by: Karim TAAM <karim.t2am@gmail.com>
  • Loading branch information
2 people authored and siladu committed Feb 7, 2023
1 parent c015567 commit 7cdc39b
Showing 1 changed file with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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 @@ -90,6 +91,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 @@ -352,18 +354,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 @@ -706,6 +716,7 @@ public void reset() {
storageToUpdate.clear();
codeToUpdate.clear();
accountsToUpdate.clear();
emptySlot.clear();
super.reset();
}

Expand Down

0 comments on commit 7cdc39b

Please sign in to comment.