[Libreoffice-commits] core.git: Branch 'private/kohei/calc-data-stream' - sc/source

Kohei Yoshida kohei.yoshida at collabora.com
Thu Jan 2 11:44:01 PST 2014


 sc/source/core/tool/scmatrix.cxx |   78 ++++++++++++++++++++++++++++++---------
 1 file changed, 60 insertions(+), 18 deletions(-)

New commits:
commit 3ec61ffcbe95a64a2fe72f21c1ee1cd531d09755
Author: Kohei Yoshida <kohei.yoshida at collabora.com>
Date:   Thu Jan 2 14:43:04 2014 -0500

    Speed up matrix element access by storing last access position.
    
    This should speed up normal element access if the elements are accessed
    in sequential order.
    
    Change-Id: Ia5c688ed79aabbc1fdcfc4dd5bf4b25359a585d5

diff --git a/sc/source/core/tool/scmatrix.cxx b/sc/source/core/tool/scmatrix.cxx
index 1a5ebbb..ba6d578 100644
--- a/sc/source/core/tool/scmatrix.cxx
+++ b/sc/source/core/tool/scmatrix.cxx
@@ -191,6 +191,7 @@ _Comp CompareMatrixElemFunc<_Comp>::maComp;
 class ScMatrixImpl
 {
     MatrixImplType maMat;
+    mutable MatrixImplType::const_position_type maMatPos;
     MatrixImplType maMatFlag;
     ScInterpreter* pErrorInterpreter;
     bool            mbCloneIfConst; // Whether the matrix is cloned with a CloneIfConst() call.
@@ -287,13 +288,22 @@ private:
 };
 
 ScMatrixImpl::ScMatrixImpl(SCSIZE nC, SCSIZE nR) :
-    maMat(nR, nC), maMatFlag(nR, nC), pErrorInterpreter(NULL), mbCloneIfConst(true) {}
+    maMat(nR, nC), maMatFlag(nR, nC), pErrorInterpreter(NULL), mbCloneIfConst(true)
+{
+    maMatPos = maMat.end_position();
+}
 
 ScMatrixImpl::ScMatrixImpl(SCSIZE nC, SCSIZE nR, double fInitVal) :
-    maMat(nR, nC, fInitVal), maMatFlag(nR, nC), pErrorInterpreter(NULL), mbCloneIfConst(true) {}
+    maMat(nR, nC, fInitVal), maMatFlag(nR, nC), pErrorInterpreter(NULL), mbCloneIfConst(true)
+{
+    maMatPos = maMat.end_position();
+}
 
 ScMatrixImpl::ScMatrixImpl( size_t nC, size_t nR, const std::vector<bool>& rInitVals ) :
