18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 06:35:21 +01:00

perf(blackbox): each vehicle looked up once a look

A look around went over the vehicles three times (states, couplers, derailments), and each time looked
every vehicle up in the map of known vehicles, up to three times in the derailment pass. On Tomaszewo,
with 1142 vehicles, that is several thousand hash lookups a second, mostly cache misses. The vehicles
pass now keeps the vehicles it saw with their entries, and the two passes after it go over that list.
The vehicles come in the same order and the same records are written; a vehicle is still written the
first time it is seen.
This commit is contained in:
jerrrrycho
2026-12-02 07:04:30 +01:00
parent b58ab43bea
commit fdbf54ba15
2 changed files with 25 additions and 19 deletions
+21 -19
View File
@@ -624,17 +624,15 @@ void blackbox_recorder::write_derailed(TDynamicObject const *Vehicle, derailment
}
void blackbox_recorder::look_at_derailments() {
for (auto *vehicle : simulation::Vehicles.sequence()) {
if (false == vehicle->bEnabled)
continue;
for (auto const &[vehicle, looked] : m_looked) {
auto const &motion{vehicle->physics->OffTrack};
auto const id{vehicle_id(vehicle)};
auto &entry{*looked};
auto const id{entry.id};
// the derailment itself first, if it came before the recording did; forgotten once back on the rails
if (auto const *notice{vehicle->derailment_notice()})
write_derailed(vehicle, *notice);
else
m_vehicles[vehicle].derailed = false;
auto &entry{m_vehicles[vehicle]};
entry.derailed = false;
if (false == motion.active()) {
entry.derailment = 0;
continue;
@@ -667,10 +665,8 @@ void blackbox_recorder::look_at_derailments() {
}
void blackbox_recorder::look_at_couplers() {
for (auto *vehicle : simulation::Vehicles.sequence()) {
if (false == vehicle->bEnabled)
continue;
auto &entry{m_vehicles[vehicle]};
for (auto const &[vehicle, looked] : m_looked) {
auto &entry{*looked};
auto const *physics{vehicle->physics};
for (int side = 0; side < 2; ++side) {
auto const flags{physics->Couplers[side].CouplingFlag};
@@ -735,12 +731,14 @@ void blackbox_recorder::write_camera() {
void blackbox_recorder::look_at_vehicles(bool const Keyframe) {
std::vector<blackbox::vehicle_state> states;
// the vehicles on the scenery, kept with their entries for the rest of the look
m_looked.clear();
for (auto *vehicle : simulation::Vehicles.sequence()) {
if (false == vehicle->bEnabled)
continue;
auto const id{vehicle_id(vehicle)};
auto &entry{m_vehicles[vehicle]};
auto const state{state_of(*vehicle, id)};
auto &entry{entry_of(vehicle)};
m_looked.emplace_back(vehicle, &entry);
auto const state{state_of(*vehicle, entry.id)};
auto const changed{false == entry.listed || glm::distance(state.position, entry.last.position) > listmove || std::abs(state.speed_kmh - entry.last.speed_kmh) > listspeed ||
state.driving != entry.last.driving};
if (Keyframe || changed) {
@@ -812,18 +810,22 @@ void blackbox_recorder::look_at_timetable(TDynamicObject const &Vehicle, std::ui
m_writer->timetable(data);
}
std::uint32_t blackbox_recorder::vehicle_id(TDynamicObject const *Vehicle) {
if (auto const lookup{m_vehicles.find(Vehicle)}; lookup != m_vehicles.end())
return lookup->second.id;
auto &entry{m_vehicles[Vehicle]};
blackbox_recorder::vehicle_entry &blackbox_recorder::entry_of(TDynamicObject const *Vehicle) {
auto const [lookup, fresh]{m_vehicles.try_emplace(Vehicle)};
auto &entry{lookup->second};
if (false == fresh)
return entry;
entry.id = m_nextvehicle++;
auto const *physics{Vehicle->physics};
auto const *driver{Vehicle->Mechanik};
auto const train{driver != nullptr && driver->TrainName() != "none" ? driver->TrainName() : std::string{}};
m_writer->vehicle({entry.id, text_of(Vehicle->name()), text_of(physics->TypeName), text_of(train), static_cast<float>(physics->Dim.L), static_cast<float>(physics->Dim.W),
static_cast<std::uint8_t>(physics->CategoryFlag)});
return entry.id;
return entry;
}
std::uint32_t blackbox_recorder::vehicle_id(TDynamicObject const *Vehicle) {
return entry_of(Vehicle).id;
}
std::uint32_t blackbox_recorder::known_id(TDynamicObject const *Vehicle) {
+4
View File
@@ -115,6 +115,8 @@ private:
void write_camera();
// the timetable of the train the user is in, written again when it is another one; none isn't written
void look_at_timetable(TDynamicObject const &Vehicle, std::uint32_t Id);
// what is known of the vehicle, its number given (and the vehicle written) on first sight
vehicle_entry &entry_of(TDynamicObject const *Vehicle);
// number of the vehicle, given (and the vehicle written) on first sight
std::uint32_t vehicle_id(TDynamicObject const *Vehicle);
// the number of a vehicle still on the scenery, no_vehicle for none or one taken off it
@@ -135,6 +137,8 @@ private:
double m_lookdue{0.0};
double m_keyframedue{0.0};
std::unordered_map<TDynamicObject const *, vehicle_entry> m_vehicles;
// the vehicles of the current look, in their order, with their entries: each looked up once in a look
std::vector<std::pair<TDynamicObject *, vehicle_entry *>> m_looked;
std::uint32_t m_nextvehicle{1};
std::vector<switch_entry> m_switches;
std::vector<signal_entry> m_signals;