Title: [295040] trunk/Source
Revision
295040
Author
[email protected]
Date
2022-05-30 21:13:54 -0700 (Mon, 30 May 2022)

Log Message

[JSC] Make VMEntryScope cheap
https://bugs.webkit.org/show_bug.cgi?id=241091

Reviewed by Mark Lam.

This patch makes VMEntryScope cheap. In some microbenchmarks, we observe repeated execution of VMEntryScope
because of many ticks driven by microtasks. And currently VMEntryScope is designed to be non-cheap based on
the assumption that this is not frequently executed.

1. We add isJSThread flag to Thread so that we can skip some of unnecessary initializations.
2. Appropriately set UNLIKELY / LIKELY for the major path.
3. Make DateCache::resetIfNecessary fast path inlined.

    ToT
    Time(doxbee-async-es2017-native): 24 ms.
    Time(doxbee-promises-es2015-native): 44.9 ms.
    Time(fibonacci-async-es2017-native): 197.6 ms.
    Time(parallel-async-es2017-native): 109.1 ms.
    Time(parallel-promises-es2015-native): 80.4 ms.

    Patched
    Time(doxbee-async-es2017-native): 21.4 ms.
    Time(doxbee-promises-es2015-native): 36.4 ms.
    Time(fibonacci-async-es2017-native): 168.1 ms.
    Time(parallel-async-es2017-native): 103.7 ms.
    Time(parallel-promises-es2015-native): 70.9 ms.

* Source/_javascript_Core/runtime/JSDateMath.cpp:
(JSC::DateCache::resetIfNecessarySlow):
(JSC::DateCache::resetIfNecessary): Deleted.
* Source/_javascript_Core/runtime/JSDateMath.h:
(JSC::DateCache::resetIfNecessary):
* Source/_javascript_Core/runtime/VM.h:
(JSC::VM::firePrimitiveGigacageEnabledIfNecessary):
* Source/_javascript_Core/runtime/VMEntryScope.cpp:
(JSC::VMEntryScope::VMEntryScope):
(JSC::VMEntryScope::~VMEntryScope):
* Source/WTF/wtf/Threading.cpp:
(WTF::Thread::registerJSThread):
* Source/WTF/wtf/Threading.h:

Canonical link: https://commits.webkit.org/251135@main

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/runtime/JSDateMath.cpp (295039 => 295040)


--- trunk/Source/_javascript_Core/runtime/JSDateMath.cpp	2022-05-31 01:26:39 UTC (rev 295039)
+++ trunk/Source/_javascript_Core/runtime/JSDateMath.cpp	2022-05-31 04:13:54 UTC (rev 295040)
@@ -100,7 +100,7 @@
 namespace JSC {
 
 #if PLATFORM(COCOA)
-static std::atomic<uint64_t> lastTimeZoneID { 1 };
+std::atomic<uint64_t> lastTimeZoneID { 1 };
 #endif
 
 #if HAVE(ICU_C_TIMEZONE_API)
@@ -470,14 +470,8 @@
 #endif
 }
 