-    maMat(nR, nC, rInitVals.begin(), rInitVals.end()), maMatFlag(nR, nC), pErrorInterpreter(NULL), mbCloneIfConst(true) {}
+    maMat(nR, nC, rInitVals.begin(), rInitVals.end()), maMatFlag(nR, nC), pErrorInterpreter(NULL), mbCloneIfConst(true)
+{
+    maMatPos = maMat.end_position();
+}
 
 ScMatrixImpl::~ScMatrixImpl()
 {
@@ -303,6 +313,7 @@ ScMatrixImpl::~ScMatrixImpl()
 void ScMatrixImpl::Clear()
 {
     maMat.clear();
+    maMatPos = maMat.end_position();
     maMatFlag.clear();
 }
 
@@ -319,12 +330,14 @@ bool ScMatrixImpl::IsImmutable() const
 void ScMatrixImpl::Resize(SCSIZE nC, SCSIZE nR)
 {
     maMat.resize(nR, nC);
+    maMatPos = maMat.end_position();
     maMatFlag.resize(nR, nC);
 }
 
 void ScMatrixImpl::Resize(SCSIZE nC, SCSIZE nR, double fVal)
 {
     maMat.resize(nR, nC, fVal);
+    maMatPos = maMat.end_position();
     maMatFlag.resize(nR, nC);
 }
 
@@ -390,7 +403,10 @@ void ScMatrixImpl::SetErrorAtInterpreter( sal_uInt16 nError ) const
 void ScMatrixImpl::PutDouble(double fVal, SCSIZE nC, SCSIZE nR)
 {
     if (ValidColRow( nC, nR))
+    {
         maMat.set(nR, nC, fVal);
+        maMatPos = maMat.end_position();
+    }
     else
     {
         OSL_FAIL("ScMatrixImpl::PutDouble: dimension error");
@@ -400,7 +416,10 @@ void ScMatrixImpl::PutDouble(double fVal, SCSIZE nC, SCSIZE nR)
 void ScMatrixImpl::PutDouble(const double* pArray, size_t nLen, SCSIZE nC, SCSIZE nR)
 {
     if (ValidColRow( nC, nR))
+    {
         maMat.set(nR, nC, pArray, pArray + nLen);
+        maMatPos = maMat.end_position();
+    }
     else
     {
         OSL_FAIL("ScMatrixImpl::PutDouble: dimension error");
@@ -417,7 +436,10 @@ void ScMatrixImpl::PutDouble( double fVal, SCSIZE nIndex)
 void ScMatrixImpl::PutString(const svl::SharedString& rStr, SCSIZE nC, SCSIZE nR)
 {
     if (ValidColRow( nC, nR))
+    {
         maMat.set(nR, nC, rStr);
+        maMatPos = maMat.end_position();
+    }
     else
     {
         OSL_FAIL("ScMatrixImpl::PutString: dimension error");
@@ -427,7 +449,10 @@ void ScMatrixImpl::PutString(const svl::SharedString& rStr, SCSIZE nC, SCSIZE nR
 void ScMatrixImpl::PutString(const svl::SharedString* pArray, size_t nLen, SCSIZE nC, SCSIZE nR)
 {
     if (ValidColRow( nC, nR))
+    {
         maMat.set(nR, nC, pArray, pArray + nLen);
+        maMatPos = maMat.end_position();
+    }
     else
     {
         OSL_FAIL("ScMatrixImpl::PutString: dimension error");
@@ -446,6 +471,7 @@ void ScMatrixImpl::PutEmpty(SCSIZE nC, SCSIZE nR)
     if (ValidColRow( nC, nR))
     {
         maMat.set_empty(nR, nC);
+        maMatPos = maMat.end_position();
         maMatFlag.set(nR, nC, false); // zero flag to indicate that this is 'empty', not 'empty path'.
     }
     else
@@ -459,6 +485,7 @@ void ScMatrixImpl::PutEmptyPath(SCSIZE nC, SCSIZE nR)
     if (ValidColRow( nC, nR))
     {
         maMat.set_empty(nR, nC);
+        maMatPos = maMat.end_position();
         maMatFlag.set(nR, nC, true); // non-zero flag to indicate empty 'path'.
     }
     else
@@ -470,12 +497,16 @@ void ScMatrixImpl::PutEmptyPath(SCSIZE nC, SCSIZE nR)
 void ScMatrixImpl::PutError( sal_uInt16 nErrorCode, SCSIZE nC, SCSIZE nR )
 {
     maMat.set(nR, nC, CreateDoubleError(nErrorCode));
+    maMatPos = maMat.end_position();
 }
 
 void ScMatrixImpl::PutBoolean(bool bVal, SCSIZE nC, SCSIZE nR)
 {
     if (ValidColRow( nC, nR))
+    {
         maMat.set(nR, nC, bVal);
+        maMatPos = maMat.end_position();
+    }
     else
     {
         OSL_FAIL("ScMatrixImpl::PutBoolean: dimension error");
@@ -486,7 +517,8 @@ sal_uInt16 ScMatrixImpl::GetError( SCSIZE nC, SCSIZE nR) const
 {
     if (ValidColRowOrReplicated( nC, nR ))
     {
-        double fVal = maMat.get_numeric(nR, nC);
+        maMatPos = maMat.position(maMatPos, nR, nC);
+        double fVal = maMat.get_numeric(maMatPos);
         return GetDoubleErrorValue(fVal);
     }
     else
@@ -500,7 +532,8 @@ double ScMatrixImpl::GetDouble(SCSIZE nC, SCSIZE nR) const
 {
     if (ValidColRowOrReplicated( nC, nR ))
     {
-        double fVal = maMat.get_numeric(nR, nC);
+        maMatPos = maMat.position(maMatPos, nR, nC);
+        double fVal = maMat.get_numeric(maMatPos);
         if ( pErrorInterpreter )
         {
             sal_uInt16 nError = GetDoubleErrorValue(fVal);
@@ -528,17 +561,17 @@ svl::SharedString ScMatrixImpl::GetString(SCSIZE nC, SCSIZE nR) const
     if (ValidColRowOrReplicated( nC, nR ))
     {
         double fErr = 0.0;
-        MatrixImplType::const_position_type aPos = maMat.position(nR, nC);
-        switch (maMat.get_type(aPos))
+        maMatPos = maMat.position(maMatPos, nR, nC);
+        switch (maMat.get_type(maMatPos))
         {
             case mdds::mtm::element_string:
-                return maMat.get_string(aPos);
+                return maMat.get_string(maMatPos);
             case mdds::mtm::element_empty:
                 return svl::SharedString::getEmptyString();
             case mdds::mtm::element_numeric:
             case mdds::mtm::element_boolean:
                 OSL_FAIL("ScMatrixImpl::GetString: access error, no string");
-                fErr = maMat.get_numeric(aPos);
+                fErr = maMat.get_numeric(maMatPos);
             default:
                 OSL_FAIL("ScMatrixImpl::GetString: access error, no string");
         }
@@ -567,11 +600,11 @@ svl::SharedString ScMatrixImpl::GetString( SvNumberFormatter& rFormatter, SCSIZE
     }
 
     double fVal = 0.0;
-    MatrixImplType::const_position_type aPos = maMat.position(nR, nC);
-    switch (maMat.get_type(aPos))
+    maMatPos = maMat.position(maMatPos, nR, nC);
+    switch (maMat.get_type(maMatPos))
     {
         case mdds::mtm::element_string:
-            return maMat.get_string(aPos).getString();
+            return maMat.get_string(maMatPos).getString();
         case mdds::mtm::element_empty:
         {
             if (!maMatFlag.get<bool>(nR, nC))
@@ -588,7 +621,7 @@ svl::SharedString ScMatrixImpl::GetString( SvNumberFormatter& rFormatter, SCSIZE
         }
         case mdds::mtm::element_numeric:
         case mdds::mtm::element_boolean:
-            fVal = maMat.get_numeric(aPos);
+            fVal = maMat.get_numeric(maMatPos);
         break;
         default:
             ;
@@ -613,21 +646,21 @@ ScMatrixValue ScMatrixImpl::Get(SCSIZE nC, SCSIZE nR) const
     ScMatrixValue aVal;
     if (ValidColRowOrReplicated(nC, nR))
     {
-        MatrixImplType::const_position_type aPos = maMat.position(nR, nC);
-        mdds::mtm::element_t eType = maMat.get_type(aPos);
+        maMatPos = maMat.position(maMatPos, nR, nC);
+        mdds::mtm::element_t eType = maMat.get_type(maMatPos);
         switch (eType)
         {
             case mdds::mtm::element_boolean:
                 aVal.nType = SC_MATVAL_BOOLEAN;
-                aVal.fVal = maMat.get_boolean(aPos);
+                aVal.fVal = maMat.get_boolean(maMatPos);
             break;
             case mdds::mtm::element_numeric:
                 aVal.nType = SC_MATVAL_VALUE;
-                aVal.fVal = maMat.get_numeric(aPos);
+                aVal.fVal = maMat.get_numeric(maMatPos);
             break;
             case mdds::mtm::element_string:
                 aVal.nType = SC_MATVAL_STRING;
-                aVal.aStr = maMat.get_string(aPos);
+                aVal.aStr = maMat.get_string(maMatPos);
             break;
             case mdds::mtm::element_empty:
                 // Empty path equals empty plus flag.
@@ -739,12 +772,14 @@ void ScMatrixImpl::MatCopy(ScMatrixImpl& mRes) const
     }
 
     mRes.maMat.copy(maMat);
+    mRes.maMatPos = mRes.maMat.end_position();
 }
 
 void ScMatrixImpl::MatTrans(ScMatrixImpl& mRes) const
 {
     mRes.maMat = maMat;
     mRes.maMat.transpose();
+    mRes.maMatPos = mRes.maMat.end_position();
 }
 
 void ScMatrixImpl::FillDouble( double fVal, SCSIZE nC1, SCSIZE nR1, SCSIZE nC2, SCSIZE nR2 )
@@ -756,6 +791,7 @@ void ScMatrixImpl::FillDouble( double fVal, SCSIZE nC1, SCSIZE nR1, SCSIZE nC2,
             // Passing value array is much faster.
             std::vector<double> aVals(nR2-nR1+1, fVal);
             maMat.set(nR1, j, aVals.begin(), aVals.end());
+            maMatPos = maMat.end_position();
         }
     }
     else
@@ -770,6 +806,7 @@ void ScMatrixImpl::CompareEqual()
     CompareMatrixElemFunc<ElemEqualZero> aFunc(aSize.row, aSize.column);
     maMat.walk(aFunc);
     aFunc.swap(maMat);
+    maMatPos = maMat.end_position();
 }
 
 void ScMatrixImpl::CompareNotEqual()
@@ -778,6 +815,7 @@ void ScMatrixImpl::CompareNotEqual()
     CompareMatrixElemFunc<ElemNotEqualZero> aFunc(aSize.row, aSize.column);
     maMat.walk(aFunc);
     aFunc.swap(maMat);
+    maMatPos = maMat.end_position();
 }
 
 void ScMatrixImpl::CompareLess()
@@ -786,6 +824,7 @@ void ScMatrixImpl::CompareLess()
     CompareMatrixElemFunc<ElemLessZero> aFunc(aSize.row, aSize.column);
     maMat.walk(aFunc);
     aFunc.swap(maMat);
+    maMatPos = maMat.end_position();
 }
 
 void ScMatrixImpl::CompareGreater()
@@ -794,6 +833,7 @@ void ScMatrixImpl::CompareGreater()
     CompareMatrixElemFunc<ElemGreaterZero> aFunc(aSize.row, aSize.column);
     maMat.walk(aFunc);
     aFunc.swap(maMat);
+    maMatPos = maMat.end_position();
 }
 
 void ScMatrixImpl::CompareLessEqual()
@@ -802,6 +842,7 @@ void ScMatrixImpl::CompareLessEqual()
     CompareMatrixElemFunc<ElemLessEqualZero> aFunc(aSize.row, aSize.column);
     maMat.walk(aFunc);
     aFunc.swap(maMat);
+    maMatPos = maMat.end_position();
 }
 
 void ScMatrixImpl::CompareGreaterEqual()
@@ -810,6 +851,7 @@ void ScMatrixImpl::CompareGreaterEqual()
     CompareMatrixElemFunc<ElemGreaterEqualZero> aFunc(aSize.row, aSize.column);
     maMat.walk(aFunc);
     aFunc.swap(maMat);
+    maMatPos = maMat.end_position();
 }
 
 namespace {


More information about the Libreoffice-commits mailing list