From a05e20f1bfb84657e76b9bb2f7817ffbb2aef5f4 Mon Sep 17 00:00:00 2001 From: Secundario Date: Tue, 17 Sep 2019 18:00:49 -0300 Subject: [PATCH] Do a single CoordStack allocation per thread --- src/simulation/Simulation.cpp | 22 ++++++++++++++++++---- src/simulation/Simulation.h | 5 +++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index 7f9db6b37..3b8aadcb0 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -599,6 +599,13 @@ bool Simulation::FloodFillPmapCheck(int x, int y, int type) return TYP(pmap[y][x]) == type; } +CoordStack& Simulation::getCoordStackSingleton() +{ + // Future-proofing in case Simulation is later multithreaded + thread_local CoordStack cs; + return cs; +} + int Simulation::flood_prop(int x, int y, size_t propoffset, PropertyValue propvalue, StructProperty::PropertyType proptype) { int i, x1, x2, dy = 1; @@ -614,7 +621,9 @@ int Simulation::flood_prop(int x, int y, size_t propoffset, PropertyValue propva memset(bitmap, 0, XRES*YRES); try { - CoordStack cs; + CoordStack& cs = getCoordStackSingleton(); + cs.clear(); + cs.push(x, y); do { @@ -745,7 +754,8 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm) if (TYP(pmap[y][x])!=cm || parts[ID(pmap[y][x])].life!=0) return 1; - CoordStack cs; + CoordStack& cs = getCoordStackSingleton(); + cs.clear(); cs.push(x, y); @@ -857,7 +867,9 @@ bool Simulation::flood_water(int x, int y, int i) try { - CoordStack cs; + CoordStack& cs = getCoordStackSingleton(); + cs.clear(); + cs.push(x, y); do { @@ -1272,7 +1284,9 @@ void Simulation::ApplyDecorationFill(Renderer *ren, int x, int y, int colR, int try { - CoordStack cs; + CoordStack& cs = getCoordStackSingleton(); + cs.clear(); + cs.push(x, y); do { diff --git a/src/simulation/Simulation.h b/src/simulation/Simulation.h index ba430d649..a68fad7b1 100644 --- a/src/simulation/Simulation.h +++ b/src/simulation/Simulation.h @@ -13,6 +13,8 @@ #include "GOLMenu.h" #include "MenuSection.h" +#include "CoordStack.h" + #include "elements/Element.h" #define CHANNELS ((int)(MAX_TEMP-73)/100+2) @@ -220,6 +222,9 @@ public: String ElementResolve(int type, int ctype); String BasicParticleInfo(Particle const &sample_part); + +private: + CoordStack& getCoordStackSingleton(); }; #endif /* SIMULATION_H */