REGRESSION(r287684) speedtest.net uses many GB of memory
https://bugs.webkit.org/show_bug.cgi?id=235615
rdar://87830583

Reviewed by Youenn Fablet.

The regression was introduced with r286937 and is a good example of
errors introduced when attempting to optimise things too early.
CachedRawResource::updateBuffer does a search in the accumulating
resource's SharedBuffer, search that was taking O(log(n)+1) prior r286937
where n is the number of DataView segments in the SharedBuffer.
This was simplified as a O(1) operation by using the combined contiguous
SharedBuffer instead.
However, that caused every single intermediary accumulated buffers to be
kept referenced by the XMLHttpRequest SharedBufferBuilder leading to
massive memory use.

* loader/cache/CachedRawResource.cpp:
(WebCore::CachedRawResource::updateBuffer):


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@288614 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index b53ec20..9a6598b 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,25 @@
+2022-01-26  Jean-Yves Avenard  <jya@apple.com>
+
+        REGRESSION(r287684) speedtest.net uses many GB of memory
+        https://bugs.webkit.org/show_bug.cgi?id=235615
+        rdar://87830583
+
+        Reviewed by Youenn Fablet.
+
+        The regression was introduced with r286937 and is a good example of
+        errors introduced when attempting to optimise things too early.
+        CachedRawResource::updateBuffer does a search in the accumulating
+        resource's SharedBuffer, search that was taking O(log(n)+1) prior r286937
+        where n is the number of DataView segments in the SharedBuffer.
+        This was simplified as a O(1) operation by using the combined contiguous
+        SharedBuffer instead.
+        However, that caused every single intermediary accumulated buffers to be
+        kept referenced by the XMLHttpRequest SharedBufferBuilder leading to
+        massive memory use.
+
+        * loader/cache/CachedRawResource.cpp:
+        (WebCore::CachedRawResource::updateBuffer):
+
 2022-01-26  Antoine Quint  <graouts@webkit.org>
 
         [Model] Mouse interaction for <model> is flipped in the y-axis
diff --git a/Source/WebCore/loader/cache/CachedRawResource.cpp b/Source/WebCore/loader/cache/CachedRawResource.cpp
index d336113..91a3992 100644
--- a/Source/WebCore/loader/cache/CachedRawResource.cpp
+++ b/Source/WebCore/loader/cache/CachedRawResource.cpp
@@ -67,8 +67,8 @@
     m_data = data.makeContiguous();
 
     auto previousDataSize = encodedSize();
-    while (m_data->size() > previousDataSize) {
-        auto incrementalData = m_data->getSomeData(previousDataSize);
+    while (data.size() > previousDataSize) {
+        auto incrementalData = data.getSomeData(previousDataSize);
         previousDataSize += incrementalData.size();
 
         SetForScope<bool> notifyScope(m_inIncrementalDataNotify, true);