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

+20
View File
@@ -182,6 +182,25 @@ static void test_spirv(const char *directory)
assert(!spirv_strip_dual_source(truncated, 5, &out));
}
static void test_handle_map_null_key(void)
{
struct handle_map map = {0};
int first, second;
map_put(&map, 0x1000, &first);
map_put(&map, 0x2000, &second);
for (int i = 0; i < 1000; i++)
assert(!map_remove(&map, 0));
map_put(&map, 0, &first);
assert(map.count == 2 && !map_get(&map, 0));
for (uint64_t key = 1; key <= 30; key++) {
map_put(&map, 0x100000 + key, &first);
assert(map_remove(&map, 0x100000 + key) == &first);
}
assert(map.count == 2 && map.capacity == 64);
assert(map_get(&map, 0x1000) == &first && map_get(&map, 0x2000) == &second);
map_free(&map);
}
static VkResult VKAPI_CALL mock_create_instance(const VkInstanceCreateInfo *info, const VkAllocationCallbacks *allocator,
VkInstance *instance)
{
@@ -464,6 +483,7 @@ static void test_capable_device(void)
int main(int argc, char **argv)
{
test_spirv(argc > 1 ? argv[1] : NULL);
test_handle_map_null_key();
setenv("WINEPREFIX", "/nonexistent", 1);
unsetenv("DROIDDECK_MALI_COMPAT");
+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;