writerfilter/source/rtftok/rtfvalue.cxx |  140 +++++++++++++++++---------------
 writerfilter/source/rtftok/rtfvalue.hxx |   25 ++---
 2 files changed, 89 insertions(+), 76 deletions(-)

New commits:
commit 7678e7ed88007061c3469db3b28b0e91acea7ed6
Author:     Vasily Melenchuk <vasily.melenc...@cib.de>
AuthorDate: Thu Mar 17 22:57:51 2022 +0300
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Tue Apr 26 08:34:55 2022 +0200

    rtf filter: reduce memory allocations count
    
    RTFValue in most cases contain only one value: either num, string,
    picture, etc. No reason to allocate all of them in the same time.
    
    RTF filter should became faster and consume less memory, however I
    did not made any measuremets.
    
    Change-Id: Ic12a941ce774b9066d7c57b38139af07851ac7b7
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/131720
    Tested-by: Jenkins
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>

diff --git a/writerfilter/source/rtftok/rtfvalue.cxx 
b/writerfilter/source/rtftok/rtfvalue.cxx
index e4b3a5a11c68..42f60a1c97d7 100644
--- a/writerfilter/source/rtftok/rtfvalue.cxx
+++ b/writerfilter/source/rtftok/rtfvalue.cxx
@@ -15,106 +15,73 @@ using namespace com::sun::star;
 
 namespace writerfilter::rtftok
 {
-RTFValue::RTFValue(int nValue, OUString sValue, const RTFSprms& rAttributes, 
const RTFSprms& rSprms,
+RTFValue::RTFValue(int nValue, OUString sValue, const RTFSprms* pAttributes, 
const RTFSprms* pSprms,
                    uno::Reference<drawing::XShape> xShape, 
uno::Reference<io::XInputStream> xStream,
                    uno::Reference<embed::XEmbeddedObject> xObject, bool 
bForceString,
-                   const RTFShape& aShape, const RTFPicture& rPicture)
+                   const RTFShape* pShape, const RTFPicture* pPicture)
     : m_nValue(nValue)
     , m_sValue(std::move(sValue))
-    , m_pAttributes(new RTFSprms(rAttributes))
-    , m_pSprms(new RTFSprms(rSprms))
     , m_xShape(std::move(xShape))
     , m_xStream(std::move(xStream))
     , m_xObject(std::move(xObject))
     , m_bForceString(bForceString)
-    , m_pShape(new RTFShape(aShape))
-    , m_pPicture(new RTFPicture(rPicture))
 {
+    if (pAttributes)
+        m_pAttributes = new RTFSprms(*pAttributes);
+    if (pSprms)
+        m_pSprms = new RTFSprms(*pSprms);
+    if (pShape)
+        m_pShape = new RTFShape(*pShape);
+    if (pPicture)
+        m_pPicture = new RTFPicture(*pPicture);
 }
 
-RTFValue::RTFValue()
-    : m_pAttributes(new RTFSprms())
-    , m_pSprms(new RTFSprms())
-    , m_pShape(new RTFShape())
-    , m_pPicture(new RTFPicture)
-{
-}
+RTFValue::RTFValue() {}
 
 RTFValue::RTFValue(int nValue)
     : m_nValue(nValue)
-    , m_pAttributes(new RTFSprms())
-    , m_pSprms(new RTFSprms())
-    , m_pShape(new RTFShape())
-    , m_pPicture(new RTFPicture)
 {
 }
 
 RTFValue::RTFValue(OUString sValue, bool bForce)
     : m_sValue(std::move(sValue))
-    , m_pAttributes(new RTFSprms())
-    , m_pSprms(new RTFSprms())
     , m_bForceString(bForce)
-    , m_pShape(new RTFShape())
-    , m_pPicture(new RTFPicture)
 {
 }
 
 RTFValue::RTFValue(const RTFSprms& rAttributes)
     : m_pAttributes(new RTFSprms(rAttributes))
-    , m_pSprms(new RTFSprms())
-    , m_pShape(new RTFShape())
-    , m_pPicture(new RTFPicture)
 {
 }
 
 RTFValue::RTFValue(const RTFSprms& rAttributes, const RTFSprms& rSprms)
     : m_pAttributes(new RTFSprms(rAttributes))
     , m_pSprms(new RTFSprms(rSprms))
-    , m_pShape(new RTFShape())
-    , m_pPicture(new RTFPicture)
 {
 }
 
 RTFValue::RTFValue(uno::Reference<drawing::XShape> xShape)
-    : m_pAttributes(new RTFSprms())
-    , m_pSprms(new RTFSprms())
-    , m_xShape(std::move(xShape))
-    , m_pShape(new RTFShape())
-    , m_pPicture(new RTFPicture)
+    : m_xShape(std::move(xShape))
 {
 }
 
 RTFValue::RTFValue(uno::Reference<io::XInputStream> xStream)
-    : m_pAttributes(new RTFSprms())
-    , m_pSprms(new RTFSprms())
-    , m_xStream(std::move(xStream))
-    , m_pShape(new RTFShape())
-    , m_pPicture(new RTFPicture)
+    : m_xStream(std::move(xStream))
 {
 }
 
 RTFValue::RTFValue(uno::Reference<embed::XEmbeddedObject> xObject)
-    : m_pAttributes(new RTFSprms())
-    , m_pSprms(new RTFSprms())
-    , m_xObject(std::move(xObject))
-    , m_pShape(new RTFShape())
-    , m_pPicture(new RTFPicture)
+    : m_xObject(std::move(xObject))
 {
 }
 
 RTFValue::RTFValue(const RTFShape& aShape)
-    : m_pAttributes(new RTFSprms())
-    , m_pSprms(new RTFSprms())
-    , m_pShape(new RTFShape(aShape))
-    , m_pPicture(new RTFPicture)
+    : m_pShape(new RTFShape(aShape))
 {
 }
 
 RTFValue::RTFValue(const RTFPicture& rPicture)
-    : m_pAttributes(new RTFSprms())
-    , m_pSprms(new RTFSprms())
-    , m_pShape(new RTFShape())
-    , m_pPicture(new RTFPicture(rPicture))
+    : m_pPicture(new RTFPicture(rPicture))
 {
 }
 
@@ -148,13 +115,23 @@ uno::Any RTFValue::getAny() const
     return ret;
 }
 
