From 2d48829999db0d647f9c0f47de7867b535d2d514 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 5 Mar 2024 18:33:58 +0000 Subject: [PATCH] Codechange: Scan station catchment tiles when removing station from nearby towns/industries. (#12129) Avoid iterating all towns and industries when updating station catchment, and scan a limited portion of the map instead. This provides a modest performance benefit when many towns/industries exist. --- src/station.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/station.cpp b/src/station.cpp index be1ec24d80..f1afaec134 100644 --- a/src/station.cpp +++ b/src/station.cpp @@ -424,12 +424,24 @@ void Station::RemoveIndustryToDeliver(Industry *ind) /** - * Remove this station from the nearby stations lists of all towns and industries. + * Remove this station from the nearby stations lists of nearby towns and industries. */ void Station::RemoveFromAllNearbyLists() { - for (Town *t : Town::Iterate()) { t->stations_near.erase(this); } - for (Industry *i : Industry::Iterate()) { i->stations_near.erase(this); } + std::set towns; + std::set industries; + + for (const auto &tile : this->catchment_tiles) { + TileType type = GetTileType(tile); + if (type == MP_HOUSE) { + towns.insert(GetTownIndex(tile)); + } else if (type == MP_INDUSTRY) { + industries.insert(GetIndustryIndex(tile)); + } + } + + for (const TownID &townid : towns) { Town::Get(townid)->stations_near.erase(this); } + for (const IndustryID &industryid : industries) { Industry::Get(industryid)->stations_near.erase(this); } } /**