From e625fa4a2420fd4876826bf811ecbfb94d5adb8e Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 2 Dec 2026 07:53:39 +0100 Subject: [PATCH] perf(physics): the vehicles whose hoses exchange air taken in the update loop, not in a loop after it update_brake_pipes() went through all the vehicles once more after the update loop to find the ones whose step left the air of their hoses to exchange, looking at two flags of each, and the memory of each vehicle was gone from the cache by then: 1.3 % of the main thread on Tomaszewo. The final update loop now takes each vehicle right after its Update(), while its memory is at hand (collect_brake_pipes()), and the exchange goes on as before (exchange_brake_pipes()). The flags are set only by the step of the vehicle itself and cleared only by the exchange, so the vehicles taken and their order are the same. The other callers still look at all the vehicles. --- vehicle/DynObj.h | 6 ++++++ vehicle/dynamic/vehicletable.cpp | 28 ++++++++++++++++++++++------ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/vehicle/DynObj.h b/vehicle/DynObj.h index 08f8f7e3e..f22ca687c 100644 --- a/vehicle/DynObj.h +++ b/vehicle/DynObj.h @@ -1157,6 +1157,12 @@ private: // the air exchange through the brake and main reservoir hoses of the vehicles which stepped their physics, after all of them did void update_brake_pipes( double const Deltatime ); + // the vehicle goes into m_brakepipes if its step of the physics left the air of its hoses to exchange + void + collect_brake_pipes( TDynamicObject const *Vehicle ); + // the exchange of update_brake_pipes() for the vehicles in m_brakepipes + void + exchange_brake_pipes( double const Deltatime ); // physics.derailment new: the bodies of the vehicles off the track and those near them run into each other instead // of into one another: the impulses of the hits, the bodies moved apart, a vehicle on the rails pushed off them; // what the bodies overlap by goes to the log every second diff --git a/vehicle/dynamic/vehicletable.cpp b/vehicle/dynamic/vehicletable.cpp index ff9af245e..797b3ef3f 100644 --- a/vehicle/dynamic/vehicletable.cpp +++ b/vehicle/dynamic/vehicletable.cpp @@ -400,14 +400,18 @@ vehicle_table::update( double Deltatime, int Iterationcount ) { { std::size_t index { 0 }; auto mark { counted ? std::chrono::steady_clock::now() : std::chrono::steady_clock::time_point {} }; + // the vehicles whose hoses exchange their air after the loop are taken right after their update, while their memory + // is at hand: only the step of a vehicle itself leaves its hoses to exchange, and nothing else in the loop clears them + m_brakepipes.clear(); for( auto *vehicle : m_items ) { auto const coarsestep { ( index < coarse.size() ) && ( coarse[ index ] != 0 ) }; // Ra 2015-01: tylko tu przelicza sieć trakcyjną vehicle->Update( coarsestep ? totaltime : Deltatime, totaltime ); + collect_brake_pipes( vehicle ); lap( index++, Timer::subsystem.vehicles_update_kind, mark ); } } - update_brake_pipes( Deltatime ); + exchange_brake_pipes( Deltatime ); Timer::subsystem.vehicles_update.stop(); // jeśli jest coś do usunięcia z listy, to trzeba na końcu @@ -636,12 +640,24 @@ void vehicle_table::update_brake_pipes( double const Deltatime ) { m_brakepipes.clear(); - for( auto *vehicle : m_items ) { - if( ( true == vehicle->physics->brakePipe.CouplersPending() ) - || ( true == vehicle->physics->mainPipe.CouplersPending() ) ) { - m_brakepipes.push_back( vehicle->physics ); - } + for( auto const *vehicle : m_items ) { + collect_brake_pipes( vehicle ); } + exchange_brake_pipes( Deltatime ); +} + +void +vehicle_table::collect_brake_pipes( TDynamicObject const *Vehicle ) { + + if( ( true == Vehicle->physics->brakePipe.CouplersPending() ) + || ( true == Vehicle->physics->mainPipe.CouplersPending() ) ) { + m_brakepipes.push_back( Vehicle->physics ); + } +} + +void +vehicle_table::exchange_brake_pipes( double const Deltatime ) { + // the clusters of the trains at once, on the threads of worker_pool: a thread for each MEMBERS_PER_THREAD vehicles // exchanged, up to all the cores. with fewer, waking the threads takes longer than the work, so they go one after another if( false == static_cast( m_hosetrains.run ) ) {