Title: [292483] trunk/Source/WebKit
Revision
292483
Author
[email protected]
Date
2022-04-06 11:29:39 -0700 (Wed, 06 Apr 2022)

Log Message

Improve API::SerializedScriptValue::deserialize to not allocate a new JSContext every second
https://bugs.webkit.org/show_bug.cgi?id=210920

Reviewed by Geoffrey Garen.

Instead of forcing the lifetime of the shared JSContext to 1 second, we now use a hysteresis
logic so that it can stay alive as long as it is used at least every 10 seconds.

* UIProcess/API/Cocoa/APISerializedScriptValueCocoa.mm:
(API::SharedJSContext::SharedJSContext):
(API::SharedJSContext::ensureContext):
(API::SharedJSContext::releaseContextIfNecessary):
(API::SharedJSContext::releaseContext): Deleted.

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (292482 => 292483)


--- trunk/Source/WebKit/ChangeLog	2022-04-06 18:22:11 UTC (rev 292482)
+++ trunk/Source/WebKit/ChangeLog	2022-04-06 18:29:39 UTC (rev 292483)
@@ -1,5 +1,21 @@
 2022-04-06  Chris Dumez  <[email protected]>
 
+        Improve API::SerializedScriptValue::deserialize to not allocate a new JSContext every second
+        https://bugs.webkit.org/show_bug.cgi?id=210920
+
+        Reviewed by Geoffrey Garen.
+
+        Instead of forcing the lifetime of the shared JSContext to 1 second, we now use a hysteresis
+        logic so that it can stay alive as long as it is used at least every 10 seconds.
+
+        * UIProcess/API/Cocoa/APISerializedScriptValueCocoa.mm:
+        (API::SharedJSContext::SharedJSContext):
+        (API::SharedJSContext::ensureContext):
+        (API::SharedJSContext::releaseContextIfNecessary):
+        (API::SharedJSContext::releaseContext): Deleted.
+
+2022-04-06  Chris Dumez  <[email protected]>
+
         Reduce number of conversions from StringView to String
         https://bugs.webkit.org/show_bug.cgi?id=238841
 

Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/APISerializedScriptValueCocoa.mm (292482 => 292483)


--- trunk/Source/WebKit/UIProcess/API/Cocoa/APISerializedScriptValueCocoa.mm	2022-04-06 18:22:11 UTC (rev 292482)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/APISerializedScriptValueCocoa.mm	2022-04-06 18:29:39 UTC (rev 292483)
@@ -36,15 +36,18 @@
 
 namespace API {
 
+static constexpr auto sharedJSContextMaxIdleTime = 10_s;
+
 class SharedJSContext {
 public:
     SharedJSContext()
-        : m_timer(RunLoop::main(), this, &SharedJSContext::releaseContext)
+        : m_timer(RunLoop::main(), this, &SharedJSContext::releaseContextIfNecessary)
     {
     }
 
     JSContext* ensureContext()
     {
+        m_lastUseTime = MonotonicTime::now();
         if (!m_context) {
             bool previous = JSRemoteInspectorGetInspectionEnabledByDefault();
             JSRemoteInspectorSetInspectionEnabledByDefault(false);
@@ -51,13 +54,20 @@
             m_context = adoptNS([[JSContext alloc] init]);
             JSRemoteInspectorSetInspectionEnabledByDefault(previous);
 
-            m_timer.startOneShot(1_s);
+            m_timer.startOneShot(sharedJSContextMaxIdleTime);
         }
         return m_context.get();
     }
 
-    void releaseContext()
+    void releaseContextIfNecessary()
     {
+        auto idleTime = MonotonicTime::now() - m_lastUseTime;
+        if (idleTime < sharedJSContextMaxIdleTime) {
+            // We lazily restart the timer if needed every 10 seconds instead of doing so every time ensureContext()
+            // is called, for performance reasons.
+            m_timer.startOneShot(sharedJSContextMaxIdleTime - idleTime);
+            return;
+        }
         m_context.clear();
     }
 
@@ -64,6 +74,7 @@
 private:
     RetainPtr<JSContext> m_context;
     RunLoop::Timer<SharedJSContext> m_timer;
+    MonotonicTime m_lastUseTime;
 };
 
 static SharedJSContext& sharedContext()
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to