Codechange: Build station and depot vehicle lists from shared order lists. (#11676)

The brings some performance advantages:

* No need to iterate all vehicles and check for primary vehicle as only vehicles that can have orders are listed.
* Shared orders only need to be tested once instead of for each vehicle sharing them.
* Vehicle tests only need to be performed on the first shared vehicle instead of all.
This commit is contained in:
Peter Nelson
2024-01-05 18:59:38 +00:00
committed by GitHub
parent 7788b68bbe
commit 34e8c8e1c1
4 changed files with 71 additions and 41 deletions

View File

@@ -14,6 +14,7 @@
#include "script_station.hpp"
#include "../../depot_map.h"
#include "../../vehicle_base.h"
#include "../../vehiclelist_func.h"
#include "../../train.h"
#include "../../core/backup_type.hpp"
#include <../squirrel/sqvm.h>
@@ -111,16 +112,12 @@ ScriptVehicleList_Station::ScriptVehicleList_Station(StationID station_id)
bool is_deity = ScriptCompanyMode::IsDeity();
CompanyID owner = ScriptObject::GetCompany();
for (const Vehicle *v : Vehicle::Iterate()) {
if ((v->owner == owner || is_deity) && v->IsPrimaryVehicle()) {
for (const Order *order : v->Orders()) {
if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT)) && order->GetDestination() == station_id) {
this->AddItem(v->index);
break;
}
}
}
}
FindVehiclesWithOrder(
[is_deity, owner](const Vehicle *v) { return is_deity || v->owner == owner; },
[station_id](const Order *order) { return (order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT)) && order->GetDestination() == station_id; },
[this](const Vehicle *v) { this->AddItem(v->index); }
);
}
ScriptVehicleList_Depot::ScriptVehicleList_Depot(TileIndex tile)
@@ -162,16 +159,12 @@ ScriptVehicleList_Depot::ScriptVehicleList_Depot(TileIndex tile)
bool is_deity = ScriptCompanyMode::IsDeity();
CompanyID owner = ScriptObject::GetCompany();
for (const Vehicle *v : Vehicle::Iterate()) {
if ((v->owner == owner || is_deity) && v->IsPrimaryVehicle() && v->type == type) {
for (const Order *order : v->Orders()) {
if (order->IsType(OT_GOTO_DEPOT) && order->GetDestination() == dest) {
this->AddItem(v->index);
break;
}
}
}
}
FindVehiclesWithOrder(
[is_deity, owner, type](const Vehicle *v) { return (is_deity || v->owner == owner) && v->type == type; },
[dest](const Order *order) { return order->IsType(OT_GOTO_DEPOT) && order->GetDestination() == dest; },
[this](const Vehicle *v) { this->AddItem(v->index); }
);
}
ScriptVehicleList_SharedOrders::ScriptVehicleList_SharedOrders(VehicleID vehicle_id)