1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-18 01:42:38 +01:00

Codefix: prevent inefficient reserve behaviour

When reserving by size() + 2, in a loop you're not really helping.
This commit is contained in:
Rubidium
2025-12-13 22:53:46 +01:00
committed by rubidium42
parent a3393af26c
commit d3420e6e4c

View File

@@ -1757,12 +1757,14 @@ struct IndustryProductionGraphWindow : BaseCargoGraphWindow {
const Industry *i = Industry::Get(this->window_number);
this->data.clear();
this->data.reserve(
2 * std::ranges::count_if(i->produced, &IsValidCargoType, &Industry::ProducedCargo::cargo) +
2 * std::ranges::count_if(i->accepted, &IsValidCargoType, &Industry::AcceptedCargo::cargo));
for (const auto &p : i->produced) {
if (!IsValidCargoType(p.cargo)) continue;
const CargoSpec *cs = CargoSpec::Get(p.cargo);
this->data.reserve(this->data.size() + 2);
DataSet &produced = this->data.emplace_back();
produced.colour = cs->legend_colour;
produced.exclude_bit = cs->Index();
@@ -1783,8 +1785,6 @@ struct IndustryProductionGraphWindow : BaseCargoGraphWindow {
if (!IsValidCargoType(a.cargo)) continue;
const CargoSpec *cs = CargoSpec::Get(a.cargo);
this->data.reserve(this->data.size() + 2);
DataSet &accepted = this->data.emplace_back();
accepted.colour = cs->legend_colour;
accepted.exclude_bit = cs->Index();