Title: [201809] trunk
Revision
201809
Author
beid...@apple.com
Date
2016-06-08 10:31:47 -0700 (Wed, 08 Jun 2016)

Log Message

Make CrossThreadCopier more efficient (fewer copies!).
https://bugs.webkit.org/show_bug.cgi?id=158456

Reviewed by Alex Christensen.

Source/WTF:

Previously, we'd run all arguments through CrossThreadCopier, then immediately make
an unnecessary copy of the result during lambda capture.

Instead, we should just put the CrossThreadCopier generated objects directly in lambdas,
which are then captured by NoncopyableFunctions.

This reduces the number of constructor calls per argument from 2 copies to 1 move.

* wtf/CrossThreadTask.h:
(WTF::CrossThreadTask::CrossThreadTask):
(WTF::createCrossThreadTask):

Tools:

* TestWebKitAPI/Tests/WTF/CrossThreadTask.cpp:
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (201808 => 201809)


--- trunk/Source/WTF/ChangeLog	2016-06-08 17:31:12 UTC (rev 201808)
+++ trunk/Source/WTF/ChangeLog	2016-06-08 17:31:47 UTC (rev 201809)
@@ -1,3 +1,22 @@
+2016-06-08  Brady Eidson  <beid...@apple.com>
+
+        Make CrossThreadCopier more efficient (fewer copies!).
+        https://bugs.webkit.org/show_bug.cgi?id=158456
+
+        Reviewed by Alex Christensen.
+
+        Previously, we'd run all arguments through CrossThreadCopier, then immediately make
+        an unnecessary copy of the result during lambda capture.
+
+        Instead, we should just put the CrossThreadCopier generated objects directly in lambdas,
+        which are then captured by NoncopyableFunctions.
+
+        This reduces the number of constructor calls per argument from 2 copies to 1 move.
+
+        * wtf/CrossThreadTask.h:
+        (WTF::CrossThreadTask::CrossThreadTask):
+        (WTF::createCrossThreadTask):
+
 2016-06-07  Filip Pizlo  <fpi...@apple.com>
 
         Implement Air::allocateStack() in ES6 to see how much of a bad idea that is

Modified: trunk/Source/WTF/wtf/CrossThreadTask.h (201808 => 201809)


