aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhanabi1224 <harlowmoo@gmail.com>2022-04-19 20:46:33 +0800
committerhanabi1224 <harlowmoo@gmail.com>2022-04-19 20:46:33 +0800
commitded8342f3fdb176d6c264057f3bdcb890bfcfad7 (patch)
treef78c19cf2d7026d1c7a272274391249d2de39b74
parentd99ba9c073bcddce25a92d5af9afd59711df29d6 (diff)
Reduce allocations
-rw-r--r--core/container/lru/lru_cache.odin18
1 files 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)