135360
From 12776a97a32449f5c0fc6de735c29fc30c0d7d14 Mon Sep 17 00:00:00 2001
a9add1
From: David Tardon <dtardon@redhat.com>
a9add1
Date: Tue, 10 May 2016 09:00:20 +0200
a9add1
Subject: [PATCH] improve perf. of VCL event dispatch
a9add1
a9add1
Change-Id: I5052f0c3e2c8739b336da52ef9590e5008255247
a9add1
---
a9add1
 vcl/inc/window.h             |  4 +++-
135360
 vcl/source/window/event.cxx  | 39 ++++++++++++++++++++++++++++++++++++---
a9add1
 vcl/source/window/window.cxx |  1 +
135360
 3 files changed, 40 insertions(+), 4 deletions(-)
a9add1
a9add1
diff --git a/vcl/inc/window.h b/vcl/inc/window.h
a9add1
index 67d5f94..60f7e82 100644
a9add1
--- a/vcl/inc/window.h
a9add1
+++ b/vcl/inc/window.h
a9add1
@@ -224,7 +224,9 @@ public:
a9add1
     VclPtr<vcl::Window> mpNextOverlap;
a9add1
     VclPtr<vcl::Window> mpLastFocusWindow;
a9add1
     VclPtr<vcl::Window> mpDlgCtrlDownWindow;
a9add1
-    VclEventListeners   maEventListeners;
a9add1
+    std::vector<Link<>> maEventListeners;
a9add1
+    int mnEventListenersIteratingCount;
a9add1
+    std::set<Link<>> maEventListenersDeleted;
a9add1
     VclEventListeners   maChildEventListeners;
a9add1
 
a9add1
     // The canvas interface for this VCL window. Is persistent after the first GetCanvas() call
a9add1
diff --git a/vcl/source/window/event.cxx b/vcl/source/window/event.cxx
135360
index 35c3e38..cbd5b23 100644
a9add1
--- a/vcl/source/window/event.cxx
a9add1
+++ b/vcl/source/window/event.cxx
a9add1
@@ -18,6 +18,7 @@
a9add1
  */
a9add1
 
a9add1
 #include <vcl/event.hxx>
a9add1
+#include <vcl/vclevent.hxx>
a9add1
 #include <vcl/window.hxx>
a9add1
 #include <vcl/dockwin.hxx>
a9add1
 #include <vcl/layout.hxx>
a9add1
@@ -27,6 +28,8 @@
a9add1
 #include <svdata.hxx>
a9add1
 #include <salframe.hxx>
a9add1
 
a9add1
+#include <comphelper/scopeguard.hxx>
a9add1
+
a9add1
 #include <com/sun/star/awt/MouseEvent.hpp>
a9add1
 #include <com/sun/star/awt/KeyModifier.hpp>
a9add1
 #include <com/sun/star/awt/MouseButton.hpp>
135360
@@ -212,7 +215,32 @@ void Window::CallEventListeners( sal_uLong nEvent, void* pData )
a9add1
     if ( aDelData.IsDead() )
a9add1
         return;
a9add1
 
a9add1
-    mpWindowImpl->maEventListeners.Call( &aEvent );
a9add1
+    if (!mpWindowImpl->maEventListeners.empty())
a9add1
+    {
a9add1
+         // Copy the list, because this can be destroyed when calling a Link...
a9add1
+         std::vector<Link<>> aCopy( mpWindowImpl->maEventListeners );
a9add1
+        // we use an iterating counter/flag and a set of deleted Link's to avoid O(n^2) behaviour
a9add1
+        mpWindowImpl->mnEventListenersIteratingCount++;
a9add1
+        auto& rWindowImpl = *mpWindowImpl;
a9add1
+        comphelper::ScopeGuard aGuard(
135360
+            [&rWindowImpl, &aDelData]()
a9add1
+            {
135360
+                if (!aDelData.IsDead())
135360
+                {
135360
+                    rWindowImpl.mnEventListenersIteratingCount--;
135360
+                    if (rWindowImpl.mnEventListenersIteratingCount == 0)
135360
+                        rWindowImpl.maEventListenersDeleted.clear();
135360
+                }
a9add1
+            }
a9add1
+        );
a9add1
+        for ( Link<>& rLink : aCopy )
a9add1
+        {
a9add1
+            if (aDelData.IsDead()) break;
a9add1
+            // check this hasn't been removed in some re-enterancy scenario fdo#47368
a9add1
+            if( rWindowImpl.maEventListenersDeleted.find(rLink) == rWindowImpl.maEventListenersDeleted.end() )
a9add1
+                rLink.Call( &aEvent );
a9add1
+        }
a9add1
+    }
a9add1
 
a9add1
     if ( aDelData.IsDead() )
a9add1
         return;
135360
@@ -245,13 +273,18 @@ void Window::FireVclEvent( VclSimpleEvent* pEvent )
a9add1
 
a9add1
 void Window::AddEventListener( const Link<>& rEventListener )
a9add1
 {
a9add1
-    mpWindowImpl->maEventListeners.addListener( rEventListener );
a9add1
+    mpWindowImpl->maEventListeners.push_back( rEventListener );
a9add1
 }
a9add1
 
a9add1
 void Window::RemoveEventListener( const Link<>& rEventListener )
a9add1
 {
a9add1
     if (mpWindowImpl)
a9add1
-        mpWindowImpl->maEventListeners.removeListener( rEventListener );
a9add1
+    {
a9add1
+        auto& rListeners = mpWindowImpl->maEventListeners;
a9add1
+        rListeners.erase( std::remove(rListeners.begin(), rListeners.end(), rEventListener ), rListeners.end() );
a9add1
+        if (mpWindowImpl->mnEventListenersIteratingCount)
a9add1
+            mpWindowImpl->maEventListenersDeleted.insert(rEventListener);
a9add1
+    }
a9add1
 }
a9add1
 
a9add1
 void Window::AddChildEventListener( const Link<>& rEventListener )
a9add1
diff --git a/vcl/source/window/window.cxx b/vcl/source/window/window.cxx
a9add1
index e8d2b96..cbfc4cd 100644
a9add1
--- a/vcl/source/window/window.cxx
a9add1
+++ b/vcl/source/window/window.cxx
a9add1
@@ -630,6 +630,7 @@ WindowImpl::WindowImpl( WindowType nType )
a9add1
     mpLastFocusWindow                   = NULL;                      // window for focus restore
a9add1
     mpDlgCtrlDownWindow                 = NULL;                      // window for dialog control
a9add1
     mpFirstDel                          = NULL;                      // Dtor notification list
a9add1
+    mnEventListenersIteratingCount = 0;
a9add1
     mpUserData                          = NULL;                      // user data
a9add1
     mpCursor                            = NULL;                      // cursor
a9add1
     mpControlFont                       = NULL;                      // font properties
a9add1
-- 
135360
2.7.3
a9add1