Title: [274458] trunk/Source/WTF
Revision
274458
Author
cdu...@apple.com
Date
2021-03-15 18:57:01 -0700 (Mon, 15 Mar 2021)

Log Message

RunLoop::isMain() should not need to do any heap allocations
https://bugs.webkit.org/show_bug.cgi?id=223227

Reviewed by Darin Adler.

RunLoop::isMain() should not need to do any heap allocations. Before this change,
calling RunLoop::isMain() on a non-main thread would call RunLoop::current() which
would allocate the RunLoop for the current thread. This is inefficient and an issue
for WebAudio since we're not allowed to do heap allocation on the audio rendering
thread.

* wtf/RunLoop.cpp:
(WTF::RunLoop::runLoopHolder):
(WTF::RunLoop::current):
(WTF::RunLoop::isMain):
* wtf/RunLoop.h:

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (274457 => 274458)


--- trunk/Source/WTF/ChangeLog	2021-03-16 01:25:41 UTC (rev 274457)
+++ trunk/Source/WTF/ChangeLog	2021-03-16 01:57:01 UTC (rev 274458)
@@ -1,3 +1,22 @@
+2021-03-15  Chris Dumez  <cdu...@apple.com>
+
+        RunLoop::isMain() should not need to do any heap allocations
+        https://bugs.webkit.org/show_bug.cgi?id=223227
+
+        Reviewed by Darin Adler.
+
+        RunLoop::isMain() should not need to do any heap allocations. Before this change,
+        calling RunLoop::isMain() on a non-main thread would call RunLoop::current() which
+        would allocate the RunLoop for the current thread. This is inefficient and an issue
+        for WebAudio since we're not allowed to do heap allocation on the audio rendering
+        thread.
+
+        * wtf/RunLoop.cpp:
+        (WTF::RunLoop::runLoopHolder):
+        (WTF::RunLoop::current):
+        (WTF::RunLoop::isMain):
+        * wtf/RunLoop.h:
+
 2021-03-15  Alex Christensen  <achristen...@webkit.org>
 
         REGRESSION(r271642) Another app was relying on DOMWindow reuse

Modified: trunk/Source/WTF/wtf/RunLoop.cpp (274457 => 274458)


--- trunk/Source/WTF/wtf/RunLoop.cpp	2021-03-16 01:25:41 UTC (rev 274457)
+++ trunk/Source/WTF/wtf/RunLoop.cpp	2021-03-16 01:57:01 UTC (rev 274458)
@@ -28,7 +28,6 @@
 
 #include <wtf/NeverDestroyed.h>
 #include <wtf/StdLibExtras.h>
-#include <wtf/ThreadSpecific.h>
 
 namespace WTF {
 
@@ -63,7 +62,7 @@
     s_mainRunLoop = &RunLoop::current();
 }
 
-RunLoop& RunLoop::current()
+auto RunLoop::runLoopHolder() -> ThreadSpecific<Holder>&
 {
     static LazyNeverDestroyed<ThreadSpecific<Holder>> runLoopHolder;
     static std::once_flag onceKey;
@@ -70,9 +69,14 @@
     std::call_once(onceKey, [&] {
         runLoopHolder.construct();
     });
-    return runLoopHolder.get()->runLoop();
+    return runLoopHolder;
 }
 
+RunLoop& RunLoop::current()
+{
+    return runLoopHolder()->runLoop();
+}
+
 RunLoop& RunLoop::main()
 {
     ASSERT(s_mainRunLoop);
@@ -101,7 +105,8 @@
 bool RunLoop::isMain()
 {
     ASSERT(s_mainRunLoop);
-    return s_mainRunLoop == &RunLoop::current();
+    // Avoid constructing the RunLoop for the current thread if it has not been created yet.
+    return runLoopHolder().isSet() && s_mainRunLoop == &RunLoop::current();
 }
 
 void RunLoop::performWork()

Modified: trunk/Source/WTF/wtf/RunLoop.h (274457 => 274458)


--- trunk/Source/WTF/wtf/RunLoop.h	2021-03-16 01:25:41 UTC (rev 274457)
+++ trunk/Source/WTF/wtf/RunLoop.h	2021-03-16 01:57:01 UTC (rev 274458)
@@ -36,6 +36,7 @@
 #include <wtf/Observer.h>
 #include <wtf/RetainPtr.h>
 #include <wtf/Seconds.h>
+#include <wtf/ThreadSpecific.h>
 #include <wtf/ThreadingPrimitives.h>
 #include <wtf/WeakHashSet.h>
 #include <wtf/text/WTFString.h>
@@ -190,6 +191,7 @@
 
 private:
     class Holder;
+    static ThreadSpecific<Holder>& runLoopHolder();
 
     class DispatchTimer final : public TimerBase {
     public:
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to