- Revision
- 290477
- Author
- cdu...@apple.com
- Date
- 2022-02-24 16:53:47 -0800 (Thu, 24 Feb 2022)
Log Message
Take down shared worker context connection less aggressively when it becomes idle
https://bugs.webkit.org/show_bug.cgi?id=237156
Reviewed by Darin Adler.
Take down shared worker context connection less aggressively when it becomes idle. Give it 5
seconds before shutting it down in case it ends up being used again in the near future.
This is similar to what we do for service workers.
* NetworkProcess/SharedWorker/WebSharedWorkerServer.cpp:
(WebKit::WebSharedWorkerServer::shutDownSharedWorker):
* NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.cpp:
(WebKit::WebSharedWorkerServerToContextConnection::WebSharedWorkerServerToContextConnection):
(WebKit::WebSharedWorkerServerToContextConnection::addSharedWorkerObject):
(WebKit::WebSharedWorkerServerToContextConnection::removeSharedWorkerObject):
(WebKit::WebSharedWorkerServerToContextConnection::idleTerminationTimerFired):
* NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.h:
Modified Paths
Diff
Modified: trunk/Source/WebKit/ChangeLog (290476 => 290477)
--- trunk/Source/WebKit/ChangeLog 2022-02-25 00:53:36 UTC (rev 290476)
+++ trunk/Source/WebKit/ChangeLog 2022-02-25 00:53:47 UTC (rev 290477)
@@ -1,3 +1,24 @@
+2022-02-24 Chris Dumez <cdu...@apple.com>
+
+ Take down shared worker context connection less aggressively when it becomes idle
+ https://bugs.webkit.org/show_bug.cgi?id=237156
+
+ Reviewed by Darin Adler.
+
+ Take down shared worker context connection less aggressively when it becomes idle. Give it 5
+ seconds before shutting it down in case it ends up being used again in the near future.
+
+ This is similar to what we do for service workers.
+
+ * NetworkProcess/SharedWorker/WebSharedWorkerServer.cpp:
+ (WebKit::WebSharedWorkerServer::shutDownSharedWorker):
+ * NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.cpp:
+ (WebKit::WebSharedWorkerServerToContextConnection::WebSharedWorkerServerToContextConnection):
+ (WebKit::WebSharedWorkerServerToContextConnection::addSharedWorkerObject):
+ (WebKit::WebSharedWorkerServerToContextConnection::removeSharedWorkerObject):
+ (WebKit::WebSharedWorkerServerToContextConnection::idleTerminationTimerFired):
+ * NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.h:
+
2022-02-24 Jer Noble <jer.no...@apple.com>
[Refactor] Adopt LoggerHelper in Legacy EME classes
Modified: trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServer.cpp (290476 => 290477)
--- trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServer.cpp 2022-02-25 00:53:36 UTC (rev 290476)
+++ trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServer.cpp 2022-02-25 00:53:47 UTC (rev 290477)
@@ -212,14 +212,8 @@
if (!sharedWorker)
return;
- auto* contextConnection = sharedWorker->contextConnection();
- if (!contextConnection)
- return;
-
- contextConnection->terminateSharedWorker(*sharedWorker);
-
- if (contextConnection->sharedWorkerObjects().isEmpty())
- contextConnection->connectionIsNoLongerNeeded();
+ if (auto* contextConnection = sharedWorker->contextConnection())
+ contextConnection->terminateSharedWorker(*sharedWorker);
}
void WebSharedWorkerServer::addConnection(std::unique_ptr<WebSharedWorkerServerConnection>&& connection)
Modified: trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.cpp (290476 => 290477)
--- trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.cpp 2022-02-25 00:53:36 UTC (rev 290476)
+++ trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.cpp 2022-02-25 00:53:47 UTC (rev 290477)
@@ -35,15 +35,21 @@
#include "WebSharedWorker.h"
#include "WebSharedWorkerContextManagerConnectionMessages.h"
#include "WebSharedWorkerServer.h"
+#include <wtf/MemoryPressureHandler.h>
namespace WebKit {
#define CONTEXT_CONNECTION_RELEASE_LOG(fmt, ...) RELEASE_LOG(SharedWorker, "%p - [webProcessIdentifier=%" PRIu64 "] WebSharedWorkerServerToContextConnection::" fmt, this, webProcessIdentifier().toUInt64(), ##__VA_ARGS__)
+// We terminate the context connection after 5 seconds if it is no longer used by any SharedWorker objects,
+// as a performance optimization.
+constexpr Seconds idleTerminationDelay { 5_s };
+
WebSharedWorkerServerToContextConnection::WebSharedWorkerServerToContextConnection(NetworkConnectionToWebProcess& connection, const WebCore::RegistrableDomain& registrableDomain, WebSharedWorkerServer& server)
: m_connection(connection)
, m_server(server)
, m_registrableDomain(registrableDomain)
+ , m_idleTerminationTimer(*this, &WebSharedWorkerServerToContextConnection::idleTerminationTimerFired)
{
CONTEXT_CONNECTION_RELEASE_LOG("WebSharedWorkerServerToContextConnection:");
server.addContextConnection(*this);
@@ -113,16 +119,22 @@
void WebSharedWorkerServerToContextConnection::addSharedWorkerObject(WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier)
{
+ CONTEXT_CONNECTION_RELEASE_LOG("addSharedWorkerObject: sharedWorkerObjectIdentifier=%{public}s", sharedWorkerObjectIdentifier.toString().utf8().data());
auto& sharedWorkerObjects = m_sharedWorkerObjects.ensure(sharedWorkerObjectIdentifier.processIdentifier(), [] { return HashSet<WebCore::SharedWorkerObjectIdentifier> { }; }).iterator->value;
+ ASSERT(!sharedWorkerObjects.contains(sharedWorkerObjectIdentifier));
sharedWorkerObjects.add(sharedWorkerObjectIdentifier);
if (webProcessIdentifier() != sharedWorkerObjectIdentifier.processIdentifier() && sharedWorkerObjects.size() == 1)
m_connection.networkProcess().send(Messages::NetworkProcessProxy::RegisterRemoteWorkerClientProcess { RemoteWorkerType::SharedWorker, sharedWorkerObjectIdentifier.processIdentifier(), webProcessIdentifier() }, 0);
+
+ m_idleTerminationTimer.stop();
}
void WebSharedWorkerServerToContextConnection::removeSharedWorkerObject(WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier)
{
+ CONTEXT_CONNECTION_RELEASE_LOG("removeSharedWorkerObject: sharedWorkerObjectIdentifier=%{public}s", sharedWorkerObjectIdentifier.toString().utf8().data());
auto it = m_sharedWorkerObjects.find(sharedWorkerObjectIdentifier.processIdentifier());
+ ASSERT(it != m_sharedWorkerObjects.end());
if (it == m_sharedWorkerObjects.end())
return;
it->value.remove(sharedWorkerObjectIdentifier);
@@ -132,8 +144,20 @@
m_sharedWorkerObjects.remove(it);
if (webProcessIdentifier() != sharedWorkerObjectIdentifier.processIdentifier())
m_connection.networkProcess().send(Messages::NetworkProcessProxy::UnregisterRemoteWorkerClientProcess { RemoteWorkerType::SharedWorker, sharedWorkerObjectIdentifier.processIdentifier(), webProcessIdentifier() }, 0);
+
+ if (m_sharedWorkerObjects.isEmpty()) {
+ CONTEXT_CONNECTION_RELEASE_LOG("removeSharedWorkerObject: connection is now idle, starting a timer to terminate it");
+ ASSERT(!m_idleTerminationTimer.isActive());
+ m_idleTerminationTimer.startOneShot(MemoryPressureHandler::singleton().isUnderMemoryPressure() ? 0_s : idleTerminationDelay);
+ }
}
+void WebSharedWorkerServerToContextConnection::idleTerminationTimerFired()
+{
+ RELEASE_ASSERT(m_sharedWorkerObjects.isEmpty());
+ connectionIsNoLongerNeeded();
+}
+
#undef CONTEXT_CONNECTION_RELEASE_LOG
} // namespace WebKit
Modified: trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.h (290476 => 290477)
--- trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.h 2022-02-25 00:53:36 UTC (rev 290476)
+++ trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.h 2022-02-25 00:53:47 UTC (rev 290477)
@@ -31,6 +31,7 @@
#include <WebCore/RegistrableDomain.h>
#include <WebCore/SharedWorkerIdentifier.h>
#include <WebCore/SharedWorkerObjectIdentifier.h>
+#include <WebCore/Timer.h>
#include <WebCore/TransferredMessagePort.h>
namespace WebCore {
@@ -61,7 +62,6 @@
void postConnectEvent(const WebSharedWorker&, const WebCore::TransferredMessagePort&);
void terminateSharedWorker(const WebSharedWorker&);
- void connectionIsNoLongerNeeded();
const HashMap<WebCore::ProcessIdentifier, HashSet<WebCore::SharedWorkerObjectIdentifier>>& sharedWorkerObjects() const { return m_sharedWorkerObjects; }
void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
@@ -70,6 +70,9 @@
void removeSharedWorkerObject(WebCore::SharedWorkerObjectIdentifier);
private:
+ void idleTerminationTimerFired();
+ void connectionIsNoLongerNeeded();
+
// IPC messages.
void postExceptionToWorkerObject(WebCore::SharedWorkerIdentifier, const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL);
@@ -81,6 +84,7 @@
WeakPtr<WebSharedWorkerServer> m_server;
WebCore::RegistrableDomain m_registrableDomain;
HashMap<WebCore::ProcessIdentifier, HashSet<WebCore::SharedWorkerObjectIdentifier>> m_sharedWorkerObjects;
+ WebCore::Timer m_idleTerminationTimer;
};
} // namespace WebKit