--- trunk/Source/WTF/wtf/CrossThreadTask.h	2016-06-08 17:31:12 UTC (rev 201808)
+++ trunk/Source/WTF/wtf/CrossThreadTask.h	2016-06-08 17:31:47 UTC (rev 201809)
@@ -32,13 +32,31 @@
 namespace WTF {
 
 class CrossThreadTask {
+    template<class T>
+    friend CrossThreadTask createCrossThreadTask(T&, void (T::*)());
+    template<class T, class P1, class MP1>
+    friend CrossThreadTask createCrossThreadTask(T&, void (T::*)(MP1), const P1&);
+    template<class T, class P1, class MP1, class P2, class MP2>
+    friend CrossThreadTask createCrossThreadTask(T&, void (T::*)(MP1, MP2), const P1&, const P2&);
+    template<class T, class P1, class MP1, class P2, class MP2, class P3, class MP3>
+    friend CrossThreadTask createCrossThreadTask(T&, void (T::*)(MP1, MP2, MP3), const P1&, const P2&, const P3&);
+    template<class T, class P1, class MP1, class P2, class MP2, class P3, class MP3, class P4, class MP4>
+    friend CrossThreadTask createCrossThreadTask(T&, void (T::*)(MP1, MP2, MP3, MP4), const P1&, const P2&, const P3&, const P4&);
+    template<class T, class P1, class MP1, class P2, class MP2, class P3, class MP3, class P4, class MP4, class P5, class MP5>
+    friend CrossThreadTask createCrossThreadTask(T&, void (T::*)(MP1, MP2, MP3, MP4, MP5), const P1&, const P2&, const P3&, const P4&, const P5&);
+    template<class T, class P1, class MP1, class P2, class MP2, class P3, class MP3, class P4, class MP4, class P5, class MP5, class P6, class MP6>
+    friend CrossThreadTask createCrossThreadTask(T&, void (T::*)(MP1, MP2, MP3, MP4, MP5, MP6), const P1&, const P2&, const P3&, const P4&, const P5&, const P6&);
+    template<class T, class P1, class MP1, class P2, class MP2, class P3, class MP3, class P4, class MP4, class P5, class MP5, class P6, class MP6, class P7, class MP7>
+    friend CrossThreadTask createCrossThreadTask(T&, void (T::*)(MP1, MP2, MP3, MP4, MP5, MP6, MP7), const P1&, const P2&, const P3&, const P4&, const P5&, const P6&, const P7&);
+    template<class T, class P1, class MP1, class P2, class MP2, class P3, class MP3, class P4, class MP4, class P5, class MP5, class P6, class MP6, class P7, class MP7, class P8, class MP8>
+    friend CrossThreadTask createCrossThreadTask(T&, void (T::*)(MP1, MP2, MP3, MP4, MP5, MP6, MP7, MP8), const P1&, const P2&, const P3&, const P4&, const P5&, const P6&, const P7&, const P8&);
 public:
     CrossThreadTask() = default;
 
     CrossThreadTask(NoncopyableFunction<void ()>&& taskFunction)
         : m_taskFunction(WTFMove(taskFunction))
     {
-        ASSERT(taskFunction);
+        ASSERT(m_taskFunction);
     }
 
     void performTask()
@@ -50,34 +68,14 @@
     NoncopyableFunction<void ()> m_taskFunction;
 };
 
-template <typename T, typename... Arguments>
-class CrossThreadTaskImpl final : public CrossThreadTask {
-public:
-    CrossThreadTaskImpl(T* callee, void (T::*method)(Arguments...), Arguments&&... arguments)
-    {
-        m_taskFunction = [callee, method, arguments...] {
-            (callee->*method)(arguments...);
-        };
-    }
-};
-
-template <typename... Arguments>
-class CrossThreadTaskStaticImpl final : public CrossThreadTask {
-public:
-    CrossThreadTaskStaticImpl(void (*method)(Arguments...), Arguments&&... arguments)
-    {
-        m_taskFunction = [method, arguments...] {
-            method(arguments...);
-        };
-    }
-};
-
 template<typename T>
 CrossThreadTask createCrossThreadTask(
     T& callee,
     void (T::*method)())
 {
-    return CrossThreadTaskImpl<T>(&callee, method);
+    return CrossThreadTask([callee = &callee, method]() mutable {
+        (callee->*method)();
+    });
 }
 
 template<typename T, typename P1, typename MP1>
@@ -86,10 +84,10 @@
     void (T::*method)(MP1),
     const P1& parameter1)
 {
-    return CrossThreadTaskImpl<T, MP1>(
-        &callee,
-        method,
-        WTF::CrossThreadCopier<P1>::copy(parameter1));
+    return CrossThreadTask([callee = &callee, method,
+        p1 = CrossThreadCopier<P1>::copy(parameter1)]() mutable {
+        (callee->*method)(p1);
+    });
 }
 
 template<typename T, typename P1, typename MP1, typename P2, typename MP2>
@@ -99,12 +97,11 @@
     const P1& parameter1,
     const P2& parameter2)
 {
-    return CrossThreadTaskImpl<T, MP1, MP2>(
-        &callee,
-        method,
-        WTF::CrossThreadCopier<P1>::copy(parameter1),
-        WTF::CrossThreadCopier<P2>::copy(parameter2));
-
+    return CrossThreadTask([callee = &callee, method,
+        p1 = CrossThreadCopier<P1>::copy(parameter1),
+        p2 = CrossThreadCopier<P2>::copy(parameter2)]() mutable {
+        (callee->*method)(p1, p2);
+    });
 }
 
 template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3>
