sc/inc/interpretercontext.hxx              |    3 +-
 sc/source/core/inc/interpre.hxx            |    6 -----
 sc/source/core/tool/interpr1.cxx           |   31 ++++++++---------------------
 sc/source/core/tool/interpretercontext.cxx |    4 +++
 4 files changed, 16 insertions(+), 28 deletions(-)

New commits:
commit 7450af4f2545446574c7ce0b5b56aaaddffd486c
Author:     Caolán McNamara <caolan.mcnam...@collabora.com>
AuthorDate: Mon Mar 11 17:30:05 2024 +0000
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Wed Mar 13 18:22:51 2024 +0100

    move rng out of ScInterpreter and into the ScInterpreterContext
    
    the former is created a lot, while the latter lives in a reusable
    pool
    
    Change-Id: I9a4802aa7db0b591e1835768851aa9093331e918
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/164691
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/sc/inc/interpretercontext.hxx b/sc/inc/interpretercontext.hxx
index 39d528cb6ca9..8e2f654ff285 100644
--- a/sc/inc/interpretercontext.hxx
+++ b/sc/inc/interpretercontext.hxx
@@ -9,8 +9,8 @@
 
 #pragma once
 
-#include <vector>
 #include <memory>
+#include <random>
 #include "types.hxx"
 
 namespace formula
@@ -60,6 +60,7 @@ struct ScInterpreterContext
     // Allocation cache for "aConditions" array in 
ScInterpreter::IterateParameterIfs()
     // This is populated/used only when formula-group threading is enabled.
     std::vector<sal_uInt8> maConditions;
+    std::mt19937 aRNG;
     ScInterpreter* pInterpreter;
 
     ScInterpreterContext(const ScDocument& rDoc, SvNumberFormatter* 
pFormatter);
diff --git a/sc/source/core/inc/interpre.hxx b/sc/source/core/inc/interpre.hxx
index f33f548640f2..ab456a4dca86 100644
--- a/sc/source/core/inc/interpre.hxx
+++ b/sc/source/core/inc/interpre.hxx
@@ -34,7 +34,6 @@
 
 #include <map>
 #include <memory>
-#include <random>
 #include <vector>
 #include <limits>
 #include <ostream>
@@ -187,7 +186,6 @@ private:
 
     ScCalcConfig maCalcConfig;
     formula::FormulaTokenIterator aCode;
-    std::optional<std::mt19937> oRNG;
     ScAddress   aPos;
     ScTokenArray* pArr;
     ScInterpreterContext& mrContext;
@@ -505,8 +503,6 @@ private:
     // Returns true if last jump was executed and result matrix pushed.
     bool JumpMatrix( short nStackLevel );
 
