fix missed cache opportunity (#2628)

The storage leaf cache was being circumvented when actually fetching
leaves and was instead only being filled with items :/

Also avoids an expensive copy when fetching account data (broadly,
variant objects are comparatively expensive to copy and fetching
accounts is a hotspot)
This commit is contained in:
Jacek Sieka 2024-09-14 09:47:32 +02:00 committed by GitHub
parent adb8d64377
commit 5cd0297462
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 16 deletions

View File

@ -55,31 +55,31 @@ proc retrieveLeaf(
return err(FetchPathNotFound) return err(FetchPathNotFound)
proc retrieveAccountPayload( proc retrieveAccountLeaf(
db: AristoDbRef; db: AristoDbRef;
accPath: Hash256; accPath: Hash256;
): Result[LeafPayload,AristoError] = ): Result[VertexRef,AristoError] =
if (let leafVtx = db.layersGetAccLeaf(accPath); leafVtx.isSome()): if (let leafVtx = db.layersGetAccLeaf(accPath); leafVtx.isSome()):
if not leafVtx[].isValid(): if not leafVtx[].isValid():
return err(FetchPathNotFound) return err(FetchPathNotFound)
return ok leafVtx[].lData return ok leafVtx[]
if (let leafVtx = db.accLeaves.get(accPath); leafVtx.isSome()): if (let leafVtx = db.accLeaves.get(accPath); leafVtx.isSome()):
if not leafVtx[].isValid(): if not leafVtx[].isValid():
return err(FetchPathNotFound) return err(FetchPathNotFound)
return ok leafVtx[].lData return ok leafVtx[]
# Updated payloads are stored in the layers so if we didn't find them there, # Updated payloads are stored in the layers so if we didn't find them there,
# it must have been in the database # it must have been in the database
let let
leafVtx = db.retrieveLeaf(VertexID(1), accPath.data).valueOr: leafVtx = db.retrieveLeaf(VertexID(1), accPath.data).valueOr:
if error == FetchAccInaccessible: if error == FetchPathNotFound:
return err(FetchPathNotFound) db.accLeaves.put(accPath, nil)
return err(error) return err(error)
db.accLeaves.put(accPath, leafVtx) db.accLeaves.put(accPath, leafVtx)
ok leafVtx.lData ok leafVtx
proc retrieveMerkleHash( proc retrieveMerkleHash(
db: AristoDbRef; db: AristoDbRef;
@ -117,7 +117,7 @@ proc hasAccountPayload(
db: AristoDbRef; db: AristoDbRef;
accPath: Hash256; accPath: Hash256;
): Result[bool,AristoError] = ): Result[bool,AristoError] =
let error = db.retrieveAccountPayload(accPath).errorOr: let error = db.retrieveAccountLeaf(accPath).errorOr:
return ok(true) return ok(true)
if error == FetchPathNotFound: if error == FetchPathNotFound:
@ -131,8 +131,8 @@ proc fetchStorageIdImpl(
): Result[VertexID,AristoError] = ): Result[VertexID,AristoError] =
## Helper function for retrieving a storage (vertex) ID for a given account. ## Helper function for retrieving a storage (vertex) ID for a given account.
let let
payload = ?db.retrieveAccountPayload(accPath) leafVtx = ?db.retrieveAccountLeaf(accPath)
stoID = payload.stoID stoID = leafVtx[].lData.stoID
if stoID.isValid: if stoID.isValid:
ok stoID.vid ok stoID.vid
@ -197,6 +197,8 @@ proc retrieveStoragePayload(
# Updated payloads are stored in the layers so if we didn't find them there, # Updated payloads are stored in the layers so if we didn't find them there,
# it must have been in the database # it must have been in the database
let leafVtx = db.retrieveLeaf(? db.fetchStorageIdImpl(accPath), stoPath.data).valueOr: let leafVtx = db.retrieveLeaf(? db.fetchStorageIdImpl(accPath), stoPath.data).valueOr:
if error == FetchPathNotFound:
db.stoLeaves.put(mixPath, nil)
return err(error) return err(error)
db.stoLeaves.put(mixPath, leafVtx) db.stoLeaves.put(mixPath, leafVtx)
@ -233,10 +235,10 @@ proc fetchAccountRecord*(
): Result[AristoAccount,AristoError] = ): Result[AristoAccount,AristoError] =
## Fetch an account record from the database indexed by `accPath`. ## Fetch an account record from the database indexed by `accPath`.
## ##
let pyl = ? db.retrieveAccountPayload(accPath) let leafVtx = ? db.retrieveAccountLeaf(accPath)
assert pyl.pType == AccountData # debugging only assert leafVtx.lData.pType == AccountData # debugging only
ok pyl.account ok leafVtx.lData.account
proc fetchAccountState*( proc fetchAccountState*(
db: AristoDbRef; db: AristoDbRef;
@ -294,9 +296,7 @@ proc fetchStorageData*(
## For a storage tree related to account `accPath`, fetch the data record ## For a storage tree related to account `accPath`, fetch the data record
## from the database indexed by `path`. ## from the database indexed by `path`.
## ##
let leafVtx = ? db.retrieveLeaf(? db.fetchStorageIdImpl accPath, stoPath.data) db.retrieveStoragePayload(accPath, stoPath)
assert leafVtx.lData.pType == StoData # debugging only
ok leafVtx.lData.stoData
proc fetchStorageState*( proc fetchStorageState*(
db: AristoDbRef; db: AristoDbRef;