@@ -115,12 +112,12 @@
     const P2& parameter2,
     const P3& parameter3)
 {
-    return CrossThreadTaskImpl<T, MP1, MP2, MP3>(
-        &callee,
-        method,
-        WTF::CrossThreadCopier<P1>::copy(parameter1),
-        WTF::CrossThreadCopier<P2>::copy(parameter2),
-        WTF::CrossThreadCopier<P3>::copy(parameter3));
+    return CrossThreadTask([callee = &callee, method,
+        p1 = CrossThreadCopier<P1>::copy(parameter1),
+        p2 = CrossThreadCopier<P2>::copy(parameter2),
+        p3 = CrossThreadCopier<P3>::copy(parameter3)]() mutable {
+        (callee->*method)(p1, p2, p3);
+    });
 }
 
 template<typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3>
@@ -130,11 +127,12 @@
     const P2& parameter2,
     const P3& parameter3)
 {
-    return CrossThreadTaskStaticImpl<MP1, MP2, MP3>(
-        method,
-        WTF::CrossThreadCopier<P1>::copy(parameter1),
-        WTF::CrossThreadCopier<P2>::copy(parameter2),
-        WTF::CrossThreadCopier<P3>::copy(parameter3));
+    return CrossThreadTask([method,
+        p1 = CrossThreadCopier<P1>::copy(parameter1),
+        p2 = CrossThreadCopier<P2>::copy(parameter2),
+        p3 = CrossThreadCopier<P3>::copy(parameter3)]() mutable {
+        method(p1, p2, p3);
+    });
 }
 
 template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3, typename P4, typename MP4>
@@ -146,13 +144,13 @@
     const P3& parameter3,
     const P4& parameter4)
 {
-    return CrossThreadTaskImpl<T, MP1, MP2, MP3, MP4>(
-        &callee,
-        method,
-        WTF::CrossThreadCopier<P1>::copy(parameter1),
-        WTF::CrossThreadCopier<P2>::copy(parameter2),
-        WTF::CrossThreadCopier<P3>::copy(parameter3),
-        WTF::CrossThreadCopier<P4>::copy(parameter4));
+    return CrossThreadTask([callee = &callee, method,
+        p1 = CrossThreadCopier<P1>::copy(parameter1),
+        p2 = CrossThreadCopier<P2>::copy(parameter2),
+        p3 = CrossThreadCopier<P3>::copy(parameter3),
+        p4 = CrossThreadCopier<P4>::copy(parameter4)]() mutable {
+        (callee->*method)(p1, p2, p3, p4);
+    });
 }
 
 template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3, typename P4, typename MP4, typename P5, typename MP5>
@@ -165,14 +163,14 @@
     const P4& parameter4,
     const P5& parameter5)
 {
-    return CrossThreadTaskImpl<T, MP1, MP2, MP3, MP4, MP5>(
-        &callee,
-        method,
-        WTF::CrossThreadCopier<P1>::copy(parameter1),
-        WTF::CrossThreadCopier<P2>::copy(parameter2),
-        WTF::CrossThreadCopier<P3>::copy(parameter3),
-        WTF::CrossThreadCopier<P4>::copy(parameter4),
-        WTF::CrossThreadCopier<P5>::copy(parameter5));
+    return CrossThreadTask([callee = &callee, method,
+        p1 = CrossThreadCopier<P1>::copy(parameter1),
+        p2 = CrossThreadCopier<P2>::copy(parameter2),
+        p3 = CrossThreadCopier<P3>::copy(parameter3),
+        p4 = CrossThreadCopier<P4>::copy(parameter4),
+        p5 = CrossThreadCopier<P5>::copy(parameter5)]() mutable {
+        (callee->*method)(p1, p2, p3, p4, p5);
+    });
 }
 
 template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3, typename P4, typename MP4, typename P5, typename MP5, typename P6, typename MP6>
