Skip to content

Commit

Permalink
discv5: migrate to minilru (#741)
Browse files Browse the repository at this point in the history
As it happens, the two share the exact same interface (even the test
suite removed in this PR passes) - `minilru` has an edge on efficiency
however, avoiding the doubly linked list node allocations etc
  • Loading branch information
arnetheduck authored Oct 3, 2024
1 parent 5ce3c45 commit 84664b0
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 180 deletions.
3 changes: 2 additions & 1 deletion eth.nimble
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ requires "nim >= 1.6.0",
"confutils",
"testutils",
"unittest2",
"results"
"results",
"minilru"

let nimc = getEnv("NIMC", "nim") # Which nim compiler to use
let lang = getEnv("NIMLANG", "c") # Which backend (c/cpp/js)
Expand Down
45 changes: 4 additions & 41 deletions eth/p2p/discoveryv5/lru.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,9 @@
# at your option. This file may not be copied, modified, or distributed except according to those terms.
#

{.push raises: [].}

import std/[tables, lists], results

export results

type
LRUCache*[K, V] = object of RootObj
list: DoublyLinkedList[(K, V)] # Head is MRU k:v and tail is LRU k:v
table: Table[K, DoublyLinkedNode[(K, V)]] # DoublyLinkedNode is already ref
capacity: int

func init*[K, V](T: type LRUCache[K, V], capacity: int): LRUCache[K, V] =
LRUCache[K, V](capacity: capacity) # Table and list init is done default
{.deprecated: "Use minilru directly".}

func get*[K, V](lru: var LRUCache[K, V], key: K): Opt[V] =
let node = lru.table.getOrDefault(key, nil)
if node.isNil:
return Opt.none(V)

lru.list.remove(node)
lru.list.prepend(node)
return Opt.some(node.value[1])

func put*[K, V](lru: var LRUCache[K, V], key: K, value: V) =
let node = lru.table.getOrDefault(key, nil)
if not node.isNil:
lru.list.remove(node)
else:
if lru.table.len >= lru.capacity:
lru.table.del(lru.list.tail.value[0])
lru.list.remove(lru.list.tail)

lru.list.prepend((key, value))
lru.table[key] = lru.list.head

func del*[K, V](lru: var LRUCache[K, V], key: K) =
var node: DoublyLinkedNode[(K, V)]
if lru.table.pop(key, node):
lru.list.remove(node)
{.push raises: [].}

func len*[K, V](lru: LRUCache[K, V]): int =
lru.table.len
import minilru
export minilru
6 changes: 3 additions & 3 deletions eth/p2p/discoveryv5/sessions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import
std/net,
stint, stew/endians2,
node, lru
./node, minilru

export lru
export minilru

const
aesKeySize* = 128 div 8
Expand All @@ -28,7 +28,7 @@ type
AesKey* = array[aesKeySize, byte]
SessionKey* = array[keySize, byte]
SessionValue* = array[sizeof(AesKey) + sizeof(AesKey), byte]
Sessions* = LRUCache[SessionKey, SessionValue]
Sessions* = LruCache[SessionKey, SessionValue]

func makeKey(id: NodeId, address: Address): SessionKey =
var pos = 0
Expand Down
1 change: 0 additions & 1 deletion tests/p2p/all_discv5_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import
./test_enr,
./test_hkdf,
./test_lru,
./test_ip_vote,
./test_routing_table,
./test_discoveryv5_encoding,
Expand Down
134 changes: 0 additions & 134 deletions tests/p2p/test_lru.nim

This file was deleted.

0 comments on commit 84664b0

Please sign in to comment.