-RTFShape& RTFValue::getShape() const { return *m_pShape; }
+RTFShape& RTFValue::getShape() const
+{
+    if (!m_pShape)
+        m_pShape = new RTFShape();
+    return *m_pShape;
+}
 
-RTFPicture& RTFValue::getPicture() const { return *m_pPicture; }
+RTFPicture& RTFValue::getPicture() const
+{
+    if (!m_pPicture)
+        m_pPicture = new RTFPicture;
+    return *m_pPicture;
+}
 
 writerfilter::Reference<Properties>::Pointer_t RTFValue::getProperties()
 {
-    return new RTFReferenceProperties(*m_pAttributes, *m_pSprms);
+    return new RTFReferenceProperties(getAttributes(), getSprms());
 }
 
 writerfilter::Reference<BinaryObj>::Pointer_t RTFValue::getBinary()
@@ -172,16 +149,16 @@ std::string RTFValue::toString() const
 }
 #endif
 
-RTFValue* RTFValue::Clone()
+RTFValue* RTFValue::Clone() const
 {
-    return new RTFValue(m_nValue, m_sValue, *m_pAttributes, *m_pSprms, 
m_xShape, m_xStream,
-                        m_xObject, m_bForceString, *m_pShape, *m_pPicture);
+    return new RTFValue(m_nValue, m_sValue, m_pAttributes.get(), 
m_pSprms.get(), m_xShape,
+                        m_xStream, m_xObject, m_bForceString, m_pShape.get(), 
m_pPicture.get());
 }
 
-RTFValue* RTFValue::CloneWithSprms(RTFSprms const& rAttributes, RTFSprms 
const& rSprms)
+RTFValue* RTFValue::CloneWithSprms(RTFSprms const& rAttributes, RTFSprms 
const& rSprms) const
 {
-    return new RTFValue(m_nValue, m_sValue, rAttributes, rSprms, m_xShape, 
m_xStream, m_xObject,
-                        m_bForceString, *m_pShape, *m_pPicture);
+    return new RTFValue(m_nValue, m_sValue, &rAttributes, &rSprms, m_xShape, 
m_xStream, m_xObject,
+                        m_bForceString, m_pShape.get(), m_pPicture.get());
 }
 
 bool RTFValue::equals(const RTFValue& rOther) const
@@ -190,20 +167,55 @@ bool RTFValue::equals(const RTFValue& rOther) const
         return false;
     if (m_sValue != rOther.m_sValue)
         return false;
