Skip to content

Commit

Permalink
Take into account memory allocated in XHR for ArrayBuffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Mutavchi committed Dec 4, 2015
1 parent 586f9a6 commit e464f40
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
38 changes: 26 additions & 12 deletions Source/WebCore/xml/XMLHttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,7 @@ void XMLHttpRequest::clearResponse()

void XMLHttpRequest::clearResponseBuffers()
{
m_reportedExtraMemoryCost = 0;
m_responseBuilder.clear();
m_responseEncoding = String();
m_createdDocument = false;
Expand Down Expand Up @@ -907,20 +908,31 @@ void XMLHttpRequest::abortError()
dispatchErrorEvents(eventNames().abortEvent);
}

void XMLHttpRequest::dropProtection()
size_t XMLHttpRequest::extraMemoryCost() const
{
// The XHR object itself holds on to the responseText, and
// thus has extra cost even independent of any
// responseText or responseXML objects it has handed
// out. But it is protected from GC while loading, so this
// can't be recouped until the load is done, so only
// report the extra cost at that point.
JSC::VM& vm = scriptExecutionContext()->vm();
JSC::JSLockHolder lock(vm);
// FIXME: Adopt reportExtraMemoryVisited, and switch to reportExtraMemoryAllocated.
// https://bugs.webkit.org/show_bug.cgi?id=142595
vm.heap.deprecatedReportExtraMemory(m_responseBuilder.length() * 2);
size_t extraMemoryCost = m_responseBuilder.length();
extraMemoryCost += m_binaryResponseBuilder ? m_binaryResponseBuilder->size() : 0;
return extraMemoryCost;
}

void XMLHttpRequest::reportExtraMemoryCost()
{
size_t extraMemoryCost = this->extraMemoryCost();
if (extraMemoryCost < m_reportedExtraMemoryCost)
return;
size_t extraMemoryCostDelta = extraMemoryCost - m_reportedExtraMemoryCost;
if (extraMemoryCostDelta > 0) {
JSC::VM& vm = scriptExecutionContext()->vm();
JSC::JSLockHolder lock(vm);
// FIXME: Adopt reportExtraMemoryVisited, and switch to reportExtraMemoryAllocated.
// https://bugs.webkit.org/show_bug.cgi?id=142595
vm.heap.deprecatedReportExtraMemory(extraMemoryCostDelta);
m_reportedExtraMemoryCost = extraMemoryCost;
}
}

void XMLHttpRequest::dropProtection()
{
unsetPendingActivity(this);
}

Expand Down Expand Up @@ -1105,6 +1117,8 @@ void XMLHttpRequest::didFinishLoading(unsigned long identifier, double)

m_responseBuilder.shrinkToFit();

reportExtraMemoryCost();

InspectorInstrumentation::didFinishXHRLoading(scriptExecutionContext(), this, identifier, m_responseBuilder.toStringPreserveCapacity(), m_url, m_lastSendURL, m_lastSendLineNumber, m_lastSendColumnNumber);

bool hadLoader = m_loader;
Expand Down
4 changes: 4 additions & 0 deletions Source/WebCore/xml/XMLHttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ class XMLHttpRequest final : public ScriptWrappable, public RefCounted<XMLHttpRe

void resumeTimerFired();

size_t extraMemoryCost() const;
void reportExtraMemoryCost();
size_t m_reportedExtraMemoryCost;

std::unique_ptr<XMLHttpRequestUpload> m_upload;

URL m_url;
Expand Down

0 comments on commit e464f40

Please sign in to comment.