From ded8342f3fdb176d6c264057f3bdcb890bfcfad7 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Tue, 19 Apr 2022 20:46:33 +0800 Subject: [PATCH] Reduce allocations --- core/container/lru/lru_cache.odin | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/core/container/lru/lru_cache.odin b/core/container/lru/lru_cache.odin index 81f0142b0..b59f29f0c 100644 --- a/core/container/lru/lru_cache.odin +++ b/core/container/lru/lru_cache.odin @@ -65,20 +65,22 @@ set :: proc(c: ^$C/Cache($Key, $Value), key: Key, value: Value) -> runtime.Alloc return nil } - e := new(Node(Key, Value), c.node_allocator) or_return - e.key = key - e.value = value - + e : ^Node(Key, Value) = nil assert(c.count <= c.capacity) if c.count == c.capacity { - _remove_node(c, c.tail) + e = c.tail + _remove_node(c, e) } else { c.count += 1 + e = new(Node(Key, Value), c.node_allocator) or_return } - _push_front_node(c, e) + e.key = key + e.value = value + _push_front_node(c, e) c.entries[key] = e + return nil } @@ -128,6 +130,7 @@ remove :: proc(c: ^$C/Cache($Key, $Value), key: Key) -> bool { return false } _remove_node(c, e) + free(node, c.node_allocator) c.count -= 1 return true } @@ -153,9 +156,6 @@ _remove_node :: proc(c: ^$C/Cache($Key, $Value), node: ^Node(Key, Value)) { delete_key(&c.entries, node.key) _call_on_remove(c, node) - - free(node, c.node_allocator) - } @(private)