Skip to content

Commit 6939b1c

Browse files
committed
Add empty / miss lru cache
Empties were handled by the leaf cache previously
1 parent 3a31dce commit 6939b1c

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

execution_chain/db/aristo/aristo_init/rocks_db.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ proc rocksDbBackend*(
245245

246246
iterator walkVtx*(
247247
be: RdbBackendRef;
248-
kinds = {Branch, ExtBranch, AccLeaf, StoLeaf};
248+
kinds = {VertexType.Branch, VertexType.ExtBranch, AccLeaf, StoLeaf};
249249
): tuple[evid: RootedVertexID, vtx: VertexRef] =
250250
## Variant of `walk()` iteration over the vertex sub-table.
251251
for (rvid, vtx) in be.rdb.walkVtx(kinds):

execution_chain/db/aristo/aristo_init/rocks_db/rdb_desc.nim

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ type
6262
rdBranchLru*: LruCache[VertexID, (VertexID, uint16)]
6363
rdBranchSize*: int
6464

65+
rdEmptyLru*: LruCache[VertexID,tuple[]] ## Read cache
66+
6567
AristoCFs* = enum
6668
## Column family symbols/handles and names used on the database
6769
AdmCF = "AriAdm" ## Admin column family name
@@ -73,14 +75,20 @@ type
7375
Account
7476
World
7577

78+
RdbVertexType* = enum
79+
Empty
80+
Leaf
81+
Branch
82+
ExtBranch
83+
7684
var
7785
# Hit/miss counters for LRU cache - global so as to integrate easily with
7886
# nim-metrics and `uint64` to ensure that increasing them is fast - collection
7987
# happens from a separate thread.
8088
# TODO maybe turn this into more general framework for LRU reporting since
8189
# we have lots of caches of this sort
8290
rdbBranchLruStats*: array[RdbStateType, RdbLruCounter]
83-
rdbVtxLruStats*: array[RdbStateType, array[VertexType, RdbLruCounter]]
91+
rdbVtxLruStats*: array[RdbStateType, array[RdbVertexType, RdbLruCounter]]
8492
rdbKeyLruStats*: array[RdbStateType, RdbLruCounter]
8593

8694
# ------------------------------------------------------------------------------
@@ -93,6 +101,12 @@ template toOpenArray*(xid: AdminTabID): openArray[byte] =
93101
template to*(v: RootedVertexID, T: type RdbStateType): RdbStateType =
94102
if v.root == STATE_ROOT_VID: RdbStateType.World else: RdbStateType.Account
95103

104+
template to*(v: VertexType, T: type RdbVertexType): RdbVertexType =
105+
case v
106+
of VertexType.AccLeaf, VertexType.StoLeaf: RdbVertexType.Leaf
107+
of VertexType.Branch: RdbVertexType.Branch
108+
of VertexType.ExtBranch: RdbVertexType.ExtBranch
109+
96110
template inc*(v: var RdbLruCounter, hit: bool) =
97111
discard v[hit].fetchAdd(1, moRelaxed)
98112

execution_chain/db/aristo/aristo_init/rocks_db/rdb_get.nim

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ when defined(metrics):
5656
# We don't care about synchronization between each type of metric or between
5757
# the metrics thread and others since small differences like this don't matter
5858
for state in RdbStateType:
59-
for vtype in VertexType:
59+
for vtype in RdbVertexType:
6060
for hit in [false, true]:
6161
output(
6262
name = "aristo_rdb_vtx_lru_total",
@@ -191,9 +191,21 @@ proc getVtx*(
191191
rdb.rdVtxLru.get(rvid.vid)
192192

193193
if rc.isOk:
194-
rdbVtxLruStats[rvid.to(RdbStateType)][rc.value().vType].inc(true)
194+
rdbVtxLruStats[rvid.to(RdbStateType)][rc.value().vType.to(RdbVertexType)].inc(
195+
true
196+
)
195197
return ok(move(rc.value))
196198

199+
block:
200+
var rc =
201+
if GetVtxFlag.PeekCache in flags:
202+
rdb.rdEmptyLru.peek(rvid.vid)
203+
else:
204+
rdb.rdEmptyLru.get(rvid.vid)
205+
if rc.isOk():
206+
rdbVtxLruStats[rvid.to(RdbStateType)][RdbVertexType.Empty].inc(true)
207+
return ok(VertexRef(nil))
208+
197209
# Otherwise fetch from backend database
198210
# A threadvar is used to avoid allocating an environment for onData
199211
var res {.threadvar.}: Result[VertexRef, AristoError]
@@ -207,8 +219,10 @@ proc getVtx*(
207219
return err((errSym, error))
208220

209221
if not gotData:
210-
# As a hack, we count missing data as leaf nodes
211-
rdbVtxLruStats[rvid.to(RdbStateType)][VertexType.StoLeaf].inc(false)
222+
rdbVtxLruStats[rvid.to(RdbStateType)][RdbVertexType.Empty].inc(false)
223+
if GetVtxFlag.PeekCache notin flags:
224+
rdb.rdEmptyLru.put(rvid.vid, default(tuple[])) # TODO LRU void support
225+
212226
return ok(VertexRef(nil))
213227

214228
if res.isErr():
@@ -217,7 +231,9 @@ proc getVtx*(
217231
if res.value.vType == Branch:
218232
rdbBranchLruStats[rvid.to(RdbStateType)].inc(false)
219233
else:
220-
rdbVtxLruStats[rvid.to(RdbStateType)][res.value().vType].inc(false)
234+
rdbVtxLruStats[rvid.to(RdbStateType)][res.value().vType.to(RdbVertexType)].inc(
235+
false
236+
)
221237

222238
# Update cache and return - in peek mode, avoid evicting cache items
223239
if GetVtxFlag.PeekCache notin flags:

execution_chain/db/aristo/aristo_init/rocks_db/rdb_init.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ proc dumpCacheStats(keySize, vtxSize, branchSize: int) =
2727
echo "vtxLru(", vtxSize, ")"
2828
echo " state vtype miss hit total hitrate"
2929
for state in RdbStateType:
30-
for vtype in VertexType:
30+
for vtype in RdbVertexType:
3131
let
3232
(miss, hit) = (
3333
rdbVtxLruStats[state][vtype].get(false),
@@ -99,6 +99,7 @@ proc init*(rdb: var RdbInst, opts: DbOptions, baseDb: RocksDbInstanceRef) =
9999
rdb.rdKeyLru = typeof(rdb.rdKeyLru).init(rdb.rdKeySize)
100100
rdb.rdVtxLru = typeof(rdb.rdVtxLru).init(rdb.rdVtxSize)
101101
rdb.rdBranchLru = typeof(rdb.rdBranchLru).init(rdb.rdBranchSize)
102+
rdb.rdEmptyLru = typeof(rdb.rdEmptyLru).init(rdb.rdVtxSize) # reuse vtx cache size
102103

103104
if opts.rdbPrintStats:
104105
let

execution_chain/db/aristo/aristo_init/rocks_db/rdb_put.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ proc putVtx*(
105105
else:
106106
discard rdb.rdVtxLru.update(rvid.vid, vtx)
107107

108+
rdb.rdEmptyLru.del(rvid.vid)
109+
108110
if key.isValid:
109111
if rdb.rdKeyLru.len < rdb.rdKeyLru.capacity:
110112
rdb.rdKeyLru.put(rvid.vid, key)
@@ -126,6 +128,9 @@ proc putVtx*(
126128
rdb.rdVtxLru.del rvid.vid
127129
rdb.rdKeyLru.del rvid.vid
128130

131+
if rdb.rdEmptyLru.len < rdb.rdEmptyLru.capacity:
132+
rdb.rdEmptyLru.put(rvid.vid, default(tuple[]))
133+
129134
ok()
130135

131136
# ------------------------------------------------------------------------------

0 commit comments

Comments
 (0)