18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-02 14:16:48 +01:00

perf(physics): the loops over all the vehicles read their facts from an array filled on the worker threads

With a thousand vehicles the serial loops of vehicle_table::update() and
update_steps_parallel() which only look at each vehicle (the work of the
steps, the groups of the vehicles by their neighbours, the vehicles
exchanging their load) waited on the memory of every vehicle each step.
The place of each vehicle in the table, whether its physics is on, whether
it exchanges its load and the places of its neighbours (or that one of
them can't be seen) are now taken on the threads of worker_pool right
after the neighbours are found, into a short array the loops read. Nothing
changes them in between, so the loops decide as before.

Tomaszewo 600 s, seed 12345, LTO, interleaved runs (on top of a dropped
attempt that kept the positions of standing axles, worth about 1%):
110.3 / 109.2 s vs 113.2 / 113.1 s without this change (about -3%).
Physics logs (14 and 9 files) and the event logs byte-identical to the
runs before. Unit tests 1208/1208.
This commit is contained in:
jerrrrycho
2026-11-30 21:52:37 +01:00
parent 0de898ff33
commit 7f3990b63a
+57 -22
View File
@@ -27,11 +27,13 @@ http://mozilla.org/MPL/2.0/.
#include "vehicle/pulleemode.h"
#include "vehicle/Driver.h"
#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <format>
#include <random>
#include <iterator>
#include <limits>
#include <numbers>
#include <optional>
#include <string_view>
@@ -116,6 +118,19 @@ trains_closing_in( std::vector<TDynamicObject *> const &Train ) {
return trains;
}
// what the loops of vehicle_table::update() and update_steps_parallel() over all the vehicles go by, of each vehicle by
// its place in the table: taken on the threads of worker_pool along with the neighbours, so that those loops read a
// short array in place of the objects of a thousand vehicles. read where they were read before, with nothing changing
// them between
struct vehicle_facts {
static constexpr std::uint32_t none { std::numeric_limits<std::uint32_t>::max() };
static constexpr std::uint32_t unseen { none - 1 }; // coupled with one it doesn't see next to it, or one not in the table
std::array<std::uint32_t, 2> neighbours { none, none }; // the places of the vehicles at its ends
bool active { false }; // its physics on, or the user in it
bool loading { false }; // exchanging its load
};
std::vector<vehicle_facts> facts;
// whether any vehicle of the train is within the distance of any vehicle of the other one
bool
trains_within( std::vector<TDynamicObject *> const &Train, std::vector<TDynamicObject *> const &Other, double const Distance ) {
@@ -258,26 +273,57 @@ vehicle_table::update( double Deltatime, int Iterationcount ) {
// the constants of the physics of each vehicle and the vehicles next to it, on the threads of worker_pool: a vehicle
// changes only its own, and the others are looked at where they are, which nothing changes here (the wires above
// don't count). so the results are those of doing the vehicles one after another
// then the facts of each vehicle the loops below go by (vehicle_facts), once the places of all of them are known
{
auto constexpr VEHICLES_PER_PART { 16 };
auto const parts { ( m_items.size() + VEHICLES_PER_PART - 1 ) / VEHICLES_PER_PART };
auto const part = [ this ]( std::size_t const Part ) {
auto const end { std::min( m_items.size(), ( Part + 1 ) * VEHICLES_PER_PART ) };
auto const itemcount { m_items.size() };
auto const parts { ( itemcount + VEHICLES_PER_PART - 1 ) / VEHICLES_PER_PART };
facts.resize( itemcount );
auto const part = [ this, itemcount ]( std::size_t const Part ) {
auto const end { std::min( itemcount, ( Part + 1 ) * VEHICLES_PER_PART ) };
for( auto index { Part * VEHICLES_PER_PART }; index < end; ++index ) {
auto *vehicle { m_items[ index ] };
vehicle->m_tableindex = static_cast<std::uint32_t>( index );
if( false == vehicle->bEnabled ) { continue; }
vehicle->physics->ComputeConstans();
vehicle->update_neighbours();
}
};
auto const factpart = [ this, itemcount ]( std::size_t const Part ) {
auto const end { std::min( itemcount, ( Part + 1 ) * VEHICLES_PER_PART ) };
for( auto index { Part * VEHICLES_PER_PART }; index < end; ++index ) {
auto const *vehicle { m_items[ index ] };
auto const *physics { vehicle->physics };
auto &fact { facts[ index ] };
fact.active = physics->PhysicActivation() || vehicle->MechInside;
fact.loading = ( physics->LoadStatus() != 0 ) || ( vehicle->LoadExchangeTime() > 0.f );
for (auto const end : VehicleEnds) {
auto const *neighbour { physics->Neighbours[ end ].vehicle };
auto &place { fact.neighbours[ end == VehicleEnd::front ? 0 : 1 ] };
place = vehicle_facts::none;
if( ( physics->Couplers[ end ].Connected != nullptr )
&& ( ( neighbour == nullptr ) || ( neighbour->physics != physics->Couplers[ end ].Connected ) ) ) {
place = vehicle_facts::unseen;
continue;
}
if( neighbour == nullptr ) { continue; }
auto const other { neighbour->m_tableindex };
place = ( ( other >= itemcount ) || ( m_items[ other ] != neighbour ) ) ? vehicle_facts::unseen : other;
}
}
};
auto const partthreads { std::min( worker_pool::capacity(), parts ) };
if( partthreads > 1 ) {
Workers().run( parts, partthreads, part );
Workers().run( parts, partthreads, factpart );
}
else {
for( std::size_t index = 0; index < parts; ++index ) {
part( index );
}
for( std::size_t index = 0; index < parts; ++index ) {
factpart( index );
}
}
}
update_coupler_physics();
@@ -291,8 +337,7 @@ vehicle_table::update( double Deltatime, int Iterationcount ) {
auto constexpr STEPS_PER_THREAD { 128 };
std::size_t work { 0 };
for( std::size_t index = 0; index < m_items.size(); ++index ) {
auto const *vehicle { m_items[ index ] };
if( vehicle->physics->PhysicActivation() || vehicle->MechInside ) {
if( facts[ index ].active ) {
work += ( coarse[ index ] != 0 ? 1 : static_cast<std::size_t>( std::max( Iterationcount, 1 ) ) );
}
}
@@ -640,10 +685,8 @@ vehicle_table::save_odometers() {
bool
vehicle_table::update_steps_parallel( double const Deltatime, int const Iterationcount, std::vector<std::uint8_t> const &Coarse, std::size_t const Threads ) {
// the places of the vehicles in the table (m_tableindex) and what the groups are made of are in vehicle_facts, from update()
auto const itemcount { m_items.size() };
for( std::size_t index = 0; index < itemcount; ++index ) {
m_items[ index ]->m_tableindex = static_cast<std::uint32_t>( index );
}
// the groups, by joining the vehicles with the ones they act on. the coupled ones are those next to them
m_groupparent.resize( itemcount );
std::iota( std::begin( m_groupparent ), std::end( m_groupparent ), 0u );
@@ -654,19 +697,12 @@ vehicle_table::update_steps_parallel( double const Deltatime, int const Iteratio
}
return Index; };
for( std::size_t index = 0; index < itemcount; ++index ) {
auto const *physics { m_items[ index ]->physics };
for (auto const end : VehicleEnds) {
auto const *neighbour { physics->Neighbours[ end ].vehicle };
if( ( physics->Couplers[ end ].Connected != nullptr )
&& ( ( neighbour == nullptr ) || ( neighbour->physics != physics->Couplers[ end ].Connected ) ) ) {
return false;
}
if( neighbour == nullptr ) { continue; }
auto const other { neighbour->m_tableindex };
if( ( other >= itemcount ) || ( m_items[ other ] != neighbour ) ) {
// not in the table, shouldn't happen
for( auto const other : facts[ index ].neighbours ) {
// coupled with one it doesn't see next to it, or (shouldn't happen) one not in the table
if( other == vehicle_facts::unseen ) {
return false;
}
if( other == vehicle_facts::none ) { continue; }
auto const left { root( static_cast<std::uint32_t>( index ) ) };
auto const right { root( other ) };
if( left != right ) {
@@ -694,9 +730,8 @@ vehicle_table::update_steps_parallel( double const Deltatime, int const Iteratio
largest = std::max( largest, m_groups[ group ].size() );
auto const loading { std::any_of(
std::begin( m_groups[ group ] ), std::end( m_groups[ group ] ),
[ this ]( std::uint32_t const Index ) {
auto const *vehicle { m_items[ Index ] };
return ( vehicle->physics->LoadStatus() != 0 ) || ( vehicle->LoadExchangeTime() > 0.f ); } ) };
[]( std::uint32_t const Index ) {
return facts[ Index ].loading; } ) };
( loading ? m_maingroups : m_threadgroups ).push_back( static_cast<std::uint32_t>( group ) );
}
std::sort(