Fix poor performance of recursive iteration of group hierarchies

This commit is contained in:
Jonathan G Rennison
2021-05-14 02:04:37 +01:00
parent 2e28bc8e6a
commit 088922b120

View File

@@ -265,6 +265,35 @@ const Livery *GetParentLivery(const Group *g)
return &pg->livery; return &pg->livery;
} }
static inline bool IsGroupDescendantOfGroup(const Group *g, const Group *top)
{
if (g->owner != top->owner) return false;
while (true) {
if (g->parent == INVALID_GROUP) return false;
if (g->parent == top->index) return true;
g = Group::Get(g->parent);
}
NOT_REACHED();
}
template <typename F>
void IterateDescendantsOfGroup(const Group *top, F func)
{
for (Group *cg : Group::Iterate()) {
if (IsGroupDescendantOfGroup(cg, top)) {
func(cg);
}
}
}
template <typename F>
void IterateDescendantsOfGroup(GroupID id_top, F func)
{
const Group *top = Group::GetIfValid(id_top);
if (top != nullptr) IterateDescendantsOfGroup<F>(top, func);
}
/** /**
* Propagate a livery change to a group's children. * Propagate a livery change to a group's children.
@@ -283,13 +312,10 @@ void PropagateChildLivery(const Group *g)
} }
} }
for (Group *cg : Group::Iterate()) { IterateDescendantsOfGroup(g, [&](Group *cg) {
if (cg->parent == g->index) { if (!HasBit(cg->livery.in_use, 0)) cg->livery.colour1 = g->livery.colour1;
if (!HasBit(cg->livery.in_use, 0)) cg->livery.colour1 = g->livery.colour1; if (!HasBit(cg->livery.in_use, 1)) cg->livery.colour2 = g->livery.colour2;
if (!HasBit(cg->livery.in_use, 1)) cg->livery.colour2 = g->livery.colour2; });
PropagateChildLivery(cg);
}
}
} }
@@ -743,9 +769,9 @@ static void SetGroupFlag(Group *g, GroupFlags flag, bool set, bool children)
if (!children) return; if (!children) return;
for (Group *pg : Group::Iterate()) { IterateDescendantsOfGroup(g, [&](Group *pg) {
if (pg->parent == g->index) SetGroupFlag(pg, flag, set, true); SetGroupFlag(pg, flag, set, false);
} });
} }
/** /**
@@ -860,9 +886,9 @@ uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
{ {
uint count = 0; uint count = 0;
const Engine *e = Engine::Get(id_e); const Engine *e = Engine::Get(id_e);
for (const Group *g : Group::Iterate()) { IterateDescendantsOfGroup(id_g, [&](Group *g) {
if (g->parent == id_g) count += GetGroupNumEngines(company, g->index, id_e); count += GroupStatistics::Get(company, g->index, e->type).num_engines[id_e];
} });
return count + GroupStatistics::Get(company, id_g, e->type).num_engines[id_e]; return count + GroupStatistics::Get(company, id_g, e->type).num_engines[id_e];
} }
@@ -877,9 +903,9 @@ uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
uint GetGroupNumVehicle(CompanyID company, GroupID id_g, VehicleType type) uint GetGroupNumVehicle(CompanyID company, GroupID id_g, VehicleType type)
{ {
uint count = 0; uint count = 0;
for (const Group *g : Group::Iterate()) { IterateDescendantsOfGroup(id_g, [&](Group *g) {
if (g->parent == id_g) count += GetGroupNumVehicle(company, g->index, type); count += GroupStatistics::Get(company, g->index, type).num_vehicle;
} });
return count + GroupStatistics::Get(company, id_g, type).num_vehicle; return count + GroupStatistics::Get(company, id_g, type).num_vehicle;
} }
@@ -894,9 +920,9 @@ uint GetGroupNumVehicle(CompanyID company, GroupID id_g, VehicleType type)
uint GetGroupNumProfitVehicle(CompanyID company, GroupID id_g, VehicleType type) uint GetGroupNumProfitVehicle(CompanyID company, GroupID id_g, VehicleType type)
{ {
uint count = 0; uint count = 0;
for (const Group *g : Group::Iterate()) { IterateDescendantsOfGroup(id_g, [&](Group *g) {
if (g->parent == id_g) count += GetGroupNumProfitVehicle(company, g->index, type); count += GroupStatistics::Get(company, g->index, type).num_profit_vehicle;
} });
return count + GroupStatistics::Get(company, id_g, type).num_profit_vehicle; return count + GroupStatistics::Get(company, id_g, type).num_profit_vehicle;
} }
@@ -911,9 +937,9 @@ uint GetGroupNumProfitVehicle(CompanyID company, GroupID id_g, VehicleType type)
Money GetGroupProfitLastYear(CompanyID company, GroupID id_g, VehicleType type) Money GetGroupProfitLastYear(CompanyID company, GroupID id_g, VehicleType type)
{ {
Money sum = 0; Money sum = 0;
for (const Group *g : Group::Iterate()) { IterateDescendantsOfGroup(id_g, [&](Group *g) {
if (g->parent == id_g) sum += GetGroupProfitLastYear(company, g->index, type); sum += GroupStatistics::Get(company, g->index, type).profit_last_year;
} });
return sum + GroupStatistics::Get(company, id_g, type).profit_last_year; return sum + GroupStatistics::Get(company, id_g, type).profit_last_year;
} }