-    std::mt19937& GetRNG();
-
     double Compare( ScQueryOp eOp );
     /** @param pOptions
             NULL means case sensitivity document option is to be used!
@@ -531,7 +527,7 @@ private:
     void ScPi();
     void ScRandom();
     void ScRandbetween();
-    void ScRandomImpl( const std::function<double( std::mt19937& rRng, double 
fFirst, double fLast )>& RandomFunc,
+    void ScRandomImpl( const std::function<double( double fFirst, double fLast 
)>& RandomFunc,
             double fFirst, double fLast );
     void ScTrue();
     void ScFalse();
diff --git a/sc/source/core/tool/interpr1.cxx b/sc/source/core/tool/interpr1.cxx
index f5a69a0e7159..6ae277cc4429 100644
--- a/sc/source/core/tool/interpr1.cxx
+++ b/sc/source/core/tool/interpr1.cxx
@@ -67,13 +67,12 @@
 #include <compare.hxx>
 #include <comphelper/lok.hxx>
 #include <comphelper/processfactory.hxx>
-#include <comphelper/random.hxx>
 #include <comphelper/string.hxx>
 #include <svl/sharedstringpool.hxx>
 
 #include <stdlib.h>
-#include <vector>
 #include <memory>
+#include <vector>
 #include <limits>
 #include <string_view>
 #include <cmath>
@@ -1737,7 +1736,7 @@ void ScInterpreter::ScPi()
     PushDouble(M_PI);
 }
 
-void ScInterpreter::ScRandomImpl( const std::function<double( std::mt19937& 
rRng, double fFirst, double fLast )>& RandomFunc,
+void ScInterpreter::ScRandomImpl( const std::function<double( double fFirst, 
double fLast )>& RandomFunc,
         double fFirst, double fLast )
 {
     if (bMatrixFormula)
@@ -1764,7 +1763,7 @@ void ScInterpreter::ScRandomImpl( const 
std::function<double( std::mt19937& rRng
             // default are executed in array context unless
             // FA.setPropertyValue("IsArrayFunction",False) was set, return a
             // scalar double instead of a 1x1 matrix object. tdf#128218
-            PushDouble( RandomFunc( GetRNG(), fFirst, fLast));
+            PushDouble( RandomFunc( fFirst, fLast));
             return;
         }
 
@@ -1785,7 +1784,7 @@ void ScInterpreter::ScRandomImpl( const 
std::function<double( std::mt19937& rRng
             {
                 for (SCROW j=0; j < nRows; ++j)
                 {
-                    pResMat->PutDouble( RandomFunc( GetRNG(), fFirst, fLast),
+                    pResMat->PutDouble( RandomFunc( fFirst, fLast),
                             static_cast<SCSIZE>(i), static_cast<SCSIZE>(j));
                 }
             }
@@ -1794,28 +1793,16 @@ void ScInterpreter::ScRandomImpl( const 
std::function<double( std::mt19937& rRng
     }
     else
     {
-        PushDouble( RandomFunc( GetRNG(), fFirst, fLast));
-    }
-}
-
-std::mt19937& ScInterpreter::GetRNG()
-{
-    if (!oRNG)
-    {
-        // create a per-interpreter Random Number Generator, seeded from the 
global rng, so we don't have
-        // to lock a mutex to generate a random number
-        unsigned int nSeed(comphelper::rng::uniform_uint_distribution(0, 
std::numeric_limits<sal_uInt32>::max()));
-        oRNG = std::mt19937(nSeed);
+        PushDouble( RandomFunc( fFirst, fLast));
     }
-    return *oRNG;
 }
 
 void ScInterpreter::ScRandom()
 {
-    auto RandomFunc = [](std::mt19937& rRNG, double, double)
+    auto RandomFunc = [this]( double, double )
     {
         std::uniform_real_distribution<double> dist(0.0, 1.0);
-        return dist(rRNG);
+        return dist(mrContext.aRNG);
     };
     ScRandomImpl( RandomFunc, 0.0, 0.0);
 }
@@ -1835,10 +1822,10 @@ void ScInterpreter::ScRandbetween()
         return;
     }
     fMax = std::nextafter( fMax+1, -DBL_MAX);
-    auto RandomFunc = [](std::mt19937& rRNG, double fFirst, double fLast)
+    auto RandomFunc = [this]( double fFirst, double fLast )
     {
         std::uniform_real_distribution<double> dist(fFirst, fLast);
-        return floor(dist(rRNG));
+        return floor(dist(mrContext.aRNG));
     };
     ScRandomImpl( RandomFunc, fMin, fMax);
 }
diff --git a/sc/source/core/tool/interpretercontext.cxx 
b/sc/source/core/tool/interpretercontext.cxx
index deb6f6d0ed5b..41bf1b4efac0 100644
--- a/sc/source/core/tool/interpretercontext.cxx
+++ b/sc/source/core/tool/interpretercontext.cxx
@@ -22,6 +22,7 @@
 #include <svl/zforlist.hxx>
 
 #include <document.hxx>
+#include <comphelper/random.hxx>
 #include <formula/token.hxx>
 #include <lookupcache.hxx>
 #include <rangecache.hxx>
@@ -34,6 +35,9 @@ ScInterpreterContext::ScInterpreterContext(const ScDocument& 
rDoc, SvNumberForma
     : mpDoc(&rDoc)
     , mnTokenCachePos(0)
     , maTokens(TOKEN_CACHE_SIZE, nullptr)
+    // create a per-interpreter Random Number Generator, seeded from the 
global rng, so we don't have
+    // to lock a mutex to generate a random number
+    , aRNG(comphelper::rng::uniform_uint_distribution(0, 
std::numeric_limits<sal_uInt32>::max()))
     , pInterpreter(nullptr)
     , mpFormatter(pFormatter)
 {

Reply via email to