Update LRU complexity comment

why:
  hash tables might worst-case degrade into linear mode, so it is not
  strictly O(1)
This commit is contained in:
Jordan Hrycaj 2021-05-22 15:22:01 +01:00 committed by Jordan Hrycaj
parent 1965ea027b
commit 179cc75c32
1 changed files with 6 additions and 5 deletions

View File

@ -11,9 +11,10 @@
## Hash as hash can: LRU cache
## ===========================
##
## Provide last-recently-used cache data structure. The implementation
## works with complexity O(1) using a nim hash Table with doubly linked
## data entries.
## Provide last-recently-used cache data structure. The implementation works
## with the same complexity as the worst case of a nim hash tables operation
## which is assumed ~O(1) in most cases (so long as the table does not degrade
## into one-bucket linear mode, or some adjustment algorithm.)
##
const
@ -22,6 +23,7 @@ const
isMainOk {.used.} = noisy > 2
import
math,
stew/results,
tables
@ -59,8 +61,7 @@ proc initLruCache*[T,K,V,E](cache: var LruCache[T,K,V,E];
cache.maxItems = cacheMaxItems
cache.toKey = toKey
cache.toValue = toValue
# note: Starting from Nim v0.20, tables are initialized by default and it
# is not necessary to call initOrderedTable() function explicitly.
cache.tab = initTable[K,LruItem[K,V]](cacheMaxItems.nextPowerOfTwo)
proc getLruItem*[T,K,V,E](cache: var LruCache[T,K,V,E]; arg: T): Result[V,E] =