From 6b5bbb177dd5cb9c133c717fff4a0b60c98cf993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Mon, 9 Dec 2024 16:32:05 +0100 Subject: [PATCH] Stop copying the gravity process's input every frame In the vast majority of cases (i.e. when Gravity::Exchange is indirectly called from BeforeSim), the input is cleared to zeros anyway, so it's less work in Exchange to actually just exchange the old input with the new one than to copy the new one into the old one. Also make gravity functions private in Simulation. --- src/simulation/Simulation.cpp | 6 ++++++ src/simulation/Simulation.h | 7 ++++--- src/simulation/gravity/Fft.cpp | 4 ++-- src/simulation/gravity/Gravity.h | 3 ++- src/simulation/gravity/Null.cpp | 2 +- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 2c8239513..c338932c6 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -3740,6 +3740,7 @@ void Simulation::BeforeSim() air->update_airh(); DispatchNewtonianGravity(); + // gravIn is now potentially garbage, which is ok, we were going to clear it for the frame anyway for (auto p : gravIn.mass.Size().OriginRect()) { gravIn.mass[p] = 0.f; @@ -3964,6 +3965,8 @@ void Simulation::ResetNewtonianGravity(GravityInput newGravIn, GravityOutput new { gravIn = newGravIn; DispatchNewtonianGravity(); + // gravIn is now potentially garbage, set it again + gravIn = newGravIn; if (grav) { gravOut = newGravOut; @@ -3981,7 +3984,10 @@ void Simulation::EnableNewtonianGravity(bool enable) if (!grav && enable) { grav = Gravity::Create(); + auto oldGravIn = gravIn; DispatchNewtonianGravity(); + // gravIn is now potentially garbage, set it again + gravIn = std::move(oldGravIn); } } diff --git a/src/simulation/Simulation.h b/src/simulation/Simulation.h index e4ea5ae44..94dc66608 100644 --- a/src/simulation/Simulation.h +++ b/src/simulation/Simulation.h @@ -220,10 +220,11 @@ public: ~Simulation(); void EnableNewtonianGravity(bool enable); - void ResetNewtonianGravity(GravityInput newGravIn, GravityOutput newGravOut); - void DispatchNewtonianGravity(); - void UpdateGravityMask(); private: CoordStack& getCoordStackSingleton(); + + void ResetNewtonianGravity(GravityInput newGravIn, GravityOutput newGravOut); + void DispatchNewtonianGravity(); + void UpdateGravityMask(); }; diff --git a/src/simulation/gravity/Fft.cpp b/src/simulation/gravity/Fft.cpp index 43757263f..5cec0295a 100644 --- a/src/simulation/gravity/Fft.cpp +++ b/src/simulation/gravity/Fft.cpp @@ -204,7 +204,7 @@ void GravityImpl::Init() }); } -void Gravity::Exchange(GravityOutput &gravOut, const GravityInput &gravIn) +void Gravity::Exchange(GravityOutput &gravOut, GravityInput &gravIn) { auto *fftGravity = static_cast(this); @@ -231,7 +231,7 @@ void Gravity::Exchange(GravityOutput &gravOut, const GravityInput &gravIn) std::memcmp(&fftGravity->gravIn.mask[{ 0, 0 }], &gravIn.mask[{ 0, 0 }], NCELL * sizeof(float))) { fftGravity->copyGravOut = true; - fftGravity->gravIn = gravIn; + std::swap(gravIn, fftGravity->gravIn); } fftGravity->Dispatch(); diff --git a/src/simulation/gravity/Gravity.h b/src/simulation/gravity/Gravity.h index cab143200..da14c0f5a 100644 --- a/src/simulation/gravity/Gravity.h +++ b/src/simulation/gravity/Gravity.h @@ -8,7 +8,8 @@ protected: Gravity() = default; public: - void Exchange(GravityOutput &gravOut, const GravityInput &gravIn); + // potentially clobbers gravIn + void Exchange(GravityOutput &gravOut, GravityInput &gravIn); static GravityPtr Create(); }; diff --git a/src/simulation/gravity/Null.cpp b/src/simulation/gravity/Null.cpp index c57b8d057..772f02560 100644 --- a/src/simulation/gravity/Null.cpp +++ b/src/simulation/gravity/Null.cpp @@ -1,6 +1,6 @@ #include "Gravity.h" -void Gravity::Exchange(GravityOutput &gravOut, const GravityInput &gravIn) +void Gravity::Exchange(GravityOutput &gravOut, GravityInput &gravIn) { }