-    if (m_pAttributes->size() != rOther.m_pAttributes->size())
+
+    if (m_pAttributes && rOther.m_pAttributes)
+    {
+        if (m_pAttributes->size() != rOther.m_pAttributes->size())
+            return false;
+        if (!m_pAttributes->equals(rOther))
+            return false;
+    }
+    else if (m_pAttributes && m_pAttributes->size())
+    {
         return false;
-    if (!m_pAttributes->equals(rOther))
+    }
+    else if (rOther.m_pAttributes && rOther.m_pAttributes->size())
+    {
         return false;
-    if (m_pSprms->size() != rOther.m_pSprms->size())
+    }
+
+    if (m_pSprms && rOther.m_pSprms)
+    {
+        if (m_pSprms->size() != rOther.m_pSprms->size())
+            return false;
+        if (!m_pSprms->equals(rOther))
+            return false;
+    }
+    else if (m_pSprms && m_pSprms->size())
+    {
         return false;
-    if (!m_pSprms->equals(rOther))
+    }
+    else if (rOther.m_pSprms && rOther.m_pSprms->size())
+    {
         return false;
+    }
+
     return true;
 }
 
-RTFSprms& RTFValue::getAttributes() { return *m_pAttributes; }
+RTFSprms& RTFValue::getAttributes() const
+{
+    if (!m_pAttributes)
+        m_pAttributes = new RTFSprms();
+    return *m_pAttributes;
+}
 
-RTFSprms& RTFValue::getSprms() { return *m_pSprms; }
+RTFSprms& RTFValue::getSprms() const
+{
+    if (!m_pSprms)
+        m_pSprms = new RTFSprms();
+    return *m_pSprms;
+}
 
 } // namespace writerfilter::rtftok
 
diff --git a/writerfilter/source/rtftok/rtfvalue.hxx 
b/writerfilter/source/rtftok/rtfvalue.hxx
index af120d469ba1..4f37a5dcbcec 100644
--- a/writerfilter/source/rtftok/rtfvalue.hxx
+++ b/writerfilter/source/rtftok/rtfvalue.hxx
@@ -31,13 +31,14 @@ class RTFPicture;
 /// Value of an RTF keyword
 class RTFValue : public Value
 {
-public:
-    using Pointer_t = tools::SvRef<RTFValue>;
-    RTFValue(int nValue, OUString sValue, const RTFSprms& rAttributes, const 
RTFSprms& rSprms,
+    RTFValue(int nValue, OUString sValue, const RTFSprms* pAttributes, const 
RTFSprms* pSprms,
              css::uno::Reference<css::drawing::XShape> xShape,
              css::uno::Reference<css::io::XInputStream> xStream,
              css::uno::Reference<css::embed::XEmbeddedObject> xObject, bool 
bForceString,
-             const RTFShape& aShape, const RTFPicture& rPicture);
+             const RTFShape* pShape, const RTFPicture* pPicture);
+
+public:
+    using Pointer_t = tools::SvRef<RTFValue>;
     RTFValue();
     explicit RTFValue(int nValue);
     RTFValue(OUString sValue, bool bForce = false);
@@ -58,10 +59,10 @@ public:
 #ifdef DBG_UTIL
     std::string toString() const override;
 #endif
-    RTFValue* Clone();
-    RTFValue* CloneWithSprms(RTFSprms const& rAttributes, RTFSprms const& 
rSprms);
-    RTFSprms& getAttributes();
-    RTFSprms& getSprms();
+    RTFValue* Clone() const;
+    RTFValue* CloneWithSprms(RTFSprms const& rAttributes, RTFSprms const& 
rSprms) const;
+    RTFSprms& getAttributes() const;
+    RTFSprms& getSprms() const;
     RTFShape& getShape() const;
     RTFPicture& getPicture() const;
     bool equals(const RTFValue& rOther) const;
@@ -70,14 +71,14 @@ public:
 private:
     int m_nValue = 0;
     OUString m_sValue;
-    tools::SvRef<RTFSprms> m_pAttributes;
-    tools::SvRef<RTFSprms> m_pSprms;
+    mutable tools::SvRef<RTFSprms> m_pAttributes;
+    mutable tools::SvRef<RTFSprms> m_pSprms;
     css::uno::Reference<css::drawing::XShape> m_xShape;
     css::uno::Reference<css::io::XInputStream> m_xStream;
     css::uno::Reference<css::embed::XEmbeddedObject> m_xObject;
     bool m_bForceString = false;
-    tools::SvRef<RTFShape> m_pShape;
-    tools::SvRef<RTFPicture> m_pPicture;
+    mutable tools::SvRef<RTFShape> m_pShape;
+    mutable tools::SvRef<RTFPicture> m_pPicture;
 };
 } // namespace writerfilter::rtftok
 

Reply via email to