From 18d1c58994fe141d9f56aa131c9d5bf2aa88fa6e Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Mon, 30 Nov 2026 04:20:18 +0100 Subject: [PATCH] perf(world): FastGetTFromS reads the curve's t and slope of a point side by side TSegment kept the curve parameter t at equal steps of s (fTsBuffer) and the Hermite slopes at the same points (fTsSlope) in two vectors, and FastGetTFromS(), called for every axle of every vehicle on every step, read [i] and [i + 1] of both: four loads from two separate allocations. The two now go together in one vector of {t, slope} (fTs), so the interpolation touches one or two cache lines. The values and the arithmetic, in the same order, are unchanged. Tomaszewo (raj40821) 600 s in --test, seed 12345: FastGetTFromS was 1.2% of the main thread; sim 259.7 s median of 3 (255.0-271.3) against 268.3 s (262.5-279.2) before, which is about the run-to-run spread. The 14 physics logs and the event log are byte-identical, unit tests 1205/1205 with exact golden traces. --- world/Segment.cpp | 57 ++++++++++++++++++++++++----------------------- world/Segment.h | 10 ++++++--- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/world/Segment.cpp b/world/Segment.cpp index 6e0326c33..f9ec7f85e 100644 --- a/world/Segment.cpp +++ b/world/Segment.cpp @@ -187,11 +187,11 @@ bool TSegment::InitChain(std::vector> Pieces) fDirection = -std::atan2(Point2.x - Point1.x, Point2.z - Point1.z); fStoop = std::atan2(Point2.y - Point1.y, fLength); fStep = fLength / iSegCount; - fTsBuffer.resize( iSegCount + 1 ); + fTs.resize( iSegCount + 1 ); for( int i = 0; i <= iSegCount; ++i ) { - fTsBuffer[ i ] = static_cast( i ) / iSegCount; + fTs[ i ].t = static_cast( i ) / iSegCount; + fTs[ i ].slope = fStep / fLength; } - fTsSlope.assign( iSegCount + 1, fStep / fLength ); return true; } @@ -204,28 +204,27 @@ std::size_t TSegment::piece_at(double const s) const void TSegment::InitTFromSInterpolateData() { // pre-compute the curve parameter t at equal distance intervals - fTsBuffer.resize( iSegCount + 1 ); - fTsBuffer[ 0 ] = 0.0; + fTs.resize( iSegCount + 1 ); + fTs[ 0 ].t = 0.0; for( int i = 1; i < iSegCount; ++i ) { - fTsBuffer[ i ] = GetTFromS( i * fStep ); + fTs[ i ].t = GetTFromS( i * fStep ); } - fTsBuffer[ iSegCount ] = 1.0; + fTs[ iSegCount ].t = 1.0; // pre-compute Hermite slopes dt/du = fStep / |B'(t)| for FastGetTFromS // (dt/ds = 1/|B'(t)|, scaled by fStep to the interval coordinate u) - fTsSlope.resize( iSegCount + 1 ); for( int i = 0; i <= iSegCount; ++i ) { // compute ds/dt derivative = |B'(t)| - double const speed = glm::length( GetFirstDerivative( fTsBuffer[ i ] ) ); + double const speed = glm::length( GetFirstDerivative( fTs[ i ].t ) ); // compute dt/du derivative = fStep / (ds/dt) - fTsSlope[ i ] = speed > 0.0 ? fStep / speed : 0.0; + fTs[ i ].slope = speed > 0.0 ? fStep / speed : 0.0; } // clamp slopes to keep t(s) monotone (PCHIP / Fritsch-Carlson condition) for( int i = 0; i <= iSegCount; ++i ) { double limit = std::numeric_limits::max(); - if( i > 0 ) { limit = std::min( limit, 3.0 * ( fTsBuffer[ i ] - fTsBuffer[ i - 1 ] ) ); } - if( i < iSegCount ) { limit = std::min( limit, 3.0 * ( fTsBuffer[ i + 1 ] - fTsBuffer[ i ] ) ); } - fTsSlope[ i ] = std::min( fTsSlope[ i ], limit ); + if( i > 0 ) { limit = std::min( limit, 3.0 * ( fTs[ i ].t - fTs[ i - 1 ].t ) ); } + if( i < iSegCount ) { limit = std::min( limit, 3.0 * ( fTs[ i + 1 ].t - fTs[ i ].t ) ); } + fTs[ i ].slope = std::min( fTs[ i ].slope, limit ); } } @@ -296,7 +295,7 @@ double TSegment::GetTFromS(double const s) const double TSegment::FastGetTFromS(double const s) const { // Faster version of GetTFromS: cubic Hermite interpolation using - // the precomputed values in fTsBuffer and slopes in fTsSlope. It + // the precomputed values and slopes in fTs. It // avoids the expensive RombergIntegral+Newton per call. Hermite // interpolation is C1-continuous, so no sudden speed changes when // moving from one interval to the next. @@ -310,10 +309,12 @@ double TSegment::FastGetTFromS(double const s) const double const u2 = u * u; double const u3 = u2 * u; double const h01 = -2.0 * u3 + 3.0 * u2; - double const t = ( ( 1.0 - h01 ) * fTsBuffer[ i ] + - ( u3 - 2.0 * u2 + u ) * fTsSlope [ i ] + - h01 * fTsBuffer[ i + 1 ] + - ( u3 - u2 ) * fTsSlope [ i + 1 ] ); + auto const &from { fTs[ i ] }; + auto const &to { fTs[ i + 1 ] }; + double const t = ( ( 1.0 - h01 ) * from.t + + ( u3 - 2.0 * u2 + u ) * from.slope + + h01 * to.t + + ( u3 - u2 ) * to.slope ); return t; } @@ -375,11 +376,11 @@ TSegment::find_nearest_point( glm::dvec3 const &Point ) const { auto const segmentpoint = std::clamp( nearest_segment_point( - glm::dvec3{ FastGetPoint( fTsBuffer[ segmentidx ] ) }, - glm::dvec3{ FastGetPoint( fTsBuffer[ segmentidx + 1 ] ) }, + glm::dvec3{ FastGetPoint( fTs[ segmentidx ].t ) }, + glm::dvec3{ FastGetPoint( fTs[ segmentidx + 1 ].t ) }, Point ) // point in range 0-1 on current segment - * ( fTsBuffer[ segmentidx + 1 ] - fTsBuffer[ segmentidx ] ) // segment length - + fTsBuffer[ segmentidx ], // segment offset + * ( fTs[ segmentidx + 1 ].t - fTs[ segmentidx ].t ) // segment length + + fTs[ segmentidx ].t, // segment offset 0.0, 1.0 ); // we clamp the range in case there's some floating point math inaccuracies auto const segmentdistance = glm::length2( Point - glm::dvec3{ FastGetPoint( segmentpoint ) } ); @@ -545,7 +546,7 @@ void TSegment::RaPositionGet(double const fDistance, glm::dvec3 &position, glm:: }; void TSegment::plan_frame( double const Distance, glm::dvec3 &Point, glm::dvec3 &Across ) const { - if( fTsBuffer.empty() ) { + if( fTs.empty() ) { // never made into a path: tt_Cross and the like Point = Point1; Across = glm::dvec3 { 0.0 }; @@ -573,7 +574,7 @@ bool TSegment::RenderLoft( gfx::vertex_array &Output, glm::dvec3 const &Origin, // standardowo tworzy triangle_strip dla prostego albo ich zestaw dla łuku // po modyfikacji - dla ujemnego (iNumShapePoints) w dodatkowych polach tabeli podany jest przekrój końcowy - if( fTsBuffer.empty() ) + if( fTs.empty() ) return false; // prowizoryczne zabezpieczenie przed wysypem - ustalić faktyczną przyczynę glm::vec3 pos1; @@ -602,7 +603,7 @@ bool TSegment::RenderLoft( gfx::vertex_array &Output, glm::dvec3 const &Origin, tv1 = 1.0; // Ra: to by można było wyliczać dla odcinka, wyglądało by lepiej s = static_cast(fStep * iSkip); // iSkip - ile odcinków z początku pominąć int i = iSkip; // domyślnie 0 - t = static_cast(fTsBuffer[ i ]); // tabela wattości t dla segmentów + t = static_cast(fTs[ i ].t); // tabela wattości t dla segmentów // BUG: length of spline can be 0, we should skip geometry generation for such cases fOffset = static_cast(0.1 / fLength); // pierwsze 10cm pos1 = glm::dvec3{ FastGetPoint( t ) - Origin }; // wektor początku segmentu @@ -653,7 +654,7 @@ bool TSegment::RenderLoft( gfx::vertex_array &Output, glm::dvec3 const &Origin, tv1 = clamp_circular( tv1, 1.0f ); tv2 = tv1 - step / texturelength; // mapowanie na końcu segmentu - t = static_cast(fTsBuffer[ i ]); // szybsze od GetTFromS(s); + t = static_cast(fTs[ i ].t); // szybsze od GetTFromS(s); pos2 = glm::dvec3{ FastGetPoint( t ) - Origin }; dir = glm::dvec3{ FastGetDirection( t, fOffset ) }; // nowy wektor kierunku parallel2 = glm::vec3{ -dir.z, 0.f, dir.x }; // wektor poprzeczny @@ -769,7 +770,7 @@ void TSegment::roll_vertex( gfx::basic_vertex &Vertex, glm::vec3 const &Position bool TSegment::RenderLoftSteps( gfx::vertex_array &Output, glm::dvec3 const &Origin, gfx::vertex_array const &ShapePoints, double fTextureLength, int const Steps ) const { - if( fTsBuffer.empty() || Steps < 1 || fLength <= 0.0 ) + if( fTs.empty() || Steps < 1 || fLength <= 0.0 ) return false; auto const shapepoints { ShapePoints.size() / 2 }; @@ -1431,7 +1432,7 @@ gfx::vertex_array const &low_section::profile_at( gfx::vertex_array const &Shape bool TSegment::RenderLoftSpan( gfx::vertex_array &Output, glm::dvec3 const &Origin, gfx::vertex_array const &ShapePoints, loft_span const &Span ) const { - if( fTsBuffer.empty() || Span.to - Span.from < 0.001 || ShapePoints.empty() ) + if( fTs.empty() || Span.to - Span.from < 0.001 || ShapePoints.empty() ) return false; auto const stations { loft_stations( Span, ShapePoints, std::max( fStep, 0.2 ), m_dense ) }; diff --git a/world/Segment.h b/world/Segment.h index 88aa9adff..26ee986d1 100644 --- a/world/Segment.h +++ b/world/Segment.h @@ -116,8 +116,12 @@ class TSegment }; setting m_setting; double fLength { -1.0 }; // długość policzona - std::vector fTsBuffer; // wartości parametru krzywej t dla równych odcinków s - std::vector fTsSlope; // nachylenie dt/du (= dt/ds * fStep) w tych samych punktach + // the curve at equal intervals of s, for FastGetTFromS(): the two it reads of a point side by side, in one cache line + struct ts_point { + double t; // wartość parametru krzywej t dla równych odcinków s + double slope; // nachylenie dt/du (= dt/ds * fStep) w tym punkcie + }; + std::vector fTs; double fStep = 0.0; int iSegCount = 0; // ilość odcinków do rysowania krzywej double fDirection = 0.0; // Ra: kąt prostego w planie; dla łuku kąt od Point1 @@ -275,7 +279,7 @@ public: inline int RaSegCount() const { - return fTsBuffer.empty() ? 1 : iSegCount; }; + return fTs.empty() ? 1 : iSegCount; }; void render_lines(std::vector &out, float quality = 1.0f) const;