-void DateCache::resetIfNecessary()
+void DateCache::resetIfNecessarySlow()
 {
-#if PLATFORM(COCOA)
-    if (m_cachedTimezoneID == lastTimeZoneID)
-        return;
-    m_cachedTimezoneID = lastTimeZoneID;
-#endif
-
     // FIXME: We should clear it only when we know the timezone has been changed on Non-Cocoa platforms.
     // https://bugs.webkit.org/show_bug.cgi?id=218365
     m_timeZoneCache.reset();

Modified: trunk/Source/_javascript_Core/runtime/JSDateMath.h (295039 => 295040)


--- trunk/Source/_javascript_Core/runtime/JSDateMath.h	2022-05-31 01:26:39 UTC (rev 295039)
+++ trunk/Source/_javascript_Core/runtime/JSDateMath.h	2022-05-31 04:13:54 UTC (rev 295040)
@@ -54,6 +54,10 @@
 
 static constexpr double minECMAScriptTime = -8.64E15;
 
+#if PLATFORM(COCOA)
+extern JS_EXPORT_PRIVATE std::atomic<uint64_t> lastTimeZoneID;
+#endif
+
 // We do not expose icu::TimeZone in this header file. And we cannot use icu::TimeZone forward declaration
 // because icu namespace can be an alias to icu$verNum namespace.
 struct OpaqueICUTimeZoneDeleter {
@@ -77,8 +81,18 @@
     DateCache();
     ~DateCache();
 
-    JS_EXPORT_PRIVATE void resetIfNecessary();
+    void resetIfNecessary()
+    {
+#if PLATFORM(COCOA)
+        if (LIKELY(m_cachedTimezoneID == lastTimeZoneID))
+            return;
+        m_cachedTimezoneID = lastTimeZoneID;
+#endif
+        resetIfNecessarySlow();
+    }
 
+    JS_EXPORT_PRIVATE void resetIfNecessarySlow();
+
     String defaultTimeZone();
     String timeZoneDisplayName(bool isDST);
     Ref<DateInstanceData> cachedDateInstanceData(double millisecondsFromEpoch);

Modified: trunk/Source/_javascript_Core/runtime/VM.h (295039 => 295040)


--- trunk/Source/_javascript_Core/runtime/VM.h	2022-05-31 01:26:39 UTC (rev 295039)
+++ trunk/Source/_javascript_Core/runtime/VM.h	2022-05-31 04:13:54 UTC (rev 295040)
@@ -662,7 +662,7 @@
     
     void firePrimitiveGigacageEnabledIfNecessary()
     {
-        if (m_needToFirePrimitiveGigacageEnabled) {
+        if (UNLIKELY(m_needToFirePrimitiveGigacageEnabled)) {
             m_needToFirePrimitiveGigacageEnabled = false;
             m_primitiveGigacageEnabled.fireAll(*this, "Primitive gigacage disabled asynchronously");
         }

Modified: trunk/Source/_javascript_Core/runtime/VMEntryScope.cpp (295039 => 295040)


--- trunk/Source/_javascript_Core/runtime/VMEntryScope.cpp	2022-05-31 01:26:39 UTC (rev 295039)
+++ trunk/Source/_javascript_Core/runtime/VMEntryScope.cpp	2022-05-31 04:13:54 UTC (rev 295040)
@@ -43,14 +43,19 @@
     if (!vm.entryScope) {
         vm.entryScope = this;
 
+        auto& thread = Thread::current();
+        if (UNLIKELY(!thread.isJSThread())) {
+            Thread::registerJSThread(thread);
+
 #if ENABLE(WEBASSEMBLY)
-        if (Wasm::isSupported())
-            Wasm::startTrackingCurrentThread();
+                if (Wasm::isSupported())
+                    Wasm::startTrackingCurrentThread();
 #endif
 
 #if HAVE(MACH_EXCEPTIONS)
-        registerThreadForMachExceptionHandling(Thread::current());
+                registerThreadForMachExceptionHandling(thread);
 #endif
+        }
 
         vm.firePrimitiveGigacageEnabledIfNecessary();
 
@@ -58,7 +63,7 @@
         // observe time zone changes.
         vm.resetDateCacheIfNecessary();
 
-        if (vm.watchdog())
+        if (UNLIKELY(vm.watchdog()))
             vm.watchdog()->enteredVM();
 
 #if ENABLE(SAMPLING_PROFILER)
@@ -87,10 +92,10 @@
 
     ASSERT_WITH_MESSAGE(!m_vm.hasCheckpointOSRSideState(), "Exitting the VM but pending checkpoint side state still available");
 
-    if (Options::useTracePoints())
+    if (UNLIKELY(Options::useTracePoints()))
         tracePoint(VMEntryScopeEnd);
     
-    if (m_vm.watchdog())
+    if (UNLIKELY(m_vm.watchdog()))
         m_vm.watchdog()->exitedVM();
 
     m_vm.entryScope = nullptr;

Modified: trunk/Source/WTF/wtf/Threading.cpp (295039 => 295040)


--- trunk/Source/WTF/wtf/Threading.cpp	2022-05-31 01:26:39 UTC (rev 295039)
+++ trunk/Source/WTF/wtf/Threading.cpp	2022-05-31 04:13:54 UTC (rev 295040)
@@ -373,6 +373,12 @@
     return Thread::current().gcThreadType() != GCThreadType::None;
 }
 
+void Thread::registerJSThread(Thread& thread)
+{
+    ASSERT(&thread == &Thread::current());
+    thread.m_isJSThread = true;
+}
+
 void Thread::setCurrentThreadIsUserInteractive(int relativePriority)
 {
 #if HAVE(QOS_CLASSES)

Modified: trunk/Source/WTF/wtf/Threading.h (295039 => 295040)


--- trunk/Source/WTF/wtf/Threading.h	2022-05-31 01:26:39 UTC (rev 295039)
+++ trunk/Source/WTF/wtf/Threading.h	2022-05-31 04:13:54 UTC (rev 295040)
@@ -217,6 +217,8 @@
     WTF_EXPORT_PRIVATE static void registerGCThread(GCThreadType);
     WTF_EXPORT_PRIVATE static bool mayBeGCThread();
 
+    WTF_EXPORT_PRIVATE static void registerJSThread(Thread&);
+
     WTF_EXPORT_PRIVATE void dump(PrintStream& out) const;
 
     static void initializePlatformThreading();
@@ -270,6 +272,7 @@
 #endif
 
     bool isCompilationThread() const { return m_isCompilationThread; }
+    bool isJSThread() const { return m_isJSThread; }
     GCThreadType gcThreadType() const { return static_cast<GCThreadType>(m_gcThreadType); }
 
     struct NewThreadContext;
@@ -357,6 +360,7 @@
     bool m_isDestroyedOnce : 1 { false };
     bool m_isCompilationThread: 1 { false };
     bool m_didUnregisterFromAllThreads : 1 { false };
+    bool m_isJSThread : 1 { false };
     unsigned m_gcThreadType : 2 { static_cast<unsigned>(GCThreadType::None) };
 
     // Lock & ParkingLot rely on ThreadSpecific. But Thread object can be destroyed even after ThreadSpecific things are destroyed.
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to