[iOS] Maintain the last Back/Forward cache entry when the application gets suspended
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 16 Oct 2019 01:49:53 +0000 (01:49 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 16 Oct 2019 01:49:53 +0000 (01:49 +0000)
https://bugs.webkit.org/show_bug.cgi?id=203014

Reviewed by Geoffrey Garen.

Previously, we would clear all back/forward cache entries when the application is about to
be suspended. This means that we would lose fast-back when coming back to the application.
To be memory-friendly but maintain the fast-back when coming back to the application, we now
maintain the last back/forward cache entry when the application gets suspended.

* UIProcess/Cocoa/WebProcessPoolCocoa.mm:
(WebKit::WebProcessPool::applicationIsAboutToSuspend):
(WebKit::WebProcessPool::notifyProcessPoolsApplicationIsAboutToSuspend):
* UIProcess/WebBackForwardCache.cpp:
(WebKit::WebBackForwardCache::pruneToSize):
* UIProcess/WebBackForwardCache.h:
* UIProcess/WebProcessPool.h:
* UIProcess/ios/ProcessAssertionIOS.mm:
(-[WKProcessAssertionBackgroundTaskManager init]):
(-[WKProcessAssertionBackgroundTaskManager _releaseBackgroundTask]):

* WebProcess/WebProcess.cpp:
(WebKit::WebProcess::initializeWebProcess):
A WebProcess no longer clears its PageCache on suspension on iOS. We let the UIProcess's
back/forward cache control when PageCache entries should get destroyed. The back/forward
cache will properly wake up a suspended process to clear one of its PageCache entries if
needed.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@251174 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit/ChangeLog
Source/WebKit/UIProcess/Cocoa/WebProcessPoolCocoa.mm
Source/WebKit/UIProcess/WebBackForwardCache.cpp
Source/WebKit/UIProcess/WebBackForwardCache.h
Source/WebKit/UIProcess/WebProcessPool.h
Source/WebKit/UIProcess/ios/ProcessAssertionIOS.mm
Source/WebKit/WebProcess/WebProcess.cpp

index 0172a14..d87419c 100644 (file)
@@ -1,3 +1,33 @@
+2019-10-15  Chris Dumez  <cdumez@apple.com>
+
+        [iOS] Maintain the last Back/Forward cache entry when the application gets suspended
+        https://bugs.webkit.org/show_bug.cgi?id=203014
+
+        Reviewed by Geoffrey Garen.
+
+        Previously, we would clear all back/forward cache entries when the application is about to
+        be suspended. This means that we would lose fast-back when coming back to the application.
+        To be memory-friendly but maintain the fast-back when coming back to the application, we now
+        maintain the last back/forward cache entry when the application gets suspended.
+
+        * UIProcess/Cocoa/WebProcessPoolCocoa.mm:
+        (WebKit::WebProcessPool::applicationIsAboutToSuspend):
+        (WebKit::WebProcessPool::notifyProcessPoolsApplicationIsAboutToSuspend):
+        * UIProcess/WebBackForwardCache.cpp:
+        (WebKit::WebBackForwardCache::pruneToSize):
+        * UIProcess/WebBackForwardCache.h:
+        * UIProcess/WebProcessPool.h:
+        * UIProcess/ios/ProcessAssertionIOS.mm:
+        (-[WKProcessAssertionBackgroundTaskManager init]):
+        (-[WKProcessAssertionBackgroundTaskManager _releaseBackgroundTask]):
+
+        * WebProcess/WebProcess.cpp:
+        (WebKit::WebProcess::initializeWebProcess):
+        A WebProcess no longer clears its PageCache on suspension on iOS. We let the UIProcess's
+        back/forward cache control when PageCache entries should get destroyed. The back/forward
+        cache will properly wake up a suspended process to clear one of its PageCache entries if
+        needed.
+
 2019-10-15  Andres Gonzalez  <andresg_22@apple.com>
 
         AX: Make AXIsolatedTree compile again
index 4b92b93..1a41cd1 100644 (file)
@@ -37,6 +37,7 @@
 #import "TextChecker.h"
 #import "VersionChecks.h"
 #import "WKBrowsingContextControllerInternal.h"
+#import "WebBackForwardCache.h"
 #import "WebMemoryPressureHandler.h"
 #import "WebPageGroup.h"
 #import "WebPreferencesKeys.h"
@@ -585,11 +586,18 @@ int webProcessThroughputQOS()
 #if PLATFORM(IOS_FAMILY)
 void WebProcessPool::applicationIsAboutToSuspend()
 {
-    RELEASE_LOG(ProcessSuspension, "Application is about to suspend so we simulate memory pressure to terminate non-critical processes");
-    // Simulate memory pressure handling so free as much memory as possible before suspending.
-    // In particular, this will terminate prewarmed and PageCache processes.
+    RELEASE_LOG(ProcessSuspension, "WebProcessPool::applicationIsAboutToSuspend() Terminating non-critical processes");
+
+    m_backForwardCache->pruneToSize(1);
+    if (m_prewarmedProcess)
+        m_prewarmedProcess->shutDown();
+    m_webProcessCache->clear();
+}
+
+void WebProcessPool::notifyProcessPoolsApplicationIsAboutToSuspend()
+{
     for (auto* processPool : allProcessPools())
-        processPool->handleMemoryPressureWarning(Critical::Yes);
+        processPool->applicationIsAboutToSuspend();
 }
 #endif
 
index e75a896..698504d 100644 (file)
@@ -164,4 +164,11 @@ void WebBackForwardCache::clear()
         item->setBackForwardCacheEntry(nullptr);
 }
 
