mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 14:48:47 +00:00
Reduce allocations
This commit is contained in:
@@ -65,20 +65,22 @@ set :: proc(c: ^$C/Cache($Key, $Value), key: Key, value: Value) -> runtime.Alloc
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
e := new(Node(Key, Value), c.node_allocator) or_return
|
e : ^Node(Key, Value) = nil
|
||||||
e.key = key
|
|
||||||
e.value = value
|
|
||||||
|
|
||||||
assert(c.count <= c.capacity)
|
assert(c.count <= c.capacity)
|
||||||
if c.count == c.capacity {
|
if c.count == c.capacity {
|
||||||
_remove_node(c, c.tail)
|
e = c.tail
|
||||||
|
_remove_node(c, e)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
c.count += 1
|
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
|
c.entries[key] = e
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,6 +130,7 @@ remove :: proc(c: ^$C/Cache($Key, $Value), key: Key) -> bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
_remove_node(c, e)
|
_remove_node(c, e)
|
||||||
|
free(node, c.node_allocator)
|
||||||
c.count -= 1
|
c.count -= 1
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -153,9 +156,6 @@ _remove_node :: proc(c: ^$C/Cache($Key, $Value), node: ^Node(Key, Value)) {
|
|||||||
delete_key(&c.entries, node.key)
|
delete_key(&c.entries, node.key)
|
||||||
|
|
||||||
_call_on_remove(c, node)
|
_call_on_remove(c, node)
|
||||||
|
|
||||||
free(node, c.node_allocator)
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@(private)
|
@(private)
|
||||||
|
|||||||
Reference in New Issue
Block a user