From dedb0ff7e2cb3b1987d80f859e4489db7924d135 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 6 Mar 2023 22:44:17 +0000 Subject: [PATCH] Avoid unordered map/multimap for tunnel tile index and axis height maps Use robin_hood and btree_multimap instead --- src/tunnel_map.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/tunnel_map.cpp b/src/tunnel_map.cpp index e217b60c00..0155290278 100644 --- a/src/tunnel_map.cpp +++ b/src/tunnel_map.cpp @@ -11,7 +11,8 @@ #include "tunnelbridge_map.h" #include "core/pool_func.hpp" -#include +#include "3rdparty/robin_hood/robin_hood.h" +#include "3rdparty/cpp-btree/btree_map.h" #include "safeguards.h" @@ -19,8 +20,8 @@ TunnelPool _tunnel_pool("Tunnel"); INSTANTIATE_POOL_METHODS(Tunnel) -static std::unordered_map tunnel_tile_index_map; -static std::unordered_multimap tunnel_axis_height_index; +static robin_hood::unordered_map tunnel_tile_index_map; +static btree::btree_multimap tunnel_axis_height_index; static uint64 GetTunnelAxisHeightCacheKey(TileIndex tile, uint8 height, bool y_axis) { if (y_axis) { @@ -48,9 +49,9 @@ Tunnel::~Tunnel() tunnel_tile_index_map.erase(this->tile_s); } - auto range = tunnel_axis_height_index.equal_range(GetTunnelAxisHeightCacheKey(this)); bool have_erased = false; - for (auto it = range.first; it != range.second; ++it) { + const auto key = GetTunnelAxisHeightCacheKey(this); + for (auto it = tunnel_axis_height_index.lower_bound(key); it != tunnel_axis_height_index.end() && it->first == key; ++it) { if (it->second == this) { tunnel_axis_height_index.erase(it); have_erased = true; @@ -70,7 +71,7 @@ void Tunnel::UpdateIndexes() tunnel_tile_index_map[this->tile_s] = this->index; } - tunnel_axis_height_index.emplace(GetTunnelAxisHeightCacheKey(this), this); + tunnel_axis_height_index.insert({ GetTunnelAxisHeightCacheKey(this), this }); } /** @@ -103,8 +104,8 @@ TileIndex GetOtherTunnelEnd(TileIndex tile) static inline bool IsTunnelInWaySingleAxis(TileIndex tile, int z, IsTunnelInWayFlags flags, bool y_axis, TileIndexDiff tile_diff) { - const auto tunnels = tunnel_axis_height_index.equal_range(GetTunnelAxisHeightCacheKey(tile, z, y_axis)); - for (auto it = tunnels.first; it != tunnels.second; ++it) { + const auto key = GetTunnelAxisHeightCacheKey(tile, z, y_axis); + for (auto it = tunnel_axis_height_index.lower_bound(key); it != tunnel_axis_height_index.end() && it->first == key; ++it) { const Tunnel *t = it->second; if (t->tile_n > tile || tile > t->tile_s) continue;