[Libreoffice-commits] core.git: svl/source

Luboš Luňák (via logerrit) logerrit at kemper.freedesktop.org
Wed Jun 24 15:48:37 UTC 2020


 svl/source/notify/broadcast.cxx |   21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

New commits:
commit 07cda8035c34ca9f8ac3ba911a2b691349665fc7
Author:     Luboš Luňák <l.lunak at collabora.com>
AuthorDate: Wed Jun 24 12:33:37 2020 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Wed Jun 24 17:47:43 2020 +0200

    optimize SvtBroadcaster::Normalize() (tdf#132454)
    
    New items are only appended, so it's wasteful to std::sort() the whole
    container, just sort the unsorted part (often just one item) and then
    merge.
    
    Change-Id: I20b73730700c279e8f844c0b7a392a8f372a22da
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/97019
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/svl/source/notify/broadcast.cxx b/svl/source/notify/broadcast.cxx
index 94ef1588e016..cc1ffcb10917 100644
--- a/svl/source/notify/broadcast.cxx
+++ b/svl/source/notify/broadcast.cxx
@@ -20,6 +20,7 @@
 #include <svl/broadcast.hxx>
 #include <svl/listener.hxx>
 #include <svl/hint.hxx>
+#include <o3tl/safeint.hxx>
 #include <cassert>
 #include <algorithm>
 
@@ -27,8 +28,24 @@ void SvtBroadcaster::Normalize() const
 {
     if (!mbNormalized)
     {
-        std::sort(maListeners.begin(), maListeners.end());
-        mbNormalized = true;
+        // Add() only appends new values, so often the container will be sorted expect for one
+        // or few last items. For larger containers it is much more efficient to just sort
+        // the unsorted part and then merge.
+        if(maListeners.size() > 100)
+        {
+            auto sortedEnd = std::is_sorted_until(maListeners.begin(),maListeners.end());
+            if( o3tl::make_unsigned( sortedEnd - maListeners.begin()) > maListeners.size() * 3 / 4 )
+            {
+                std::sort( sortedEnd, maListeners.end());
+                std::inplace_merge( maListeners.begin(), sortedEnd, maListeners.end());
+                mbNormalized = true;
+            }
+        }
+        if (!mbNormalized)
+        {
+            std::sort(maListeners.begin(), maListeners.end());
+            mbNormalized = true;
+        }
     }
 
     if (!mbDestNormalized)


More information about the Libreoffice-commits mailing list