From 965b8c26bbedcb481d224ad0056d68c76dbdf30e Mon Sep 17 00:00:00 2001 From: Benjamin Otte Date: Sun, 23 Jul 2023 18:31:54 +0200 Subject: [PATCH] vulkan: Add a cache allocation to buddy allocator Instead of immediately freeing memory, keep one chunk around, because surely somebody is gonna do another allocation soon. --- gsk/vulkan/gskvulkanmemory.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/gsk/vulkan/gskvulkanmemory.c b/gsk/vulkan/gskvulkanmemory.c index d5338c8cd2..9266d2c670 100644 --- a/gsk/vulkan/gskvulkanmemory.c +++ b/gsk/vulkan/gskvulkanmemory.c @@ -171,6 +171,7 @@ struct _GskVulkanBuddyAllocator gsize block_size_slot; + GskVulkanAllocation cache; GskVulkanAllocationList free_lists[N_SUBDIVISIONS]; }; @@ -180,6 +181,9 @@ gsk_vulkan_buddy_allocator_free_allocator (GskVulkanAllocator *allocator) GskVulkanBuddyAllocator *self = (GskVulkanBuddyAllocator *) allocator; gsize i; + if (self->cache.vk_memory) + gsk_vulkan_free (self->allocator, &self->cache); + for (i = 0; i < N_SUBDIVISIONS; i++) { gsk_vulkan_allocation_list_clear (&self->free_lists[i]); @@ -226,10 +230,18 @@ gsk_vulkan_buddy_allocator_alloc (GskVulkanAllocator *allocator, } if (i < 0) { - /* We force alignment to our size, so that we can use offset - * to find the buddy allocation. - */ - gsk_vulkan_alloc (self->allocator, 1 << self->block_size_slot, 1 << self->block_size_slot, alloc); + if (self->cache.vk_memory) + { + *alloc = self->cache; + self->cache.vk_memory = NULL; + } + else + { + /* We force alignment to our size, so that we can use offset + * to find the buddy allocation. + */ + gsk_vulkan_alloc (self->allocator, 1 << self->block_size_slot, 1 << self->block_size_slot, alloc); + } } else { @@ -283,7 +295,10 @@ restart: alloc->size <<= 1; if (slot == 0) { - gsk_vulkan_free (self->allocator, alloc); + if (self->cache.vk_memory == NULL) + self->cache = *alloc; + else + gsk_vulkan_free (self->allocator, alloc); return; } else