From 81c2c4a88103ecb4ad9f0ef41f569a1cf1d569e9 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Tue, 16 Jan 2024 23:15:56 +0000 Subject: [PATCH] Slightly speed up unordered iteration of FlowStatMap --- src/station_base.h | 6 ++++++ src/station_cmd.cpp | 4 ++-- src/table/newgrf_debug_data.h | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/station_base.h b/src/station_base.h index 686d15bb16..4fd9df7444 100644 --- a/src/station_base.h +++ b/src/station_base.h @@ -22,6 +22,7 @@ #include "bitmap_type.h" #include "core/alloc_type.hpp" #include "core/endian_type.hpp" +#include "core/span_type.hpp" #include "strings_type.h" #include #include @@ -504,6 +505,11 @@ public: } void SortStorage(); + + span IterateUnordered() const + { + return span(this->flows_storage.data(), this->flows_storage.size()); + } }; struct GoodsEntryData : ZeroedMemoryAllocator { diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 6141807952..99f74c593f 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -5629,7 +5629,7 @@ void FlowStatMap::RestrictFlows(StationID via) uint FlowStatMap::GetFlow() const { uint ret = 0; - for (const FlowStat &it : *this) { + for (const FlowStat &it : this->IterateUnordered()) { if (it.IsInvalid()) continue; ret += (it.end() - 1)->first; } @@ -5644,7 +5644,7 @@ uint FlowStatMap::GetFlow() const uint FlowStatMap::GetFlowVia(StationID via) const { uint ret = 0; - for (const FlowStat &it : *this) { + for (const FlowStat &it : this->IterateUnordered()) { if (it.IsInvalid()) continue; ret += it.GetShare(via); } diff --git a/src/table/newgrf_debug_data.h b/src/table/newgrf_debug_data.h index 845ee0e811..008d52fbf7 100644 --- a/src/table/newgrf_debug_data.h +++ b/src/table/newgrf_debug_data.h @@ -2109,7 +2109,7 @@ class NIHStationStruct : public NIHelper { } if (ge->data != nullptr && ge->data->flows.size() > 0) { size_t total_shares = 0; - for (const FlowStat &fs : ge->data->flows) { + for (const FlowStat &fs : ge->data->flows.IterateUnordered()) { total_shares += fs.size(); } seprintf(buffer, lastof(buffer), " Flows: %u, total shares: %u", (uint)ge->data->flows.size(), (uint)total_shares);