Move StationCargoList and FlowStatMap out of GoodsEntry struct

Move them into a new GoodsEntryData struct referenced
using a std::unique_ptr from GoodsEntry.
The unique_ptr may be nullptr if the cargo list and flow stat map
are both empty (this is the case for unused cargoes).

This reduces GoodsEntry from 128 to 24 bytes,
and Station from 8680 to 2024 bytes,
(on Linux x86_64).
This commit is contained in:
Jonathan G Rennison
2023-08-19 21:21:31 +01:00
parent cd2ab6430b
commit 19835b51ee
24 changed files with 283 additions and 137 deletions

View File

@@ -172,7 +172,7 @@ void LinkGraphOverlay::RebuildCache(bool incremental)
item->to_pt = to_pt;
}
this->AddStats(c, lg.Monthly(edge.Capacity()), lg.Monthly(edge.Usage()),
ge.flows.GetFlowVia(to->index),
ge.data != nullptr ? ge.data->flows.GetFlowVia(to->index) : 0,
edge.TravelTime(),
from->owner == OWNER_NONE || to->owner == OWNER_NONE,
item->prop);

View File

@@ -121,6 +121,7 @@ void LinkGraphJob::FinaliseJob()
LinkGraph *lg = LinkGraph::Get(ge.link_graph);
FlowStatMap &flows = from.Flows();
FlowStatMap &geflows = ge.CreateData().flows;
for (Edge &edge : from.GetEdges()) {
if (edge.Flow() == 0) continue;
@@ -135,7 +136,7 @@ void LinkGraphJob::FinaliseJob()
/* Delete old flows for source stations which have been deleted
* from the new flows. This avoids flow cycles between old and
* new flows. */
while (!erased.IsEmpty()) ge.flows.erase(erased.Pop());
while (!erased.IsEmpty()) geflows.erase(erased.Pop());
} else if (lg_edge.LastUnrestrictedUpdate() == INVALID_DATE) {
/* Edge is fully restricted. */
flows.RestrictFlows(to);
@@ -146,7 +147,7 @@ void LinkGraphJob::FinaliseJob()
* really delete them as we could then end up with unroutable cargo
* somewhere. Do delete them and also reroute relevant cargo if
* automatic distribution has been turned off for that cargo. */
for (FlowStatMap::iterator it(ge.flows.begin()); it != ge.flows.end();) {
for (FlowStatMap::iterator it(geflows.begin()); it != geflows.end();) {
FlowStatMap::iterator new_it = flows.find(it->GetOrigin());
if (new_it == flows.end()) {
if (_settings_game.linkgraph.GetDistributionType(this->Cargo()) != DT_MANUAL) {
@@ -154,7 +155,7 @@ void LinkGraphJob::FinaliseJob()
NodeID origin = it->GetOrigin();
FlowStat shares(INVALID_STATION, INVALID_STATION, 1);
it->SwapShares(shares);
it = ge.flows.erase(it);
it = geflows.erase(it);
for (FlowStat::const_iterator shares_it(shares.begin());
shares_it != shares.end(); ++shares_it) {
RerouteCargoFromSource(st, this->Cargo(), origin, shares_it->second, st->index);
@@ -165,7 +166,7 @@ void LinkGraphJob::FinaliseJob()
} else {
FlowStat shares(INVALID_STATION, INVALID_STATION, 1);
it->SwapShares(shares);
it = ge.flows.erase(it);
it = geflows.erase(it);
for (FlowStat::const_iterator shares_it(shares.begin());
shares_it != shares.end(); ++shares_it) {
RerouteCargo(st, this->Cargo(), shares_it->second, st->index);
@@ -178,9 +179,9 @@ void LinkGraphJob::FinaliseJob()
}
}
for (FlowStatMap::iterator it(flows.begin()); it != flows.end(); ++it) {
ge.flows.insert(std::move(*it));
geflows.insert(std::move(*it));
}
ge.flows.SortStorage();
geflows.SortStorage();
InvalidateWindowData(WC_STATION_VIEW, st->index, this->Cargo());
}
}