- Revision
- 265163
- Author
- [email protected]
- Date
- 2020-07-31 15:23:52 -0700 (Fri, 31 Jul 2020)
Log Message
Launch time regression due to EndowmentStateTracker initialization in WebPageProxy constructor
https://bugs.webkit.org/show_bug.cgi?id=215029
<rdar://problem/66362333>
Reviewed by Geoffrey Garen.
EndowmentStateTracker was expensive and in a critical code path during MobileSafari app launch. Since
this code is new, this was a net app launch time regression. To address the regression, the following
changes were made:
1. We only need the EndowmentStateTracker to monitor if the app is user-facing or not, so that we can
suspend / resume media playback as needed. As a result, it is unnecessary to start monitoring
user-facing state changes as soon as the WebPageProxy is constructed. Instead, we now wait until
media plays on the page for the first time.
2. It turns out that over 70% of CPU time in the EndowmentStateTracker constructor was spent getting
the current endownment for the process. As a result, I updated the code to lazy populate those
until they are requested for the first time.
3. The remaining 30% of CPU time in the EndowmentStateTracker constructor was spent constructing the
RBSProcessMonitor. I moved the initialization of the RBSProcessMonitor from the constructor to
EndowmentStateTracker::addClient() so that we only construct a monitor when we actually have our
first client.
4. All this code was actually only useful for Catalyst app and was therefore doing work on iOS for no
reason. I properly #ifdef'd out this code so that it now only applies to Catalyst apps.
* UIProcess/EndowmentStateTracker.h:
(WebKit::EndowmentStateTracker::isVisible const):
(WebKit::EndowmentStateTracker::isUserFacing const):
* UIProcess/EndowmentStateTracker.mm:
(WebKit::EndowmentStateTracker::stateFromEndowments):
(WebKit::EndowmentStateTracker::registerMonitorIfNecessary):
(WebKit::EndowmentStateTracker::addClient):
(WebKit::EndowmentStateTracker::ensureState const const):
(WebKit::EndowmentStateTracker::setState):
(WebKit::EndowmentStateTracker::EndowmentStateTracker): Deleted.
(WebKit::EndowmentStateTracker::setIsUserFacing): Deleted.
(WebKit::EndowmentStateTracker::setIsVisible): Deleted.
* UIProcess/WebPageProxy.cpp:
(WebKit::m_transcodingQueue):
(WebKit::WebPageProxy::updatePlayingMediaDidChange):
* UIProcess/WebPageProxy.h:
Modified Paths
Diff
Modified: trunk/Source/WebKit/ChangeLog (265162 => 265163)
--- trunk/Source/WebKit/ChangeLog 2020-07-31 22:23:06 UTC (rev 265162)
+++ trunk/Source/WebKit/ChangeLog 2020-07-31 22:23:52 UTC (rev 265163)
@@ -1,5 +1,47 @@
2020-07-31 Chris Dumez <[email protected]>
+ Launch time regression due to EndowmentStateTracker initialization in WebPageProxy constructor
+ https://bugs.webkit.org/show_bug.cgi?id=215029
+ <rdar://problem/66362333>
+
+ Reviewed by Geoffrey Garen.
+
+ EndowmentStateTracker was expensive and in a critical code path during MobileSafari app launch. Since
+ this code is new, this was a net app launch time regression. To address the regression, the following
+ changes were made:
+ 1. We only need the EndowmentStateTracker to monitor if the app is user-facing or not, so that we can
+ suspend / resume media playback as needed. As a result, it is unnecessary to start monitoring
+ user-facing state changes as soon as the WebPageProxy is constructed. Instead, we now wait until
+ media plays on the page for the first time.
+ 2. It turns out that over 70% of CPU time in the EndowmentStateTracker constructor was spent getting
+ the current endownment for the process. As a result, I updated the code to lazy populate those
+ until they are requested for the first time.
+ 3. The remaining 30% of CPU time in the EndowmentStateTracker constructor was spent constructing the
+ RBSProcessMonitor. I moved the initialization of the RBSProcessMonitor from the constructor to
+ EndowmentStateTracker::addClient() so that we only construct a monitor when we actually have our
+ first client.
+ 4. All this code was actually only useful for Catalyst app and was therefore doing work on iOS for no
+ reason. I properly #ifdef'd out this code so that it now only applies to Catalyst apps.
+
+ * UIProcess/EndowmentStateTracker.h:
+ (WebKit::EndowmentStateTracker::isVisible const):
+ (WebKit::EndowmentStateTracker::isUserFacing const):
+ * UIProcess/EndowmentStateTracker.mm:
+ (WebKit::EndowmentStateTracker::stateFromEndowments):
+ (WebKit::EndowmentStateTracker::registerMonitorIfNecessary):
+ (WebKit::EndowmentStateTracker::addClient):
+ (WebKit::EndowmentStateTracker::ensureState const const):
+ (WebKit::EndowmentStateTracker::setState):
+ (WebKit::EndowmentStateTracker::EndowmentStateTracker): Deleted.
+ (WebKit::EndowmentStateTracker::setIsUserFacing): Deleted.
+ (WebKit::EndowmentStateTracker::setIsVisible): Deleted.
+ * UIProcess/WebPageProxy.cpp:
+ (WebKit::m_transcodingQueue):
+ (WebKit::WebPageProxy::updatePlayingMediaDidChange):
+ * UIProcess/WebPageProxy.h:
+
+2020-07-31 Chris Dumez <[email protected]>
+
Drop ProcessAssertion::Client and replace with a simple invalidation handler
https://bugs.webkit.org/show_bug.cgi?id=214976
Modified: trunk/Source/WebKit/UIProcess/EndowmentStateTracker.h (265162 => 265163)
--- trunk/Source/WebKit/UIProcess/EndowmentStateTracker.h 2020-07-31 22:23:06 UTC (rev 265162)
+++ trunk/Source/WebKit/UIProcess/EndowmentStateTracker.h 2020-07-31 22:23:52 UTC (rev 265163)
@@ -30,6 +30,7 @@
#import <wtf/RetainPtr.h>
#import <wtf/WeakHashSet.h>
+OBJC_CLASS NSSet;
OBJC_CLASS RBSProcessMonitor;
namespace WebKit {
@@ -40,7 +41,6 @@
WTF_MAKE_FAST_ALLOCATED;
public:
static EndowmentStateTracker& singleton();
- ~EndowmentStateTracker();
class Client : public CanMakeWeakPtr<Client> {
public:
@@ -49,8 +49,8 @@
virtual void isVisibleChanged(bool) { }
};
- bool isVisible() const { return m_isVisible; }
- bool isUserFacing() const { return m_isUserFacing; }
+ bool isVisible() const { return ensureState().isVisible; }
+ bool isUserFacing() const { return ensureState().isUserFacing; }
void addClient(Client&);
void removeClient(Client&);
@@ -59,14 +59,21 @@
private:
friend class NeverDestroyed<EndowmentStateTracker>;
- EndowmentStateTracker();
- void setIsUserFacing(bool);
- void setIsVisible(bool);
+ EndowmentStateTracker() = default;
+ void registerMonitorIfNecessary();
+
+ struct State {
+ bool isUserFacing;
+ bool isVisible;
+ };
+ static State stateFromEndowments(NSSet *endowments);
+ const State& ensureState() const;
+ void setState(State&&);
+
WeakHashSet<Client> m_clients;
RetainPtr<RBSProcessMonitor> m_processMonitor;
- bool m_isUserFacing;
- bool m_isVisible;
+ mutable Optional<State> m_state;
};
}
Modified: trunk/Source/WebKit/UIProcess/EndowmentStateTracker.mm (265162 => 265163)
--- trunk/Source/WebKit/UIProcess/EndowmentStateTracker.mm 2020-07-31 22:23:06 UTC (rev 265162)
+++ trunk/Source/WebKit/UIProcess/EndowmentStateTracker.mm 2020-07-31 22:23:52 UTC (rev 265163)
@@ -72,6 +72,14 @@
return [state endowmentNamespaces];
}
+inline auto EndowmentStateTracker::stateFromEndowments(NSSet *endowments) -> State
+{
+ return State {
+ [endowments containsObject:userfacingEndowment],
+ [endowments containsObject:visibilityEndowment]
+ };
+}
+
bool EndowmentStateTracker::isApplicationForeground(pid_t pid)
{
return [endowmentsForHandle(handleForPID(pid)) containsObject:visibilityEndowment];
@@ -83,28 +91,21 @@
return tracker;
}
-EndowmentStateTracker::EndowmentStateTracker()
+void EndowmentStateTracker::registerMonitorIfNecessary()
{
- auto processHandle = [RBSProcessHandle currentProcess];
- auto endowmentNamespaces = endowmentsForHandle(processHandle);
+ if (m_processMonitor)
+ return;
- m_isUserFacing = [endowmentNamespaces containsObject:userfacingEndowment];
- m_isVisible = [endowmentNamespaces containsObject:visibilityEndowment];
+ m_processMonitor = [RBSProcessMonitor monitorWithConfiguration:[this] (id<RBSProcessMonitorConfiguring> config) {
+ [config setPredicates:@[[RBSProcessPredicate predicateMatchingHandle:[RBSProcessHandle currentProcess]]]];
- m_processMonitor = [RBSProcessMonitor monitorWithConfiguration:[this, processHandle = retainPtr(processHandle)] (id<RBSProcessMonitorConfiguring> config) {
-
- RBSProcessPredicate *processPredicate = [RBSProcessPredicate predicateMatchingHandle:processHandle.get()];
-
RBSProcessStateDescriptor *stateDescriptor = [RBSProcessStateDescriptor descriptor];
stateDescriptor.endowmentNamespaces = @[visibilityEndowment, userfacingEndowment];
-
- [config setPredicates:@[processPredicate]];
[config setStateDescriptor:stateDescriptor];
[config setUpdateHandler:[this] (RBSProcessMonitor * _Nonnull monitor, RBSProcessHandle * _Nonnull process, RBSProcessStateUpdate * _Nonnull update) mutable {
- dispatch_async(dispatch_get_main_queue(), [this, endowmentNamespaces = retainPtr(update.state.endowmentNamespaces)] {
- setIsUserFacing([endowmentNamespaces containsObject:userfacingEndowment]);
- setIsVisible([endowmentNamespaces containsObject:visibilityEndowment]);
+ dispatch_async(dispatch_get_main_queue(), [this, state = stateFromEndowments(update.state.endowmentNamespaces)]() mutable {
+ setState(WTFMove(state));
});
}];
}];
@@ -113,6 +114,7 @@
void EndowmentStateTracker::addClient(Client& client)
{
m_clients.add(client);
+ registerMonitorIfNecessary();
}
void EndowmentStateTracker::removeClient(Client& client)
@@ -120,31 +122,29 @@
m_clients.remove(client);
}
-void EndowmentStateTracker::setIsUserFacing(bool isUserFacing)
+auto EndowmentStateTracker::ensureState() const -> const State&
{
- if (m_isUserFacing == isUserFacing)
- return;
- m_isUserFacing = isUserFacing;
-
- RELEASE_LOG(ViewState, "%p - EndowmentStateTracker::setIsUserFacing(%{public}s)", this, isUserFacing ? "true" : "false");
-
- for (auto& client : copyToVector(m_clients)) {
- if (client)
- client->isUserFacingChanged(m_isUserFacing);
- }
+ if (!m_state)
+ m_state = stateFromEndowments(endowmentsForHandle([RBSProcessHandle currentProcess]));
+ return *m_state;
}
-void EndowmentStateTracker::setIsVisible(bool isVisible)
+void EndowmentStateTracker::setState(State&& state)
{
- if (m_isVisible == isVisible)
+ bool isUserFacingChanged = !m_state || m_state->isUserFacing != state.isUserFacing;
+ bool isVisibleChanged = !m_state || m_state->isVisible != state.isVisible;
+ if (!isUserFacingChanged && !isVisibleChanged)
return;
- m_isVisible = isVisible;
- RELEASE_LOG(ViewState, "%p - EndowmentStateTracker::setIsVisible(%{public}s)", this, isVisible ? "true" : "false");
+ m_state = WTFMove(state);
+ RELEASE_LOG(ViewState, "%p - EndowmentStateTracker::setState() isUserFacing: %{public}s isVisible: %{public}s", this, m_state->isUserFacing ? "true" : "false", m_state->isVisible ? "true" : "false");
+
for (auto& client : copyToVector(m_clients)) {
- if (client)
- client->isVisibleChanged(m_isVisible);
+ if (isUserFacingChanged && client)
+ client->isUserFacingChanged(m_state->isUserFacing);
+ if (isVisibleChanged && client)
+ client->isVisibleChanged(m_state->isVisible);
}
}
Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.cpp (265162 => 265163)
--- trunk/Source/WebKit/UIProcess/WebPageProxy.cpp 2020-07-31 22:23:06 UTC (rev 265162)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.cpp 2020-07-31 22:23:52 UTC (rev 265163)
@@ -532,8 +532,6 @@
if (m_configuration->preferences()->serviceWorkerEntitlementDisabledForTesting())
disableServiceWorkerEntitlementInNetworkProcess();
-
- EndowmentStateTracker::singleton().addClient(*this);
#endif
#if PLATFORM(COCOA)
@@ -576,7 +574,7 @@
webPageProxyCounter.decrement();
#endif
-#if PLATFORM(IOS_FAMILY)
+#if PLATFORM(MACCATALYST)
EndowmentStateTracker::singleton().removeClient(*this);
#endif
}
@@ -8938,6 +8936,16 @@
if (newState == m_mediaState)
return;
+#if PLATFORM(MACCATALYST)
+ // When the page starts playing media for the first time, make sure we register with
+ // the EndowmentStateTracker to get notifications when the application is no longer
+ // user-facing, so that we can appropriately suspend all media playback.
+ if (!m_isListeningForUserFacingStateChangeNotification) {
+ EndowmentStateTracker::singleton().addClient(*this);
+ m_isListeningForUserFacingStateChangeNotification = true;
+ }
+#endif
+
#if ENABLE(MEDIA_STREAM)
WebCore::MediaProducer::MediaStateFlags oldMediaCaptureState = m_mediaState & WebCore::MediaProducer::MediaCaptureMask;
WebCore::MediaProducer::MediaStateFlags newMediaCaptureState = newState & WebCore::MediaProducer::MediaCaptureMask;
Modified: trunk/Source/WebKit/UIProcess/WebPageProxy.h (265162 => 265163)
--- trunk/Source/WebKit/UIProcess/WebPageProxy.h 2020-07-31 22:23:06 UTC (rev 265162)
+++ trunk/Source/WebKit/UIProcess/WebPageProxy.h 2020-07-31 22:23:52 UTC (rev 265163)
@@ -121,11 +121,14 @@
#include <wtf/text/WTFString.h>
#if PLATFORM(IOS_FAMILY)
-#include "EndowmentStateTracker.h"
#include "GestureTypes.h"
#include "WebAutocorrectionContext.h"
#endif
+#if PLATFORM(MACCATALYST)
+#include "EndowmentStateTracker.h"
+#endif
+
OBJC_CLASS NSView;
OBJC_CLASS _WKRemoteObjectRegistry;
@@ -434,7 +437,7 @@
, public WebCore::PlatformSpeechSynthesisUtteranceClient
, public WebCore::PlatformSpeechSynthesizerClient
#endif
-#if PLATFORM(IOS_FAMILY)
+#if PLATFORM(MACCATALYST)
, public EndowmentStateTracker::Client
#else
, public CanMakeWeakPtr<WebPageProxy>
@@ -1188,8 +1191,9 @@
#if PLATFORM(IOS_FAMILY)
void processWillBecomeSuspended();
void processWillBecomeForeground();
+#endif
+#if PLATFORM(MACCATALYST)
void isUserFacingChanged(bool) final;
- void isVisibleChanged(bool) final;
#endif
#if HAVE(VISIBILITY_PROPAGATION_VIEW)
@@ -2472,6 +2476,9 @@
OptionSet<WebCore::ActivityState::Flag> m_activityState;
bool m_viewWasEverInWindow { false };
+#if PLATFORM(MACCATALYST)
+ bool m_isListeningForUserFacingStateChangeNotification { false };
+#endif
#if PLATFORM(IOS_FAMILY)
bool m_allowsMediaDocumentInlinePlayback { false };
std::unique_ptr<ProcessThrottler::ForegroundActivity> m_isVisibleActivity;
Modified: trunk/Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm (265162 => 265163)
--- trunk/Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm 2020-07-31 22:23:06 UTC (rev 265162)
+++ trunk/Source/WebKit/UIProcess/ios/WebPageProxyIOS.mm 2020-07-31 22:23:52 UTC (rev 265163)
@@ -1624,23 +1624,16 @@
}
}
+#if PLATFORM(MACCATALYST)
void WebPageProxy::isUserFacingChanged(bool isUserFacing)
{
-#if PLATFORM(MACCATALYST)
if (!isUserFacing)
suspendAllMediaPlayback();
else
resumeAllMediaPlayback();
-#else
- UNUSED_PARAM(isUserFacing);
+}
#endif
-}
-void WebPageProxy::isVisibleChanged(bool isVisible)
-{
- UNUSED_PARAM(isVisible);
-}
-
#if PLATFORM(IOS)
void WebPageProxy::grantAccessToAssetServices()
{