@@ -186,15 +184,15 @@
     const P5& parameter5,
     const P6& parameter6)
 {
-    return CrossThreadTaskImpl<T, MP1, MP2, MP3, MP4, MP5, MP6>(
-        &callee,
-        method,
-        WTF::CrossThreadCopier<P1>::copy(parameter1),
-        WTF::CrossThreadCopier<P2>::copy(parameter2),
-        WTF::CrossThreadCopier<P3>::copy(parameter3),
-        WTF::CrossThreadCopier<P4>::copy(parameter4),
-        WTF::CrossThreadCopier<P5>::copy(parameter5),
-        WTF::CrossThreadCopier<P6>::copy(parameter6));
+    return CrossThreadTask([callee = &callee, method,
+        p1 = CrossThreadCopier<P1>::copy(parameter1),
+        p2 = CrossThreadCopier<P2>::copy(parameter2),
+        p3 = CrossThreadCopier<P3>::copy(parameter3),
+        p4 = CrossThreadCopier<P4>::copy(parameter4),
+        p5 = CrossThreadCopier<P5>::copy(parameter5),
+        p6 = CrossThreadCopier<P6>::copy(parameter6)]() mutable {
+        (callee->*method)(p1, p2, p3, p4, p5, p6);
+    });
 }
 
 template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3, typename P4, typename MP4, typename P5, typename MP5, typename P6, typename MP6, typename P7, typename MP7>
@@ -209,16 +207,16 @@
     const P6& parameter6,
     const P7& parameter7)
 {
-    return CrossThreadTaskImpl<T, MP1, MP2, MP3, MP4, MP5, MP6, MP7>(
-        &callee,
-        method,
-        WTF::CrossThreadCopier<P1>::copy(parameter1),
-        WTF::CrossThreadCopier<P2>::copy(parameter2),
-        WTF::CrossThreadCopier<P3>::copy(parameter3),
-        WTF::CrossThreadCopier<P4>::copy(parameter4),
-        WTF::CrossThreadCopier<P5>::copy(parameter5),
-        WTF::CrossThreadCopier<P6>::copy(parameter6),
-        WTF::CrossThreadCopier<P7>::copy(parameter7));
+    return CrossThreadTask([callee = &callee, method,
+        p1 = CrossThreadCopier<P1>::copy(parameter1),
+        p2 = CrossThreadCopier<P2>::copy(parameter2),
+        p3 = CrossThreadCopier<P3>::copy(parameter3),
+        p4 = CrossThreadCopier<P4>::copy(parameter4),
+        p5 = CrossThreadCopier<P5>::copy(parameter5),
+        p6 = CrossThreadCopier<P6>::copy(parameter6),
+        p7 = CrossThreadCopier<P7>::copy(parameter7)]() mutable {
+        (callee->*method)(p1, p2, p3, p4, p5, p6, p7);
+    });
 }
 
 template<typename T, typename P1, typename MP1, typename P2, typename MP2, typename P3, typename MP3, typename P4, typename MP4, typename P5, typename MP5, typename P6, typename MP6, typename P7, typename MP7, typename P8, typename MP8>
