diff --git a/eth.nimble b/eth.nimble index c2d1acf..1cd29d0 100644 --- a/eth.nimble +++ b/eth.nimble @@ -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) diff --git a/eth/p2p/discoveryv5/lru.nim b/eth/p2p/discoveryv5/lru.nim index 77ad5f5..4ec557c 100644 --- a/eth/p2p/discoveryv5/lru.nim +++ b/eth/p2p/discoveryv5/lru.nim @@ -6,46 +6,9 @@ # at your option. This file may not be copied, modified, or distributed except according to those terms. # +{.deprecated: "Use minilru directly".} + {.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 - -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) - -func len*[K, V](lru: LRUCache[K, V]): int = - lru.table.len +import minilru +export minilru diff --git a/eth/p2p/discoveryv5/sessions.nim b/eth/p2p/discoveryv5/sessions.nim index 3df12d0..ae4385c 100644 --- a/eth/p2p/discoveryv5/sessions.nim +++ b/eth/p2p/discoveryv5/sessions.nim @@ -14,9 +14,9 @@ import std/net, stint, stew/endians2, - node, lru + ./node, minilru -export lru +export minilru const aesKeySize* = 128 div 8 @@ -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 diff --git a/tests/p2p/all_discv5_tests.nim b/tests/p2p/all_discv5_tests.nim index b20317b..95a81dc 100644 --- a/tests/p2p/all_discv5_tests.nim +++ b/tests/p2p/all_discv5_tests.nim @@ -3,7 +3,6 @@ import ./test_enr, ./test_hkdf, - ./test_lru, ./test_ip_vote, ./test_routing_table, ./test_discoveryv5_encoding, diff --git a/tests/p2p/test_lru.nim b/tests/p2p/test_lru.nim deleted file mode 100644 index 26ec5f4..0000000 --- a/tests/p2p/test_lru.nim +++ /dev/null @@ -1,134 +0,0 @@ -{.used.} - -import - std/options, - unittest2, - ../../eth/p2p/discoveryv5/lru - -suite "LRUCache": - const - capacity = 10 - target = 4 - test "LRU value gets removed": - var lru = LRUCache[int, int].init(capacity = capacity) - - # Fully fill the LRU - for i in 0..