# UNDF: UNDF-2026-000000983 --- a/source/glest_game/world/unit_updater.h +++ b/source/glest_game/world/unit_updater.h @@ -1,6 +1,7 @@ #ifndef _GLEST_GAME_UNITUPDATER_H_ #define _GLEST_GAME_UNITUPDATER_H_ +#include #include ... @@ -127,7 +128,7 @@ vector findUnitsInRange(const Unit *unit, int radius); private: - void findUnitsForCell(Cell *cell, vector &units); + void findUnitsForCell(Cell *cell, vector &units, std::unordered_set &seenIds); --- a/source/glest_game/world/unit_updater.cpp +++ b/source/glest_game/world/unit_updater.cpp @@ -3461,13 +3461,10 @@ -void UnitUpdater::findUnitsForCell(Cell *cell, vector &units) { +void UnitUpdater::findUnitsForCell(Cell *cell, vector &units, std::unordered_set &seenIds) { // all fields if (cell != NULL) { for (int k = 0; k < fieldCount; k++) { Field f = static_cast(k); // check field Unit *cellUnit = cell->getUnit(f); if (cellUnit != NULL && cellUnit->isAlive()) { - // check if unit already is in list - bool found = false; - for (unsigned int i = 0; i < units.size(); ++i) { - Unit *unitInList = units[i]; - if (unitInList->getId() == cellUnit->getId()) { - found = true; - break; - } - } - if (found == false) { + // O(1) dedup via hash set instead of O(U) linear scan + if (seenIds.insert(cellUnit->getId()).second) { units.push_back(cellUnit); } } @@ -3492,6 +3489,8 @@ vector UnitUpdater::findUnitsInRange(const Unit *unit, int radius) { int range = radius; vector units; + std::unordered_set seenIds; // aux vars int size = unit->getType()->getSize(); @@ -3507,7 +3506,7 @@ #endif Cell *cell = map->getCell(i, j); - findUnitsForCell(cell, units); + findUnitsForCell(cell, units, seenIds); } } }