2021-05-21 11:33:38 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
|
|
|
## Hash as hash can: LRU cache
|
|
|
|
## ===========================
|
|
|
|
##
|
2021-05-25 17:38:39 +00:00
|
|
|
## This module provides a generic last-recently-used cache data structure.
|
|
|
|
##
|
|
|
|
## The implementation works with the same complexity as the worst case of a
|
|
|
|
## nim hash tables operation. This is is assumed to be O(1) in most cases
|
|
|
|
## (so long as the table does not degrade into one-bucket linear mode, or
|
|
|
|
## some bucket-adjustment algorithm takes over.)
|
2021-05-21 18:44:30 +00:00
|
|
|
##
|
2021-05-25 18:52:09 +00:00
|
|
|
## For consistency with every other data type in Nim these have value
|
|
|
|
## semantics, this means that `=` performs a deep copy of the LRU cache.
|
|
|
|
##
|
2021-05-21 11:33:38 +00:00
|
|
|
|
|
|
|
import
|
2021-05-22 14:22:01 +00:00
|
|
|
math,
|
2021-05-25 17:38:39 +00:00
|
|
|
eth/rlp,
|
2021-05-21 11:33:38 +00:00
|
|
|
stew/results,
|
|
|
|
tables
|
|
|
|
|
|
|
|
export
|
|
|
|
results
|
|
|
|
|
|
|
|
type
|
2021-05-25 17:38:39 +00:00
|
|
|
LruKey*[T,K] = ## User provided handler function, derives an
|
|
|
|
## LRU `key` from function argument `arg`. The
|
|
|
|
## `key` is used to index the cache data.
|
2021-05-21 11:33:38 +00:00
|
|
|
proc(arg: T): K {.gcsafe, raises: [Defect,CatchableError].}
|
|
|
|
|
2021-05-25 17:38:39 +00:00
|
|
|
LruValue*[T,V,E] = ## User provided handler function, derives an
|
|
|
|
## LRU `value` from function argument `arg`.
|
2021-05-21 11:33:38 +00:00
|
|
|
proc(arg: T): Result[V,E] {.gcsafe, raises: [Defect,CatchableError].}
|
|
|
|
|
2021-05-25 17:38:39 +00:00
|
|
|
LruItem*[K,V] = ## Doubly linked hash-tab item encapsulating
|
|
|
|
## the `value` (which is the result from
|
|
|
|
## `LruValue` handler function.
|
|
|
|
tuple[prv, nxt: K, value: V]
|
|
|
|
|
|
|
|
# There could be {.rlpCustomSerialization.} annotation for the tab field.
|
|
|
|
# As there was a problem with the automatic Rlp serialisation for generic
|
|
|
|
# type, the easier solution was an all manual read()/append() for the whole
|
|
|
|
# generic LruCacheData[K,V] type.
|
|
|
|
LruData[K,V] = object
|
|
|
|
maxItems: int ## Max number of entries
|
|
|
|
first, last: K ## Doubly linked item list queue
|
2021-05-25 18:52:09 +00:00
|
|
|
tab: Table[K,LruItem[K,V]] ## (`key`,encapsulated(`value`)) data table
|
2021-05-21 18:44:30 +00:00
|
|
|
|
2021-05-21 11:33:38 +00:00
|
|
|
LruCache*[T,K,V,E] = object
|
2021-05-25 17:38:39 +00:00
|
|
|
data*: LruData[K,V] ## Cache data, can be serialised
|
|
|
|
toKey: LruKey[T,K] ## Handler function, derives `key`
|
|
|
|
toValue: LruValue[T,V,E] ## Handler function, derives `value`
|
2021-05-21 11:33:38 +00:00
|
|
|
|
2021-06-14 18:33:57 +00:00
|
|
|
{.push raises: [Defect].}
|
2021-05-21 11:33:38 +00:00
|
|
|
|
2021-05-25 17:38:39 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Private functions
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc `==`[K,V](a, b: var LruData[K,V]): bool =
|
|
|
|
a.maxItems == b.maxItems and
|
|
|
|
a.first == b.first and
|
|
|
|
a.last == b.last and
|
|
|
|
a.tab == b.tab
|
|
|
|
|
2021-05-21 11:33:38 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
2021-07-14 15:13:27 +00:00
|
|
|
# Public constructor and reset
|
2021-05-21 11:33:38 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc clearCache*[T,K,V,E](cache: var LruCache[T,K,V,E]; cacheInitSize = 0)
|
|
|
|
{.gcsafe, raises: [Defect].} =
|
|
|
|
## Reset/clear an initialised LRU cache. The cache will be re-allocated
|
|
|
|
## with `cacheInitSize` initial spaces if this is positive, or `cacheMaxItems`
|
|
|
|
## spaces (see `initLruCache()`) as a default.
|
|
|
|
var initSize = cacheInitSize
|
|
|
|
if initSize <= 0:
|
|
|
|
initSize = cache.data.maxItems
|
2021-05-25 17:38:39 +00:00
|
|
|
cache.data.first.reset
|
|
|
|
cache.data.last.reset
|
2021-07-14 15:13:27 +00:00
|
|
|
cache.data.tab = initTable[K,LruItem[K,V]](initSize.nextPowerOfTwo)
|
2021-05-25 17:38:39 +00:00
|
|
|
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc initCache*[T,K,V,E](cache: var LruCache[T,K,V,E];
|
|
|
|
toKey: LruKey[T,K], toValue: LruValue[T,V,E];
|
|
|
|
cacheMaxItems = 10; cacheInitSize = 0)
|
|
|
|
{.gcsafe, raises: [Defect].} =
|
|
|
|
## Initialise LRU cache. The handlers `toKey()` and `toValue()` are explained
|
|
|
|
## at the data type definition. The cache will be allocated with
|
|
|
|
## `cacheInitSize` initial spaces if this is positive, or `cacheMaxItems`
|
|
|
|
## spaces (see `initLruCache()`) as a default.
|
2021-05-25 17:38:39 +00:00
|
|
|
cache.data.maxItems = cacheMaxItems
|
2021-05-21 11:33:38 +00:00
|
|
|
cache.toKey = toKey
|
|
|
|
cache.toValue = toValue
|
2021-07-14 15:13:27 +00:00
|
|
|
cache.clearCache
|
2021-05-21 11:33:38 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, basic mechanism
|
|
|
|
# ------------------------------------------------------------------------------
|
2021-05-21 11:33:38 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
proc getItem*[T,K,V,E](lru: var LruCache[T,K,V,E];
|
2022-04-08 04:54:11 +00:00
|
|
|
arg: T; peekOk = false): Result[V,E]
|
2021-07-14 15:13:27 +00:00
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## If the key `lru.toKey(arg)` is a cached key, the associated value will
|
|
|
|
## be returnd. If the `peekOK` argument equals `false`, the associated
|
|
|
|
## key-value pair will have been moved to the end of the LRU queue.
|
|
|
|
##
|
|
|
|
## If the key `lru.toKey(arg)` is not a cached key and the LRU queue has at
|
|
|
|
## least `cacheMaxItems` entries (see `initLruCache()`, the first key-value
|
|
|
|
## pair will be removed from the LRU queue. Then the value the pair
|
|
|
|
## (`lru.toKey(arg)`,`lru.toValue(arg)`) will be appended to the LRU queue
|
|
|
|
## and the value part returned.
|
|
|
|
##
|
2021-05-25 17:38:39 +00:00
|
|
|
let key = lru.toKey(arg)
|
2021-05-21 11:33:38 +00:00
|
|
|
|
2021-05-21 18:44:30 +00:00
|
|
|
# Relink item if already in the cache => move to last position
|
2021-05-25 17:38:39 +00:00
|
|
|
if lru.data.tab.hasKey(key):
|
|
|
|
let lruItem = lru.data.tab[key]
|
2021-05-21 18:44:30 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
if peekOk or key == lru.data.last:
|
2021-05-21 18:44:30 +00:00
|
|
|
# Nothing to do
|
|
|
|
return ok(lruItem.value)
|
|
|
|
|
|
|
|
# Unlink key Item
|
2021-05-25 17:38:39 +00:00
|
|
|
if key == lru.data.first:
|
|
|
|
lru.data.first = lruItem.nxt
|
2021-05-21 18:44:30 +00:00
|
|
|
else:
|
2021-05-25 17:38:39 +00:00
|
|
|
lru.data.tab[lruItem.prv].nxt = lruItem.nxt
|
|
|
|
lru.data.tab[lruItem.nxt].prv = lruItem.prv
|
2021-05-21 18:44:30 +00:00
|
|
|
|
|
|
|
# Append key item
|
2021-05-25 17:38:39 +00:00
|
|
|
lru.data.tab[lru.data.last].nxt = key
|
|
|
|
lru.data.tab[key].prv = lru.data.last
|
|
|
|
lru.data.last = key
|
2021-05-21 18:44:30 +00:00
|
|
|
return ok(lruItem.value)
|
|
|
|
|
|
|
|
# Calculate value, pass through error unless OK
|
2021-05-25 17:38:39 +00:00
|
|
|
let rcValue = ? lru.toValue(arg)
|
2021-05-21 11:33:38 +00:00
|
|
|
|
2021-05-21 18:44:30 +00:00
|
|
|
# Limit number of cached items
|
2021-05-25 17:38:39 +00:00
|
|
|
if lru.data.maxItems <= lru.data.tab.len:
|
2021-05-21 11:33:38 +00:00
|
|
|
# Delete oldest/first entry
|
2021-05-25 17:38:39 +00:00
|
|
|
var nextKey = lru.data.tab[lru.data.first].nxt
|
|
|
|
lru.data.tab.del(lru.data.first)
|
|
|
|
lru.data.first = nextKey
|
2021-05-21 11:33:38 +00:00
|
|
|
|
|
|
|
# Add cache entry
|
2021-05-21 18:44:30 +00:00
|
|
|
var tabItem: LruItem[K,V]
|
|
|
|
|
|
|
|
# Initialise empty queue
|
2021-05-25 17:38:39 +00:00
|
|
|
if lru.data.tab.len == 0:
|
|
|
|
lru.data.first = key
|
|
|
|
lru.data.last = key
|
2021-05-21 18:44:30 +00:00
|
|
|
else:
|
|
|
|
# Append queue item
|
2021-05-25 17:38:39 +00:00
|
|
|
lru.data.tab[lru.data.last].nxt = key
|
|
|
|
tabItem.prv = lru.data.last
|
|
|
|
lru.data.last = key
|
2021-05-21 18:44:30 +00:00
|
|
|
|
|
|
|
tabItem.value = rcValue
|
2021-05-25 17:38:39 +00:00
|
|
|
lru.data.tab[key] = tabItem
|
2021-05-21 11:33:38 +00:00
|
|
|
result = ok(rcValue)
|
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, cache info
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc hasKey*[T,K,V,E](lru: var LruCache[T,K,V,E]; arg: T): bool {.gcsafe.} =
|
|
|
|
## Check whether the `arg` argument is cached
|
|
|
|
let key = lru.toKey(arg)
|
|
|
|
lru.data.tab.hasKey(key)
|
|
|
|
|
|
|
|
proc firstKey*[T,K,V,E](lru: var LruCache[T,K,V,E]): K {.gcsafe.} =
|
|
|
|
## Returns the key of the first item in the LRU queue, or the reset
|
|
|
|
## value it the cache is empty.
|
|
|
|
if 0 < lru.data.tab.len:
|
|
|
|
result = lru.data.first
|
|
|
|
|
|
|
|
proc lastKey*[T,K,V,E](lru: var LruCache[T,K,V,E]): K {.gcsafe.} =
|
|
|
|
## Returns the key of the last item in the LRU queue, or the reset
|
|
|
|
## value it the cache is empty.
|
|
|
|
if 0 < lru.data.tab.len:
|
|
|
|
result = lru.data.last
|
|
|
|
|
|
|
|
|
|
|
|
proc maxLen*[T,K,V,E](lru: var LruCache[T,K,V,E]): int {.gcsafe.} =
|
|
|
|
## Maximal number of cache entries.
|
|
|
|
lru.data.maxItems
|
|
|
|
|
|
|
|
proc len*[T,K,V,E](lru: var LruCache[T,K,V,E]): int {.gcsafe.} =
|
|
|
|
## Return the number of elements in the cache.
|
|
|
|
lru.data.tab.len
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, advanced features
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
proc setItem*[T,K,V,E](lru: var LruCache[T,K,V,E]; arg: T; value: V): bool
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## Update entry with key `lru.toKey(arg)` by `value`. Reurns `true` if the
|
|
|
|
## key exists in the database, and false otherwise.
|
|
|
|
##
|
|
|
|
## This function allows for simlifying the `toValue()` function (see
|
|
|
|
## `initLruCache()`) to provide a placeholder only and later fill this
|
|
|
|
## slot with this `setLruItem()` function.
|
|
|
|
let key = lru.toKey(arg)
|
|
|
|
if lru.data.tab.hasKey(key):
|
|
|
|
lru.data.tab[key].value = value
|
|
|
|
return true
|
2022-04-08 04:54:11 +00:00
|
|
|
|
2021-07-14 15:13:27 +00:00
|
|
|
|
|
|
|
proc delItem*[T,K,V,E](lru: var LruCache[T,K,V,E]; arg: T): bool
|
|
|
|
{.gcsafe, discardable, raises: [Defect,KeyError].} =
|
|
|
|
## Delete the `arg` argument from cached. That way, the LRU cache can
|
|
|
|
## be re-purposed as a sequence with efficient random delete facility.
|
|
|
|
let key = lru.toKey(arg)
|
|
|
|
|
|
|
|
# Relink item if already in the cache => move to last position
|
|
|
|
if lru.data.tab.hasKey(key):
|
|
|
|
let lruItem = lru.data.tab[key]
|
|
|
|
|
|
|
|
# Unlink key Item
|
|
|
|
if lru.data.tab.len == 1:
|
|
|
|
lru.data.first.reset
|
|
|
|
lru.data.last.reset
|
|
|
|
elif key == lru.data.last:
|
|
|
|
lru.data.last = lruItem.prv
|
|
|
|
elif key == lru.data.first:
|
|
|
|
lru.data.first = lruItem.nxt
|
|
|
|
else:
|
|
|
|
lru.data.tab[lruItem.prv].nxt = lruItem.nxt
|
|
|
|
lru.data.tab[lruItem.nxt].prv = lruItem.prv
|
|
|
|
|
|
|
|
lru.data.tab.del(key)
|
|
|
|
return true
|
|
|
|
|
|
|
|
|
|
|
|
iterator keyItemPairs*[T,K,V,E](lru: var LruCache[T,K,V,E]): (K,LruItem[K,V])
|
|
|
|
{.gcsafe, raises: [Defect,CatchableError].} =
|
|
|
|
## Cycle through all (key,lruItem) pairs in chronological order.
|
|
|
|
if 0 < lru.data.tab.len:
|
|
|
|
var key = lru.data.first
|
|
|
|
for _ in 0 ..< lru.data.tab.len - 1:
|
|
|
|
var item = lru.data.tab[key]
|
|
|
|
yield (key, item)
|
|
|
|
key = item.nxt
|
|
|
|
yield (key, lru.data.tab[key])
|
|
|
|
if key != lru.data.last:
|
|
|
|
raiseAssert "Garbled LRU cache next/prv references"
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# Public functions, RLP support
|
|
|
|
# ------------------------------------------------------------------------------
|
2021-05-21 11:33:38 +00:00
|
|
|
|
2021-05-25 17:38:39 +00:00
|
|
|
proc `==`*[T,K,V,E](a, b: var LruCache[T,K,V,E]): bool =
|
|
|
|
## Returns `true` if both argument LRU caches contain the same data
|
|
|
|
## regardless of `toKey()`/`toValue()` handler functions.
|
|
|
|
a.data == b.data
|
|
|
|
|
|
|
|
|
2021-06-14 18:33:57 +00:00
|
|
|
proc append*[K,V](rw: var RlpWriter; data: LruData[K,V]) {.
|
|
|
|
inline, raises: [Defect,KeyError].} =
|
2021-05-25 17:38:39 +00:00
|
|
|
## Generic support for `rlp.encode(lru.data)` for serialising the data
|
|
|
|
## part of an LRU cache.
|
|
|
|
rw.append(data.maxItems)
|
|
|
|
rw.append(data.first)
|
|
|
|
rw.append(data.last)
|
|
|
|
rw.startList(data.tab.len)
|
2021-05-25 17:44:55 +00:00
|
|
|
# store keys in LRU order
|
|
|
|
if 0 < data.tab.len:
|
|
|
|
var key = data.first
|
|
|
|
for _ in 0 ..< data.tab.len - 1:
|
|
|
|
var value = data.tab[key]
|
|
|
|
rw.append((key, value))
|
|
|
|
key = value.nxt
|
|
|
|
rw.append((key, data.tab[key]))
|
|
|
|
if key != data.last:
|
|
|
|
raiseAssert "Garbled LRU cache next/prv references"
|
2021-05-25 17:38:39 +00:00
|
|
|
|
2021-06-14 18:33:57 +00:00
|
|
|
proc read*[K,V](rlp: var Rlp; Q: type LruData[K,V]): Q {.
|
|
|
|
inline, raises: [Defect,RlpError].} =
|
2021-05-25 17:38:39 +00:00
|
|
|
## Generic support for `rlp.decode(bytes)` for loading the data part
|
|
|
|
## of an LRU cache from a serialised data stream.
|
|
|
|
result.maxItems = rlp.read(int)
|
|
|
|
result.first = rlp.read(K)
|
|
|
|
result.last = rlp.read(K)
|
|
|
|
for w in rlp.items:
|
|
|
|
let (key,value) = w.read((K,LruItem[K,V]))
|
|
|
|
result.tab[key] = value
|
|
|
|
|
2021-05-21 11:33:38 +00:00
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# End
|
|
|
|
# ------------------------------------------------------------------------------
|