sw/inc/unofieldcoll.hxx             |    5 ++++
 sw/source/core/unocore/unofield.cxx |   42 ++++++++++++++++++++++++++++++++++++
 xmloff/source/text/txtfldi.cxx      |   22 +++---------------
 3 files changed, 51 insertions(+), 18 deletions(-)

New commits:
commit 5723e2054bb90f07fff7368e3f38fbe0311f6b83
Author:     Noel Grandin <noel.gran...@collabora.co.uk>
AuthorDate: Wed Jun 12 12:34:48 2024 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Wed Jun 12 21:12:25 2024 +0200

    tdf#144208 speedup doc with lots of redline (6)
    
    implement a mechanism to look up annotation by name instead of iterating
    through all of them.
    Shaves 5% off the load time
    
    Change-Id: I14bf46cf2aebc5d4b019a3428107b46ac82c5087
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/168705
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>
    Tested-by: Jenkins

diff --git a/sw/inc/unofieldcoll.hxx b/sw/inc/unofieldcoll.hxx
index efe3f814ef4a..a7bfda4bcbfe 100644
--- a/sw/inc/unofieldcoll.hxx
+++ b/sw/inc/unofieldcoll.hxx
@@ -23,6 +23,7 @@
 #include <memory>
 
 #include <com/sun/star/util/XRefreshable.hpp>
+#include <com/sun/star/container/XUniqueIDAccess.hpp>
 
 #include "unocoll.hxx"
 
@@ -68,6 +69,7 @@ typedef ::cppu::WeakImplHelper
 <   css::container::XEnumerationAccess
 ,   css::lang::XServiceInfo
 ,   css::util::XRefreshable
+,   css::container::XUniqueIDAccess
 > SwXTextFieldTypes_Base;
 
 class SwXTextFieldTypes final
@@ -111,6 +113,9 @@ public:
             const css::uno::Reference<
                 css::util::XRefreshListener>& xListener) override;
 
+    // container::XUniqueIDAccess
+    virtual css::uno::Any SAL_CALL getByUniqueID( const OUString& ID ) 
override;
+    virtual void SAL_CALL removeByUniqueID( const OUString& ID ) override;
 };
 
 #endif
diff --git a/sw/source/core/unocore/unofield.cxx 
b/sw/source/core/unocore/unofield.cxx
index 152c1aa3bc44..f0fb208aa8cf 100644
--- a/sw/source/core/unocore/unofield.cxx
+++ b/sw/source/core/unocore/unofield.cxx
@@ -2939,6 +2939,48 @@ void SAL_CALL SwXTextFieldTypes::removeRefreshListener(
     m_pImpl->m_RefreshListeners.removeInterface(aGuard, xListener);
 }
 
+// This is specifically for looking up annotations, so we only need to search 
a couple of places
+css::uno::Any SAL_CALL SwXTextFieldTypes::getByUniqueID(const OUString& ID)
+{
+    SolarMutexGuard aGuard;
+    uno::Any aRet;
+    auto& rDoc = GetDoc();
+
+    const SwFieldTypes* pFieldTypes = 
rDoc.getIDocumentFieldsAccess().GetFieldTypes();
+    auto fieldTypeIt = std::find_if(pFieldTypes->begin(), pFieldTypes->end(),
+                           [](const std::unique_ptr<SwFieldType>& pType) {
+                               return pType->Which() == SwFieldIds::Postit;
+                            });
+    const SwFieldType & rCurType = **fieldTypeIt;
+    std::vector<SwFormatField*> vFormatFields;
+    rCurType.GatherFields(vFormatFields);
+    for (const SwFormatField* pFormatField : vFormatFields)
+    {
+        const SwPostItField* pField = static_cast<const 
SwPostItField*>(pFormatField->GetField());
+        if (pField->GetName() == ID)
+        {
+            aRet <<= 
uno::Reference<beans::XPropertySet>(SwXTextField::CreateXTextField(&rDoc, 
pFormatField));
+            return aRet;
+        }
+    }
+
+    IDocumentMarkAccess& rMarksAccess(*rDoc.getIDocumentMarkAccess());
+    auto it = rMarksAccess.findMark(ID);
+    if (it != rMarksAccess.getAllMarksEnd())
+    {
+        aRet <<= 
uno::Reference<beans::XPropertySet>(SwXFieldmark::CreateXFieldmark(rDoc, *it));
+        if (aRet.hasValue())
+            return aRet;
+    }
+
+    return aRet;
+}
+
+void SAL_CALL SwXTextFieldTypes::removeByUniqueID(const OUString& /*ID*/)
+{
+    throw uno::RuntimeException("unsupported");
+}
+
 class SwXFieldEnumeration::Impl
     : public SvtListener
 {
diff --git a/xmloff/source/text/txtfldi.cxx b/xmloff/source/text/txtfldi.cxx
index a4a602b63665..3f75fa118311 100644
--- a/xmloff/source/text/txtfldi.cxx
+++ b/xmloff/source/text/txtfldi.cxx
@@ -61,6 +61,7 @@
 #include <com/sun/star/util/XUpdatable.hpp>
 #include <com/sun/star/sdb/CommandType.hpp>
 #include <com/sun/star/container/XIndexReplace.hpp>
+#include <com/sun/star/container/XUniqueIDAccess.hpp>
 
 #include <sax/tools/converter.hxx>
 
@@ -3256,24 +3257,9 @@ void 
XMLAnnotationImportContext::endFastElement(sal_Int32 /*nElement*/)
             Reference<XTextFieldsSupplier> 
xTextFieldsSupplier(GetImport().GetModel(), UNO_QUERY);
             if (!xTextFieldsSupplier)
                 return;
-            uno::Reference<container::XEnumerationAccess> 
xFieldsAccess(xTextFieldsSupplier->getTextFields());
-            uno::Reference<container::XEnumeration> 
xFields(xFieldsAccess->createEnumeration());
-            while (xFields->hasMoreElements())
-            {
-                uno::Reference<beans::XPropertySet> 
xCurrField(xFields->nextElement(), uno::UNO_QUERY);
-                uno::Reference<beans::XPropertySetInfo> const xInfo(
-                        xCurrField->getPropertySetInfo());
-                if (xInfo->hasPropertyByName(sAPI_name))
-                {
-                    OUString aFieldName;
-                    xCurrField->getPropertyValue(sAPI_name) >>= aFieldName;
-                    if (aFieldName == aName)
-                    {
-                        xPrevField.set( xCurrField, uno::UNO_QUERY );
-                        break;
-                    }
-                }
-            }
+            uno::Reference<container::XUniqueIDAccess> 
xFieldsAccess(xTextFieldsSupplier->getTextFields(), UNO_QUERY_THROW);
+            uno::Any aAny = xFieldsAccess->getByUniqueID(aName);
+            aAny >>= xPrevField;
         }
         if ( xPrevField.is() )
         {

Reply via email to