fix(venus): destroying a null handle no longer grows the Mali layer's maps (#3101)

The handle maps use key 0 for an empty slot. vkDestroyBuffer, vkFreeMemory and the other destroy
calls accept VK_NULL_HANDLE, and map_remove(0) matched the first empty slot it reached, so every
such call lowered count without removing anything. Once count wrapped below zero, every map_put
saw a full table and doubled it, and each rehash took twice as long as the one before.

Steam's web helper frees null buffers about 50,000 times in a few minutes. On a Mali-G610 its UI
stalled periodically for 0.2 s, 0.37 s, 0.75 s, 1.5 s and then 3 s, and the buffer map reported a
count of 4294967295.

map_get, map_put and map_remove now ignore key 0. On a Nothing Phone (2a) running 0.7.1 with this
change, the buffer map stayed at 256 slots after 115,000 frees and the stalls were gone.
This commit is contained in:
LarzVader authored and GitHub committed 2026-11-24 16:16:19 -05:00
1 parent 1f82db66ac
commit 6e252ea3ad
2 files changed
+26 -2

No files matched your search

+6 -2
View File
@@ -60,9 +60,11 @@ static uint64_t hash(uint64_t key)
return key;
}
/* Key 0 marks an empty slot, so VK_NULL_HANDLE is never looked up, stored or removed. Destroying a
* null handle is valid Vulkan, and map_remove(0) would otherwise match an empty slot and drop count. */
void *map_get(const struct handle_map *map, uint64_t key)
{
if (!map->capacity)
if (!map->capacity || !key)
return NULL;
for (uint32_t i = hash(key) & (map->capacity - 1);; i = (i + 1) & (map->capacity - 1)) {
if (map->keys[i] == key)
@@ -90,6 +92,8 @@ static void map_grow(struct handle_map *map)
void map_put(struct handle_map *map, uint64_t key, void *value)
{
if (!key)
return;
if ((map->count + 1) * 4 > map->capacity * 3)
map_grow(map);
uint32_t i = hash(key) & (map->capacity - 1);
@@ -103,7 +107,7 @@ void map_put(struct handle_map *map, uint64_t key, void *value)
void *map_remove(struct handle_map *map, uint64_t key)
{
if (!map->capacity)
if (!map->capacity || !key)
return NULL;
uint32_t mask = map->capacity - 1;
uint32_t i = hash(key) & mask;