Title: [290060] branches/safari-613.1.17.1-branch/Source
Revision
290060
Author
[email protected]
Date
2022-02-17 13:01:12 -0800 (Thu, 17 Feb 2022)

Log Message

Cherry-pick r290005. rdar://problem/87596724

    CrashTracer: com.apple.WebKit.WebContent at _javascript_Core: bmalloc_allocate_impl_impl_slow
    https://bugs.webkit.org/show_bug.cgi?id=236695
    rdar://87596724

    Reviewed by Jer Noble.

    Source/WebCore:

    When a new FragmentedSharedBuffer is received from the network process, it is proactively coalesced
    into a SharedBuffer during each call to CachedResource::updateBuffer(). This causes a large number
    of re-allocations and copies; essentially re-allocating each time the resource's buffer receives
    one more chunk of data.
    Instead we only flatten the data buffer once all data have been received.

    Running speedtest.net with a gigabit link, we see a reduction of peak
    memory use in the content process by about 40% (from 740MB to under 440MB of
    physical memory, 1.7GB of memory allocation vs 535MB)

    Covered by existing tests.

    * editing/ios/EditorIOS.mm:
    (WebCore::Editor::writeImageToPasteboard):
    * editing/mac/EditorMac.mm:
    (WebCore::Editor::writeImageToPasteboard):
    * loader/DocumentThreadableLoader.cpp:
    (WebCore::DocumentThreadableLoader::didFinishLoading):
    * loader/cache/CachedApplicationManifest.cpp:
    (WebCore::CachedApplicationManifest::finishLoading):
    * loader/cache/CachedCSSStyleSheet.cpp:
    (WebCore::CachedCSSStyleSheet::finishLoading):
    * loader/cache/CachedFont.cpp:
    (WebCore::CachedFont::ensureCustomFontData):
    * loader/cache/CachedImage.cpp:
    (WebCore::CachedImage::updateImageData):
    * loader/cache/CachedRawResource.cpp:
    (WebCore::CachedRawResource::calculateIncrementalDataChunk const):
    (WebCore::CachedRawResource::updateBuffer):
    (WebCore::CachedRawResource::finishLoading):
    * loader/cache/CachedRawResource.h:
    * loader/cache/CachedResource.h:
    (WebCore::CachedResource::resourceBuffer const):
    * loader/cache/CachedScript.cpp:
    (WebCore::CachedScript::script):
    * loader/cache/CachedXSLStyleSheet.cpp:
    (WebCore::CachedXSLStyleSheet::finishLoading):

    Source/WebKit:

    * WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp:
    (WebKit::WebFrameLoaderClient::finishedLoading): Fly-by fix, the
    IPC::DataReference would reference a SharedBuffer going
    out of scope before it was sent over IPC.

    Source/WebKitLegacy/mac:

    * WebView/WebHTMLView.mm:
    (-[WebHTMLView namesOfPromisedFilesDroppedAtDestination:]):

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

Modified Paths

Diff

