From 746dddc0673d7261f19b1e056e90e6e3a49ef33a Mon Sep 17 00:00:00 2001 From: reduz Date: Fri, 13 May 2022 15:04:37 +0200 Subject: Replace most uses of Map by HashMap * Map is unnecessary and inefficient in almost every case. * Replaced by the new HashMap. * Renamed Map to RBMap and Set to RBSet for cases that still make sense (order matters) but use is discouraged. There were very few cases where replacing by HashMap was undesired because keeping the key order was intended. I tried to keep those (as RBMap) as much as possible, but might have missed some. Review appreciated! --- drivers/vulkan/rendering_device_vulkan.cpp | 60 +++++++++++++++--------------- drivers/vulkan/rendering_device_vulkan.h | 22 +++++------ drivers/vulkan/vulkan_context.h | 4 +- 3 files changed, 43 insertions(+), 43 deletions(-) (limited to 'drivers/vulkan') diff --git a/drivers/vulkan/rendering_device_vulkan.cpp b/drivers/vulkan/rendering_device_vulkan.cpp index 5102eab979..23c2d764b8 100644 --- a/drivers/vulkan/rendering_device_vulkan.cpp +++ b/drivers/vulkan/rendering_device_vulkan.cpp @@ -132,13 +132,13 @@ static void update_external_dependency_for_store(VkSubpassDependency &dependency void RenderingDeviceVulkan::_add_dependency(RID p_id, RID p_depends_on) { if (!dependency_map.has(p_depends_on)) { - dependency_map[p_depends_on] = Set(); + dependency_map[p_depends_on] = RBSet(); } dependency_map[p_depends_on].insert(p_id); if (!reverse_dependency_map.has(p_id)) { - reverse_dependency_map[p_id] = Set(); + reverse_dependency_map[p_id] = RBSet(); } reverse_dependency_map[p_id].insert(p_depends_on); @@ -147,26 +147,26 @@ void RenderingDeviceVulkan::_add_dependency(RID p_id, RID p_depends_on) { void RenderingDeviceVulkan::_free_dependencies(RID p_id) { //direct dependencies must be freed - Map>::Element *E = dependency_map.find(p_id); + HashMap>::Iterator E = dependency_map.find(p_id); if (E) { - while (E->get().size()) { - free(E->get().front()->get()); + while (E->value.size()) { + free(E->value.front()->get()); } - dependency_map.erase(E); + dependency_map.remove(E); } //reverse dependencies must be unreferenced E = reverse_dependency_map.find(p_id); if (E) { - for (Set::Element *F = E->get().front(); F; F = F->next()) { - Map>::Element *G = dependency_map.find(F->get()); + for (RBSet::Element *F = E->value.front(); F; F = F->next()) { + HashMap>::Iterator G = dependency_map.find(F->get()); ERR_CONTINUE(!G); - ERR_CONTINUE(!G->get().has(p_id)); - G->get().erase(p_id); + ERR_CONTINUE(!G->value.has(p_id)); + G->value.erase(p_id); } - reverse_dependency_map.erase(E); + reverse_dependency_map.remove(E); } } @@ -3856,7 +3856,7 @@ RenderingDevice::FramebufferFormatID RenderingDeviceVulkan::framebuffer_format_c key.passes = p_passes; key.view_count = p_view_count; - const Map::Element *E = framebuffer_format_cache.find(key); + const RBMap::Element *E = framebuffer_format_cache.find(key); if (E) { //exists, return return E->get(); @@ -3884,7 +3884,7 @@ RenderingDevice::FramebufferFormatID RenderingDeviceVulkan::framebuffer_format_c FramebufferFormatKey key; key.passes.push_back(FramebufferPass()); - const Map::Element *E = framebuffer_format_cache.find(key); + const RBMap::Element *E = framebuffer_format_cache.find(key); if (E) { //exists, return return E->get(); @@ -3935,11 +3935,11 @@ RenderingDevice::FramebufferFormatID RenderingDeviceVulkan::framebuffer_format_c } RenderingDevice::TextureSamples RenderingDeviceVulkan::framebuffer_format_get_texture_samples(FramebufferFormatID p_format, uint32_t p_pass) { - Map::Element *E = framebuffer_formats.find(p_format); + HashMap::Iterator E = framebuffer_formats.find(p_format); ERR_FAIL_COND_V(!E, TEXTURE_SAMPLES_1); - ERR_FAIL_COND_V(p_pass >= uint32_t(E->get().pass_samples.size()), TEXTURE_SAMPLES_1); + ERR_FAIL_COND_V(p_pass >= uint32_t(E->value.pass_samples.size()), TEXTURE_SAMPLES_1); - return E->get().pass_samples[p_pass]; + return E->value.pass_samples[p_pass]; } /***********************/ @@ -4138,7 +4138,7 @@ RenderingDevice::VertexFormatID RenderingDeviceVulkan::vertex_format_create(cons vdcache.bindings = memnew_arr(VkVertexInputBindingDescription, p_vertex_formats.size()); vdcache.attributes = memnew_arr(VkVertexInputAttributeDescription, p_vertex_formats.size()); - Set used_locations; + RBSet used_locations; for (int i = 0; i < p_vertex_formats.size(); i++) { ERR_CONTINUE(p_vertex_formats[i].format >= DATA_FORMAT_MAX); ERR_FAIL_COND_V(used_locations.has(p_vertex_formats[i].location), INVALID_ID); @@ -5292,7 +5292,7 @@ RID RenderingDeviceVulkan::shader_create_from_bytecode(const Vector &p_ //has data, needs an actual format; UniformSetFormat usformat; usformat.uniform_info = set.uniform_info; - Map::Element *E = uniform_set_format_cache.find(usformat); + RBMap::Element *E = uniform_set_format_cache.find(usformat); if (E) { format = E->get(); } else { @@ -5468,12 +5468,12 @@ RID RenderingDeviceVulkan::texture_buffer_create(uint32_t p_size_elements, DataF RenderingDeviceVulkan::DescriptorPool *RenderingDeviceVulkan::_descriptor_pool_allocate(const DescriptorPoolKey &p_key) { if (!descriptor_pools.has(p_key)) { - descriptor_pools[p_key] = Set(); + descriptor_pools[p_key] = RBSet(); } DescriptorPool *pool = nullptr; - for (Set::Element *E = descriptor_pools[p_key].front(); E; E = E->next()) { + for (RBSet::Element *E = descriptor_pools[p_key].front(); E; E = E->next()) { if (E->get()->usage < max_descriptors_per_pool) { pool = E->get(); break; @@ -8349,7 +8349,7 @@ void RenderingDeviceVulkan::compute_list_end(uint32_t p_post_barrier) { uint32_t barrier_idx = 0; - for (Set::Element *E = compute_list->state.textures_to_sampled_layout.front(); E; E = E->next()) { + for (RBSet::Element *E = compute_list->state.textures_to_sampled_layout.front(); E; E = E->next()) { VkImageMemoryBarrier &image_memory_barrier = image_barriers[barrier_idx++]; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; image_memory_barrier.pNext = nullptr; @@ -9477,21 +9477,21 @@ void RenderingDeviceVulkan::finalize() { vmaDestroyBuffer(allocator, staging_buffer_blocks[i].buffer, staging_buffer_blocks[i].allocation); } while (small_allocs_pools.size()) { - Map::Element *E = small_allocs_pools.front(); - vmaDestroyPool(allocator, E->get()); - small_allocs_pools.erase(E); + HashMap::Iterator E = small_allocs_pools.begin(); + vmaDestroyPool(allocator, E->value); + small_allocs_pools.remove(E); } vmaDestroyAllocator(allocator); while (vertex_formats.size()) { - Map::Element *temp = vertex_formats.front(); - memdelete_arr(temp->get().bindings); - memdelete_arr(temp->get().attributes); - vertex_formats.erase(temp); + HashMap::Iterator temp = vertex_formats.begin(); + memdelete_arr(temp->value.bindings); + memdelete_arr(temp->value.attributes); + vertex_formats.remove(temp); } - for (int i = 0; i < framebuffer_formats.size(); i++) { - vkDestroyRenderPass(device, framebuffer_formats[i].render_pass, nullptr); + for (KeyValue &E : framebuffer_formats) { + vkDestroyRenderPass(device, E.value.render_pass, nullptr); } framebuffer_formats.clear(); diff --git a/drivers/vulkan/rendering_device_vulkan.h b/drivers/vulkan/rendering_device_vulkan.h index 236742c15d..601c44c728 100644 --- a/drivers/vulkan/rendering_device_vulkan.h +++ b/drivers/vulkan/rendering_device_vulkan.h @@ -101,8 +101,8 @@ class RenderingDeviceVulkan : public RenderingDevice { VkDevice device = VK_NULL_HANDLE; - Map> dependency_map; //IDs to IDs that depend on it - Map> reverse_dependency_map; //same as above, but in reverse + HashMap> dependency_map; //IDs to IDs that depend on it + HashMap> reverse_dependency_map; //same as above, but in reverse void _add_dependency(RID p_id, RID p_depends_on); void _free_dependencies(RID p_id); @@ -349,15 +349,15 @@ class RenderingDeviceVulkan : public RenderingDevice { VkRenderPass _render_pass_create(const Vector &p_attachments, const Vector &p_passes, InitialAction p_initial_action, FinalAction p_final_action, InitialAction p_initial_depth_action, FinalAction p_final_depth_action, uint32_t p_view_count = 1, Vector *r_samples = nullptr); // This is a cache and it's never freed, it ensures // IDs for a given format are always unique. - Map framebuffer_format_cache; + RBMap framebuffer_format_cache; struct FramebufferFormat { - const Map::Element *E; + const RBMap::Element *E; VkRenderPass render_pass = VK_NULL_HANDLE; //here for constructing shaders, never used, see section (7.2. Render Pass Compatibility from Vulkan spec) Vector pass_samples; uint32_t view_count = 1; // number of views }; - Map framebuffer_formats; + HashMap framebuffer_formats; struct Framebuffer { FramebufferFormatID format_id = 0; @@ -398,7 +398,7 @@ class RenderingDeviceVulkan : public RenderingDevice { uint32_t subpass_count = 1; }; - Map framebuffers; + RBMap framebuffers; Size2 size; uint32_t view_count; }; @@ -488,7 +488,7 @@ class RenderingDeviceVulkan : public RenderingDevice { VkPipelineVertexInputStateCreateInfo create_info; }; - Map vertex_formats; + HashMap vertex_formats; struct VertexArray { RID buffer; @@ -592,7 +592,7 @@ class RenderingDeviceVulkan : public RenderingDevice { // Always grows, never shrinks, ensuring unique IDs, but we assume // the amount of formats will never be a problem, as the amount of shaders // in a game is limited. - Map uniform_set_format_cache; + RBMap uniform_set_format_cache; // Shaders in Vulkan are just pretty much // precompiled blocks of SPIR-V bytecode. They @@ -702,7 +702,7 @@ class RenderingDeviceVulkan : public RenderingDevice { uint32_t usage; }; - Map> descriptor_pools; + RBMap> descriptor_pools; uint32_t max_descriptors_per_pool = 0; DescriptorPool *_descriptor_pool_allocate(const DescriptorPoolKey &p_key); @@ -923,7 +923,7 @@ class RenderingDeviceVulkan : public RenderingDevice { }; struct State { - Set textures_to_sampled_layout; + RBSet textures_to_sampled_layout; SetState sets[MAX_UNIFORM_SETS]; uint32_t set_count = 0; RID pipeline; @@ -1016,7 +1016,7 @@ class RenderingDeviceVulkan : public RenderingDevice { void _free_pending_resources(int p_frame); VmaAllocator allocator = nullptr; - Map small_allocs_pools; + HashMap small_allocs_pools; VmaPool _find_or_create_small_allocs_pool(uint32_t p_mem_type_index); VulkanContext *context = nullptr; diff --git a/drivers/vulkan/vulkan_context.h b/drivers/vulkan/vulkan_context.h index 6858eb7524..236e3bf35f 100644 --- a/drivers/vulkan/vulkan_context.h +++ b/drivers/vulkan/vulkan_context.h @@ -34,7 +34,7 @@ #include "core/error/error_list.h" #include "core/os/mutex.h" #include "core/string/ustring.h" -#include "core/templates/map.h" +#include "core/templates/rb_map.h" #include "core/templates/rid_owner.h" #include "servers/display_server.h" #include "servers/rendering/rendering_device.h" @@ -161,7 +161,7 @@ private: RID_Owner local_device_owner; - Map windows; + HashMap windows; uint32_t swapchainImageCount = 0; // Commands. -- cgit v1.2.3