@@ -234,17 +232,17 @@
     const P7& parameter7,
     const P8& parameter8)
 {
-    return CrossThreadTaskImpl<T, MP1, MP2, MP3, MP4, MP5, MP6, MP7, MP8>(
-        &callee,
-        method,
-        WTF::CrossThreadCopier<P1>::copy(parameter1),
-        WTF::CrossThreadCopier<P2>::copy(parameter2),
-        WTF::CrossThreadCopier<P3>::copy(parameter3),
-        WTF::CrossThreadCopier<P4>::copy(parameter4),
-        WTF::CrossThreadCopier<P5>::copy(parameter5),
-        WTF::CrossThreadCopier<P6>::copy(parameter6),
-        WTF::CrossThreadCopier<P7>::copy(parameter7),
-        WTF::CrossThreadCopier<P8>::copy(parameter8));
+    return CrossThreadTask([callee = &callee, method,
+        p1 = CrossThreadCopier<P1>::copy(parameter1),
+        p2 = CrossThreadCopier<P2>::copy(parameter2),
+        p3 = CrossThreadCopier<P3>::copy(parameter3),
+        p4 = CrossThreadCopier<P4>::copy(parameter4),
+        p5 = CrossThreadCopier<P5>::copy(parameter5),
+        p6 = CrossThreadCopier<P6>::copy(parameter6),
+        p7 = CrossThreadCopier<P7>::copy(parameter7),
+        p8 = CrossThreadCopier<P8>::copy(parameter8)]() mutable {
+        (callee->*method)(p1, p2, p3, p4, p5, p6, p7, p8);
+    });
 }
 
 } // namespace WTF

Modified: trunk/Tools/ChangeLog (201808 => 201809)


--- trunk/Tools/ChangeLog	2016-06-08 17:31:12 UTC (rev 201808)
+++ trunk/Tools/ChangeLog	2016-06-08 17:31:47 UTC (rev 201809)
@@ -1,3 +1,13 @@
+2016-06-08  Brady Eidson  <beid...@apple.com>
+
+        Make CrossThreadCopier more efficient (fewer copies!).
+        https://bugs.webkit.org/show_bug.cgi?id=158456
+
+        Reviewed by Alex Christensen.
+
+        * TestWebKitAPI/Tests/WTF/CrossThreadTask.cpp:
+        (TestWebKitAPI::TEST):
+
 2016-06-07  Filip Pizlo  <fpi...@apple.com>
 
         Add result validation to JSAir

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/CrossThreadTask.cpp (201808 => 201809)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/CrossThreadTask.cpp	2016-06-08 17:31:12 UTC (rev 201808)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/CrossThreadTask.cpp	2016-06-08 17:31:47 UTC (rev 201809)
@@ -102,7 +102,7 @@
         auto task = createCrossThreadTask(testFunction, logger1, logger2, logger3);
         task.performTask();
     }
-    ASSERT_STREQ("default_constructor(<default>-0-0) copy_constructor(<default>-1-0) name_constructor(logger-0-0) isolatedCopy() copy_constructor(<default>-1-0) isolatedCopy() copy_constructor(<default>-2-0) isolatedCopy() copy_constructor(logger-1-0) copy_constructor(<default>-2-0) copy_constructor(<default>-3-0) copy_constructor(logger-2-0) copy_constructor(<default>-3-0) copy_constructor(<default>-4-0) copy_constructor(logger-3-0) destructor(logger-2-0) destructor(<default>-3-0) destructor(<default>-2-0) destructor(logger-1-0) destructor(<default>-2-0) destructor(<default>-1-0) testFunction called destructor(logger-3-0) destructor(<default>-4-0) destructor(<default>-3-0) destructor(logger-0-0) destructor(<default>-1-0) destructor(<default>-0-0) ", takeLogStr().c_str());
+    ASSERT_STREQ("default_constructor(<default>-0-0) copy_constructor(<default>-1-0) name_constructor(logger-0-0) isolatedCopy() copy_constructor(<default>-1-0) isolatedCopy() copy_constructor(<default>-2-0) isolatedCopy() copy_constructor(logger-1-0) move_constructor(<default>-1-1) move_constructor(<default>-2-1) move_constructor(logger-1-1) destructor(logger-1-0) destructor(<default>-2-0) destructor(<default>-1-0) testFunction called destructor(logger-1-1) destructor(<default>-2-1) destructor(<default>-1-1) destructor(logger-0-0) destructor(<default>-1-0) destructor(<default>-0-0) ", takeLogStr().c_str());
 }
     
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to