Modified: branches/safari-613.1.17.1-branch/Source/WebCore/ChangeLog (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebCore/ChangeLog	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebCore/ChangeLog	2022-02-17 21:01:12 UTC (rev 290060)
@@ -1,5 +1,116 @@
 2022-02-17  Russell Epstein  <[email protected]>
 
+        Cherry-pick r290005. rdar://problem/87596724
+
+    CrashTracer: com.apple.WebKit.WebContent at _javascript_Core: bmalloc_allocate_impl_impl_slow
+    https://bugs.webkit.org/show_bug.cgi?id=236695
+    rdar://87596724
+    
+    Reviewed by Jer Noble.
+    
+    Source/WebCore:
+    
+    When a new FragmentedSharedBuffer is received from the network process, it is proactively coalesced
+    into a SharedBuffer during each call to CachedResource::updateBuffer(). This causes a large number
+    of re-allocations and copies; essentially re-allocating each time the resource's buffer receives
+    one more chunk of data.
+    Instead we only flatten the data buffer once all data have been received.
+    
+    Running speedtest.net with a gigabit link, we see a reduction of peak
+    memory use in the content process by about 40% (from 740MB to under 440MB of
+    physical memory, 1.7GB of memory allocation vs 535MB)
+    
+    Covered by existing tests.
+    
+    * editing/ios/EditorIOS.mm:
+    (WebCore::Editor::writeImageToPasteboard):
+    * editing/mac/EditorMac.mm:
+    (WebCore::Editor::writeImageToPasteboard):
+    * loader/DocumentThreadableLoader.cpp:
+    (WebCore::DocumentThreadableLoader::didFinishLoading):
+    * loader/cache/CachedApplicationManifest.cpp:
+    (WebCore::CachedApplicationManifest::finishLoading):
+    * loader/cache/CachedCSSStyleSheet.cpp:
+    (WebCore::CachedCSSStyleSheet::finishLoading):
+    * loader/cache/CachedFont.cpp:
+    (WebCore::CachedFont::ensureCustomFontData):
+    * loader/cache/CachedImage.cpp:
+    (WebCore::CachedImage::updateImageData):
+    * loader/cache/CachedRawResource.cpp:
+    (WebCore::CachedRawResource::calculateIncrementalDataChunk const):
+    (WebCore::CachedRawResource::updateBuffer):
+    (WebCore::CachedRawResource::finishLoading):
+    * loader/cache/CachedRawResource.h:
+    * loader/cache/CachedResource.h:
+    (WebCore::CachedResource::resourceBuffer const):
+    * loader/cache/CachedScript.cpp:
+    (WebCore::CachedScript::script):
+    * loader/cache/CachedXSLStyleSheet.cpp:
+    (WebCore::CachedXSLStyleSheet::finishLoading):
+    
+    Source/WebKit:
+    
+    * WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp:
+    (WebKit::WebFrameLoaderClient::finishedLoading): Fly-by fix, the
+    IPC::DataReference would reference a SharedBuffer going
+    out of scope before it was sent over IPC.
+    
+    Source/WebKitLegacy/mac:
+    
+    * WebView/WebHTMLView.mm:
+    (-[WebHTMLView namesOfPromisedFilesDroppedAtDestination:]):
+    
+    
+    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@290005 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+    2022-02-17  Jean-Yves Avenard  <[email protected]>
+
+            CrashTracer: com.apple.WebKit.WebContent at _javascript_Core: bmalloc_allocate_impl_impl_slow
+            https://bugs.webkit.org/show_bug.cgi?id=236695
+            rdar://87596724
+
+            Reviewed by Jer Noble.
+
+            When a new FragmentedSharedBuffer is received from the network process, it is proactively coalesced
+            into a SharedBuffer during each call to CachedResource::updateBuffer(). This causes a large number
+            of re-allocations and copies; essentially re-allocating each time the resource's buffer receives
+            one more chunk of data.
+            Instead we only flatten the data buffer once all data have been received.
+
+            Running speedtest.net with a gigabit link, we see a reduction of peak
+            memory use in the content process by about 40% (from 740MB to under 440MB of
+            physical memory, 1.7GB of memory allocation vs 535MB)
+
+            Covered by existing tests.
+
+            * editing/ios/EditorIOS.mm:
+            (WebCore::Editor::writeImageToPasteboard):
+            * editing/mac/EditorMac.mm:
+            (WebCore::Editor::writeImageToPasteboard):
+            * loader/DocumentThreadableLoader.cpp:
+            (WebCore::DocumentThreadableLoader::didFinishLoading):
+            * loader/cache/CachedApplicationManifest.cpp:
+            (WebCore::CachedApplicationManifest::finishLoading):
+            * loader/cache/CachedCSSStyleSheet.cpp:
+            (WebCore::CachedCSSStyleSheet::finishLoading):
+            * loader/cache/CachedFont.cpp:
+            (WebCore::CachedFont::ensureCustomFontData):
+            * loader/cache/CachedImage.cpp:
+            (WebCore::CachedImage::updateImageData):
+            * loader/cache/CachedRawResource.cpp:
+            (WebCore::CachedRawResource::calculateIncrementalDataChunk const):
+            (WebCore::CachedRawResource::updateBuffer):
+            (WebCore::CachedRawResource::finishLoading):
+            * loader/cache/CachedRawResource.h:
+            * loader/cache/CachedResource.h:
+            (WebCore::CachedResource::resourceBuffer const):
+            * loader/cache/CachedScript.cpp:
+            (WebCore::CachedScript::script):
+            * loader/cache/CachedXSLStyleSheet.cpp:
+            (WebCore::CachedXSLStyleSheet::finishLoading):
+
+2022-02-17  Russell Epstein  <[email protected]>
+
         Cherry-pick r289995. rdar://problem/87462825
 
     REGRESSION(r285885) Unable to exit Trip Details in Amtrak app

Modified: branches/safari-613.1.17.1-branch/Source/WebCore/editing/ios/EditorIOS.mm (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebCore/editing/ios/EditorIOS.mm	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebCore/editing/ios/EditorIOS.mm	2022-02-17 21:01:12 UTC (rev 290060)
@@ -204,7 +204,8 @@
     pasteboardImage.suggestedName = imageSourceURL.lastPathComponent().toString();
     pasteboardImage.imageSize = image->size();
     pasteboardImage.resourceMIMEType = pasteboard.resourceMIMEType(cachedImage->response().mimeType());
-    pasteboardImage.resourceData = cachedImage->resourceBuffer();
+    if (auto* buffer = cachedImage->resourceBuffer())
+        pasteboardImage.resourceData = buffer->makeContiguous();
 
     if (!pasteboard.isStatic())
         client()->getClientPasteboardData(makeRangeSelectingNode(imageElement), pasteboardImage.clientTypes, pasteboardImage.clientData);

Modified: branches/safari-613.1.17.1-branch/Source/WebCore/editing/mac/EditorMac.mm (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebCore/editing/mac/EditorMac.mm	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebCore/editing/mac/EditorMac.mm	2022-02-17 21:01:12 UTC (rev 290060)
@@ -345,7 +345,8 @@
     pasteboardImage.url.url = ""
     pasteboardImage.url.title = title;
     pasteboardImage.url.userVisibleForm = WTF::userVisibleString(pasteboardImage.url.url);
-    pasteboardImage.resourceData = cachedImage->resourceBuffer();
+    if (auto* buffer = cachedImage->resourceBuffer())
+        pasteboardImage.resourceData = buffer->makeContiguous();
     pasteboardImage.resourceMIMEType = cachedImage->response().mimeType();
 
     pasteboard.write(pasteboardImage);

Modified: branches/safari-613.1.17.1-branch/Source/WebCore/loader/DocumentThreadableLoader.cpp (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebCore/loader/DocumentThreadableLoader.cpp	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebCore/loader/DocumentThreadableLoader.cpp	2022-02-17 21:01:12 UTC (rev 290060)
@@ -491,15 +491,18 @@
 
         auto response = m_resource->response();
 
+        RefPtr<SharedBuffer> buffer;
+        if (m_resource->resourceBuffer())
+            buffer = m_resource->resourceBuffer()->makeContiguous();
         if (options().filteringPolicy == ResponseFilteringPolicy::Disable) {
             m_client->didReceiveResponse(identifier, response);
-            if (auto* buffer = m_resource->resourceBuffer())
+            if (buffer)
                 m_client->didReceiveData(*buffer);
         } else {
             ASSERT(response.type() == ResourceResponse::Type::Default);
 
             m_client->didReceiveResponse(identifier, ResourceResponse::filter(response, m_options.credentials == FetchOptions::Credentials::Include ? ResourceResponse::PerformExposeAllHeadersCheck::No : ResourceResponse::PerformExposeAllHeadersCheck::Yes));
-            if (auto* buffer = m_resource->resourceBuffer())
+            if (buffer)
                 m_client->didReceiveData(*buffer);
         }
     }

Modified: branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedApplicationManifest.cpp (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedApplicationManifest.cpp	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedApplicationManifest.cpp	2022-02-17 21:01:12 UTC (rev 290060)
@@ -43,9 +43,10 @@
 void CachedApplicationManifest::finishLoading(const FragmentedSharedBuffer* data, const NetworkLoadMetrics& metrics)
 {
     if (data) {
-        m_data = data->makeContiguous();
+        auto contiguousData = data->makeContiguous();
         setEncodedSize(data->size());
-        m_text = m_decoder->decodeAndFlush(m_data->data(), data->size());
+        m_text = m_decoder->decodeAndFlush(contiguousData->data(), data->size());
+        m_data = WTFMove(contiguousData);
     } else {
         m_data = nullptr;
         setEncodedSize(0);

Modified: branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedCSSStyleSheet.cpp (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedCSSStyleSheet.cpp	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedCSSStyleSheet.cpp	2022-02-17 21:01:12 UTC (rev 290060)
@@ -100,10 +100,11 @@
 void CachedCSSStyleSheet::finishLoading(const FragmentedSharedBuffer* data, const NetworkLoadMetrics& metrics)
 {
     if (data) {
-        m_data = data->makeContiguous();
+        auto contiguousData = data->makeContiguous();
         setEncodedSize(data->size());
         // Decode the data to find out the encoding and keep the sheet text around during checkNotify()
-        m_decodedSheetText = m_decoder->decodeAndFlush(m_data->data(), data->size());
+        m_decodedSheetText = m_decoder->decodeAndFlush(contiguousData->data(), data->size());
+        m_data = WTFMove(contiguousData);
     } else {
         m_data = nullptr;
         setEncodedSize(0);

Modified: branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedFont.cpp (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedFont.cpp	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedFont.cpp	2022-02-17 21:01:12 UTC (rev 290060)
@@ -87,7 +87,11 @@
 
 bool CachedFont::ensureCustomFontData(const AtomString&)
 {
-    return ensureCustomFontData(m_data.get());
+    if (!m_data)
+        return ensureCustomFontData(nullptr);
+    if (!m_data->isContiguous())
+        m_data = m_data->makeContiguous();
+    return ensureCustomFontData(downcast<SharedBuffer>(m_data.get()));
 }
 
 String CachedFont::calculateItemInCollection() const

Modified: branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedImage.cpp (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedImage.cpp	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedImage.cpp	2022-02-17 21:01:12 UTC (rev 290060)
@@ -542,7 +542,7 @@
 {
     if (!m_image || !m_data)
         return EncodedDataStatus::Error;
-    EncodedDataStatus result = m_image->setData(m_data.get(), allDataReceived);
+    EncodedDataStatus result = m_image->setData(m_data.copyRef(), allDataReceived);
     didUpdateImageData();
     return result;
 }

Modified: branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedRawResource.cpp (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedRawResource.cpp	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedRawResource.cpp	2022-02-17 21:01:12 UTC (rev 290060)
@@ -48,7 +48,7 @@
     ASSERT(isMainOrMediaOrIconOrRawResource());
 }
 
-std::optional<SharedBufferDataView> CachedRawResource::calculateIncrementalDataChunk(const SharedBuffer& data) const
+std::optional<SharedBufferDataView> CachedRawResource::calculateIncrementalDataChunk(const FragmentedSharedBuffer& data) const
 {
     size_t previousDataLength = encodedSize();
     if (data.size() <= previousDataLength)
@@ -68,7 +68,8 @@
     auto protectedData = Ref { data };
 
     ASSERT(dataBufferingPolicy() == DataBufferingPolicy::BufferData);
-    m_data = data.makeContiguous();
+    // While m_data is immutable, we need to drop the const, this will be removed in bug 236736.
+    m_data = const_cast<FragmentedSharedBuffer*>(&data);
 
     // Notify clients only of the newly appended content since the last run.
     auto previousDataSize = encodedSize();
@@ -112,15 +113,13 @@
     CachedResourceHandle<CachedRawResource> protectedThis(this);
     DataBufferingPolicy dataBufferingPolicy = this->dataBufferingPolicy();
     if (dataBufferingPolicy == DataBufferingPolicy::BufferData) {
+        m_data = const_cast<FragmentedSharedBuffer*>(data);
         if (data) {
-            if (data != m_data.get())
-                m_data = data->makeContiguous();
-            if (auto incrementalData = calculateIncrementalDataChunk(*m_data)) {
+            if (auto incrementalData = calculateIncrementalDataChunk(*data)) {
                 setEncodedSize(data->size());
                 notifyClientsDataWasReceived(incrementalData->createSharedBuffer());
             }
-        } else
-            m_data = nullptr;
+        }
     }
 
 #if USE(QUICK_LOOK)

Modified: branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedRawResource.h (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedRawResource.h	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedRawResource.h	2022-02-17 21:01:12 UTC (rev 290060)
@@ -67,7 +67,7 @@
     void switchClientsToRevalidatedResource() override;
     bool mayTryReplaceEncodedData() const override { return m_allowEncodedDataReplacement; }
 
-    std::optional<SharedBufferDataView> calculateIncrementalDataChunk(const SharedBuffer&) const;
+    std::optional<SharedBufferDataView> calculateIncrementalDataChunk(const FragmentedSharedBuffer&) const;
     void notifyClientsDataWasReceived(const SharedBuffer&);
     
 #if USE(QUICK_LOOK)

Modified: branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedResource.h (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedResource.h	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedResource.h	2022-02-17 21:01:12 UTC (rev 290060)
@@ -213,7 +213,7 @@
 
     void clearLoader();
 
-    SharedBuffer* resourceBuffer() const { return m_data.get(); }
+    FragmentedSharedBuffer* resourceBuffer() const { return m_data.get(); }
 
     virtual void redirectReceived(ResourceRequest&&, const ResourceResponse&, CompletionHandler<void(ResourceRequest&&)>&&);
     virtual void responseReceived(const ResourceResponse&);
@@ -336,7 +336,7 @@
     HashCountedSet<CachedResourceClient*> m_clients;
     std::unique_ptr<ResourceRequest> m_originalRequest; // Needed by Ping loads.
     RefPtr<SubresourceLoader> m_loader;
-    RefPtr<SharedBuffer> m_data;
+    RefPtr<FragmentedSharedBuffer> m_data;
 
 private:
     MonotonicTime m_lastDecodedAccessTime; // Used as a "thrash guard" in the cache

Modified: branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedScript.cpp (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedScript.cpp	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedScript.cpp	2022-02-17 21:01:12 UTC (rev 290060)
@@ -59,10 +59,14 @@
     if (!m_data)
         return emptyString();
 
+    if (!m_data->isContiguous())
+        m_data = m_data->makeContiguous();
+
+    auto& contiguousData = downcast<SharedBuffer>(*m_data);
     if (m_decodingState == NeverDecoded
         && PAL::TextEncoding(encoding()).isByteBasedEncoding()
         && m_data->size()
-        && charactersAreAllASCII(m_data->data(), m_data->size())) {
+        && charactersAreAllASCII(contiguousData.data(), m_data->size())) {
 
         m_decodingState = DataAndDecodedStringHaveSameBytes;
 
@@ -70,14 +74,14 @@
         setDecodedSize(0);
         m_decodedDataDeletionTimer.stop();
 
-        m_scriptHash = StringHasher::computeHashAndMaskTop8Bits(m_data->data(), m_data->size());
+        m_scriptHash = StringHasher::computeHashAndMaskTop8Bits(contiguousData.data(), m_data->size());
     }
 
     if (m_decodingState == DataAndDecodedStringHaveSameBytes)
-        return { m_data->data(), static_cast<unsigned>(m_data->size()) };
+        return { contiguousData.data(), static_cast<unsigned>(m_data->size()) };
 
     if (!m_script) {
-        m_script = m_decoder->decodeAndFlush(m_data->data(), encodedSize());
+        m_script = m_decoder->decodeAndFlush(contiguousData.data(), encodedSize());
         ASSERT(!m_scriptHash || m_scriptHash == m_script.impl()->hash());
         if (m_decodingState == NeverDecoded)
             m_scriptHash = m_script.impl()->hash();

Modified: branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedXSLStyleSheet.cpp (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedXSLStyleSheet.cpp	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebCore/loader/cache/CachedXSLStyleSheet.cpp	2022-02-17 21:01:12 UTC (rev 290060)
@@ -64,9 +64,10 @@
 void CachedXSLStyleSheet::finishLoading(const FragmentedSharedBuffer* data, const NetworkLoadMetrics& metrics)
 {
     if (data) {
-        m_data = data->makeContiguous();
+        auto contiguousData = data->makeContiguous();
         setEncodedSize(data->size());
-        m_sheet = m_decoder->decodeAndFlush(m_data->data(), encodedSize());
+        m_sheet = m_decoder->decodeAndFlush(contiguousData->data(), encodedSize());
+        m_data = WTFMove(contiguousData);
     } else {
         m_data = nullptr;
         setEncodedSize(0);

Modified: branches/safari-613.1.17.1-branch/Source/WebKit/ChangeLog (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebKit/ChangeLog	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebKit/ChangeLog	2022-02-17 21:01:12 UTC (rev 290060)
@@ -1,3 +1,81 @@
+2022-02-17  Russell Epstein  <[email protected]>
+
+        Cherry-pick r290005. rdar://problem/87596724
+
+    CrashTracer: com.apple.WebKit.WebContent at _javascript_Core: bmalloc_allocate_impl_impl_slow
+    https://bugs.webkit.org/show_bug.cgi?id=236695
+    rdar://87596724
+    
+    Reviewed by Jer Noble.
+    
+    Source/WebCore:
+    
+    When a new FragmentedSharedBuffer is received from the network process, it is proactively coalesced
+    into a SharedBuffer during each call to CachedResource::updateBuffer(). This causes a large number
+    of re-allocations and copies; essentially re-allocating each time the resource's buffer receives
+    one more chunk of data.
+    Instead we only flatten the data buffer once all data have been received.
+    
+    Running speedtest.net with a gigabit link, we see a reduction of peak
+    memory use in the content process by about 40% (from 740MB to under 440MB of
+    physical memory, 1.7GB of memory allocation vs 535MB)
+    
+    Covered by existing tests.
+    
+    * editing/ios/EditorIOS.mm:
+    (WebCore::Editor::writeImageToPasteboard):
+    * editing/mac/EditorMac.mm:
+    (WebCore::Editor::writeImageToPasteboard):
+    * loader/DocumentThreadableLoader.cpp:
+    (WebCore::DocumentThreadableLoader::didFinishLoading):
+    * loader/cache/CachedApplicationManifest.cpp:
+    (WebCore::CachedApplicationManifest::finishLoading):
+    * loader/cache/CachedCSSStyleSheet.cpp:
+    (WebCore::CachedCSSStyleSheet::finishLoading):
+    * loader/cache/CachedFont.cpp:
+    (WebCore::CachedFont::ensureCustomFontData):
+    * loader/cache/CachedImage.cpp:
+    (WebCore::CachedImage::updateImageData):
+    * loader/cache/CachedRawResource.cpp:
+    (WebCore::CachedRawResource::calculateIncrementalDataChunk const):
+    (WebCore::CachedRawResource::updateBuffer):
+    (WebCore::CachedRawResource::finishLoading):
+    * loader/cache/CachedRawResource.h:
+    * loader/cache/CachedResource.h:
+    (WebCore::CachedResource::resourceBuffer const):
+    * loader/cache/CachedScript.cpp:
+    (WebCore::CachedScript::script):
+    * loader/cache/CachedXSLStyleSheet.cpp:
+    (WebCore::CachedXSLStyleSheet::finishLoading):
+    
+    Source/WebKit:
+    
+    * WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp:
+    (WebKit::WebFrameLoaderClient::finishedLoading): Fly-by fix, the
+    IPC::DataReference would reference a SharedBuffer going
+    out of scope before it was sent over IPC.
+    
+    Source/WebKitLegacy/mac:
+    
+    * WebView/WebHTMLView.mm:
+    (-[WebHTMLView namesOfPromisedFilesDroppedAtDestination:]):
+    
+    
+    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@290005 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+    2022-02-17  Jean-Yves Avenard  <[email protected]>
+
+            CrashTracer: com.apple.WebKit.WebContent at _javascript_Core: bmalloc_allocate_impl_impl_slow
+            https://bugs.webkit.org/show_bug.cgi?id=236695
+            rdar://87596724
+
+            Reviewed by Jer Noble.
+
+            * WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp:
+            (WebKit::WebFrameLoaderClient::finishedLoading): Fly-by fix, the
+            IPC::DataReference would reference a SharedBuffer going
+            out of scope before it was sent over IPC.
+
 2022-02-16  Russell Epstein  <[email protected]>
 
         Cherry-pick r289926. rdar://problem/88787266

Modified: branches/safari-613.1.17.1-branch/Source/WebKit/WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebKit/WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebKit/WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp	2022-02-17 21:01:12 UTC (rev 290060)
@@ -1187,8 +1187,11 @@
             if (!webPage)
                 return;
 
-            RefPtr<FragmentedSharedBuffer> mainResourceData = loader->mainResourceData();
-            IPC::DataReference dataReference(mainResourceData ? mainResourceData->makeContiguous()->data() : nullptr, mainResourceData ? mainResourceData->size() : 0);
+            RefPtr<const SharedBuffer> contiguousData;
+            RefPtr<const FragmentedSharedBuffer> mainResourceData = loader->mainResourceData();
+            if (mainResourceData)
+                contiguousData = mainResourceData->makeContiguous();
+            IPC::DataReference dataReference(contiguousData ? contiguousData->data() : nullptr, contiguousData ? contiguousData->size() : 0);
             webPage->send(Messages::WebPageProxy::DidFinishLoadingDataForCustomContentProvider(loader->response().suggestedFilename(), dataReference));
         }
 

Modified: branches/safari-613.1.17.1-branch/Source/WebKitLegacy/mac/ChangeLog (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebKitLegacy/mac/ChangeLog	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebKitLegacy/mac/ChangeLog	2022-02-17 21:01:12 UTC (rev 290060)
@@ -1,3 +1,79 @@
+2022-02-17  Russell Epstein  <[email protected]>
+
+        Cherry-pick r290005. rdar://problem/87596724
+
+    CrashTracer: com.apple.WebKit.WebContent at _javascript_Core: bmalloc_allocate_impl_impl_slow
+    https://bugs.webkit.org/show_bug.cgi?id=236695
+    rdar://87596724
+    
+    Reviewed by Jer Noble.
+    
+    Source/WebCore:
+    
+    When a new FragmentedSharedBuffer is received from the network process, it is proactively coalesced
+    into a SharedBuffer during each call to CachedResource::updateBuffer(). This causes a large number
+    of re-allocations and copies; essentially re-allocating each time the resource's buffer receives
+    one more chunk of data.
+    Instead we only flatten the data buffer once all data have been received.
+    
+    Running speedtest.net with a gigabit link, we see a reduction of peak
+    memory use in the content process by about 40% (from 740MB to under 440MB of
+    physical memory, 1.7GB of memory allocation vs 535MB)
+    
+    Covered by existing tests.
+    
+    * editing/ios/EditorIOS.mm:
+    (WebCore::Editor::writeImageToPasteboard):
+    * editing/mac/EditorMac.mm:
+    (WebCore::Editor::writeImageToPasteboard):
+    * loader/DocumentThreadableLoader.cpp:
+    (WebCore::DocumentThreadableLoader::didFinishLoading):
+    * loader/cache/CachedApplicationManifest.cpp:
+    (WebCore::CachedApplicationManifest::finishLoading):
+    * loader/cache/CachedCSSStyleSheet.cpp:
+    (WebCore::CachedCSSStyleSheet::finishLoading):
+    * loader/cache/CachedFont.cpp:
+    (WebCore::CachedFont::ensureCustomFontData):
+    * loader/cache/CachedImage.cpp:
+    (WebCore::CachedImage::updateImageData):
+    * loader/cache/CachedRawResource.cpp:
+    (WebCore::CachedRawResource::calculateIncrementalDataChunk const):
+    (WebCore::CachedRawResource::updateBuffer):
+    (WebCore::CachedRawResource::finishLoading):
+    * loader/cache/CachedRawResource.h:
+    * loader/cache/CachedResource.h:
+    (WebCore::CachedResource::resourceBuffer const):
+    * loader/cache/CachedScript.cpp:
+    (WebCore::CachedScript::script):
+    * loader/cache/CachedXSLStyleSheet.cpp:
+    (WebCore::CachedXSLStyleSheet::finishLoading):
+    
+    Source/WebKit:
+    
+    * WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp:
+    (WebKit::WebFrameLoaderClient::finishedLoading): Fly-by fix, the
+    IPC::DataReference would reference a SharedBuffer going
+    out of scope before it was sent over IPC.
+    
+    Source/WebKitLegacy/mac:
+    
+    * WebView/WebHTMLView.mm:
+    (-[WebHTMLView namesOfPromisedFilesDroppedAtDestination:]):
+    
+    
+    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@290005 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+    2022-02-17  Jean-Yves Avenard  <[email protected]>
+
+            CrashTracer: com.apple.WebKit.WebContent at _javascript_Core: bmalloc_allocate_impl_impl_slow
+            https://bugs.webkit.org/show_bug.cgi?id=236695
+            rdar://87596724
+
+            Reviewed by Jer Noble.
+
+            * WebView/WebHTMLView.mm:
+            (-[WebHTMLView namesOfPromisedFilesDroppedAtDestination:]):
+
 2022-02-07  Russell Epstein  <[email protected]>
 
         Cherry-pick r287951. rdar://problem/83501315

Modified: branches/safari-613.1.17.1-branch/Source/WebKitLegacy/mac/WebView/WebHTMLView.mm (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebKitLegacy/mac/WebView/WebHTMLView.mm	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebKitLegacy/mac/WebView/WebHTMLView.mm	2022-02-17 21:01:12 UTC (rev 290060)
@@ -4302,12 +4302,12 @@
 {
     RetainPtr<NSFileWrapper> wrapper;
     NSURL *draggingElementURL = nil;
-    
+
     if (auto tiffResource = _private->promisedDragTIFFDataSource) {
         if (auto* buffer = tiffResource->resourceBuffer()) {
             NSURLResponse *response = tiffResource->response().nsURLResponse();
             draggingElementURL = [response URL];
-            wrapper = adoptNS([[NSFileWrapper alloc] initRegularFileWithContents:buffer->createNSData().get()]);
+            wrapper = adoptNS([[NSFileWrapper alloc] initRegularFileWithContents:buffer->makeContiguous()->createNSData().get()]);
             NSString* filename = [response suggestedFilename];
             NSString* trueExtension(tiffResource->image()->filenameExtension());
             if (!matchesExtensionOrEquivalent(filename, trueExtension))
@@ -4315,17 +4315,17 @@
             [wrapper setPreferredFilename:filename];
         }
     }
-    
+
     if (!wrapper) {
         ASSERT(![self _webView] || [self _isTopHTMLView]);
         auto* page = core([self _webView]);
-        
+
         //If a load occurs midway through a drag, the view may be detached, which gives
         //us no ability to get to the original Page, so we cannot access any drag state
         //FIXME: is there a way to recover?
         if (!page) 
             return nil; 
-        
+
         const URL& imageURL = page->dragController().draggingImageURL();
         if (!imageURL.isEmpty())
             draggingElementURL = imageURL;

Modified: branches/safari-613.1.17.1-branch/Source/WebKitLegacy/win/WebDataSource.cpp (290059 => 290060)


--- branches/safari-613.1.17.1-branch/Source/WebKitLegacy/win/WebDataSource.cpp	2022-02-17 21:01:05 UTC (rev 290059)
+++ branches/safari-613.1.17.1-branch/Source/WebKitLegacy/win/WebDataSource.cpp	2022-02-17 21:01:12 UTC (rev 290060)
@@ -349,7 +349,7 @@
     if (!cachedResource)
         return E_FAIL;
 
-    *resource = WebResource::createInstance(cachedResource->resourceBuffer(), cachedResource->response());
+    *resource = WebResource::createInstance(cachedResource->resourceBuffer()->makeContiguous(), cachedResource->response());
     return S_OK;
 }
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to