+void WebBackForwardCache::pruneToSize(unsigned newSize)
+{
+    RELEASE_LOG(BackForwardCache, "WebBackForwardCache::pruneToSize(%u)", newSize);
+    while (size() > newSize)
+        removeOldestEntry();
+}
+
 } // namespace WebKit.
index c1e115a..be4bc4e 100644 (file)
@@ -50,6 +50,7 @@ public:
     unsigned size() const { return m_itemsWithCachedPage.size(); }
 
     void clear();
+    void pruneToSize(unsigned);
     void removeEntriesForProcess(WebProcessProxy&);
     void removeEntriesForPage(WebPageProxy&);
     void removeEntriesForSession(PAL::SessionID);
index 78bdc04..c6ba0fc 100644 (file)
@@ -230,7 +230,8 @@ public:
     void populateVisitedLinks();
 
 #if PLATFORM(IOS_FAMILY)
-    static void applicationIsAboutToSuspend();
+    void applicationIsAboutToSuspend();
+    static void notifyProcessPoolsApplicationIsAboutToSuspend();
 #endif
 
     void handleMemoryPressureWarning(Critical);
index 32fcf36..37a6982 100644 (file)
@@ -84,7 +84,7 @@ static const Seconds releaseBackgroundTaskAfterExpirationDelay { 2_s };
         _applicationIsBackgrounded = YES;
         
         if (_backgroundTask == UIBackgroundTaskInvalid)
-            WebKit::WebProcessPool::applicationIsAboutToSuspend();
+            WebKit::WebProcessPool::notifyProcessPoolsApplicationIsAboutToSuspend();
     }];
 
     return self;
@@ -188,7 +188,7 @@ static const Seconds releaseBackgroundTaskAfterExpirationDelay { 2_s };
 
     RELEASE_LOG(ProcessSuspension, "%p - WKProcessAssertionBackgroundTaskManager - endBackgroundTask", self);
     if (_applicationIsBackgrounded)
-        WebKit::WebProcessPool::applicationIsAboutToSuspend();
+        WebKit::WebProcessPool::notifyProcessPoolsApplicationIsAboutToSuspend();
     [[UIApplication sharedApplication] endBackgroundTask:_backgroundTask];
     _backgroundTask = UIBackgroundTaskInvalid;
 }
index accd337..c66c805 100644 (file)
@@ -306,7 +306,7 @@ void WebProcess::initializeWebProcess(WebProcessCreationParameters&& parameters)
             }
 #endif
 
-            auto maintainPageCache = m_isSuspending && hasPageRequiringPageCacheWhileSuspended() ? WebCore::MaintainPageCache::Yes : WebCore::MaintainPageCache::No;
+            auto maintainPageCache = m_isSuspending ? WebCore::MaintainPageCache::Yes : WebCore::MaintainPageCache::No;
             auto maintainMemoryCache = m_isSuspending && m_hasSuspendedPageProxy ? WebCore::MaintainMemoryCache::Yes : WebCore::MaintainMemoryCache::No;
             WebCore::releaseMemory(critical, synchronous, maintainPageCache, maintainMemoryCache);
         });