⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 201561 in webkit


Ignore:
Timestamp:
Jun 1, 2016, 11:50:09 AM (10 years ago)
Author:
commit-queue@webkit.org
Message:

SVGImage should report its memory cost to JS garbage collector
https://bugs.webkit.org/show_bug.cgi?id=158139

Patch by Said Abou-Hallawa <sabouhallawa@apple.com> on 2016-06-01
Reviewed by Geoffrey Garen.

Like what we do in HTMLImageLoader::notifyFinished() by reporting the memory
cost of the BitmapImage, we need to do something similar for the SVGImage. In
SVGImage::dataChange() and when allDataReceived is true, we can calculate
the size of all DOM nodes and their renderers. The size of the encoded data
has to be added as well to the total memory cost. An approximation for the
memory cost has to be used since it is costly to get an accurate number.

  • bindings/js/JSDocumentCustom.cpp:

(WebCore::reportMemoryForDocumentIfFrameless): Use Node::approximateMemoryCost()
instead of sizeof(Node). A Node's descendant can override this function and
return a more accurate memory cost.

  • dom/Node.h:

(WebCore::Node::approximateMemoryCost): Define this new virtual function in the
Node class. Its default value is sizeof(Node) but any descendant can return a
more accurate number.

  • platform/graphics/Image.h:

(WebCore::Image::data): Define a const version of data() so it can be called
the const function SVGImage::reportApproximateMemoryCost().

  • svg/SVGGraphicsElement.h: Override approximateMemoryCost() to return

sizeof(SVGGraphicsElement).

  • svg/SVGPathElement.cpp:

(WebCore::SVGPathElement::approximateMemoryCost): Override this function to return
the memory cost of the points and the m_path of the renderer.

  • svg/SVGPathElement.h:
  • svg/SVGPolyElement.cpp:

(WebCore::SVGPolyElement::approximateMemoryCost): Override this function to return
the memory cost of the points and the m_path of the renderer.

  • svg/SVGPolyElement.h:
  • svg/graphics/SVGImage.cpp:

(WebCore::SVGImage::reportApproximateMemoryCost): Calculate the memory cost of the
nodes in the SVGDocument of an SVGImage. Then report this number to the JS garbage
collector.

(WebCore::SVGImage::dataChanged): After loading all the SVG encoded data and building
its DOM tree and the render tree, report the total memory cost to the JS garbage collector.

  • svg/graphics/SVGImage.h:
Location:
trunk/Source/WebCore
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r201559 r201561  
     12016-06-01  Said Abou-Hallawa  <sabouhallawa@apple.com>
     2
     3        SVGImage should report its memory cost to JS garbage collector
     4        https://bugs.webkit.org/show_bug.cgi?id=158139
     5
     6        Reviewed by Geoffrey Garen.
     7
     8        Like what we do in HTMLImageLoader::notifyFinished() by reporting the memory
     9        cost of the BitmapImage, we need to do something similar for the SVGImage. In
     10        SVGImage::dataChange() and when allDataReceived is true, we can calculate
     11        the size of all DOM nodes and their renderers. The size of the encoded data
     12        has to be added as well to the total memory cost. An approximation for the
     13        memory cost has to be used since it is costly to get an accurate number.
     14
     15        * bindings/js/JSDocumentCustom.cpp:
     16        (WebCore::reportMemoryForDocumentIfFrameless): Use Node::approximateMemoryCost()
     17        instead of sizeof(Node). A Node's descendant can override this function and
     18        return a more accurate memory cost.
     19       
     20        * dom/Node.h:
     21        (WebCore::Node::approximateMemoryCost): Define this new virtual function in the
     22        Node class. Its default value is sizeof(Node) but any descendant can return a
     23        more accurate number.
     24       
     25        * platform/graphics/Image.h:
     26        (WebCore::Image::data): Define a const version of data() so it can be called
     27        the const function SVGImage::reportApproximateMemoryCost().
     28       
     29        * svg/SVGGraphicsElement.h: Override approximateMemoryCost() to return
     30        sizeof(SVGGraphicsElement).
     31       
     32        * svg/SVGPathElement.cpp:
     33        (WebCore::SVGPathElement::approximateMemoryCost): Override this function to return
     34        the memory cost of the points and the m_path of the renderer.
     35        * svg/SVGPathElement.h:
     36       
     37        * svg/SVGPolyElement.cpp:
     38        (WebCore::SVGPolyElement::approximateMemoryCost): Override this function to return
     39        the memory cost of the points and the m_path of the renderer.
     40        * svg/SVGPolyElement.h:
     41       
     42        * svg/graphics/SVGImage.cpp:
     43        (WebCore::SVGImage::reportApproximateMemoryCost): Calculate the memory cost of the
     44        nodes in the SVGDocument of an SVGImage. Then report this number to the JS garbage
     45        collector.
     46       
     47        (WebCore::SVGImage::dataChanged): After loading all the SVG encoded data and building
     48        its DOM tree and the render tree, report the total memory cost to the JS garbage collector.
     49        * svg/graphics/SVGImage.h:
     50
    1512016-06-01  Andreas Kling  <akling@apple.com>
    252
  • trunk/Source/WebCore/bindings/js/JSDocumentCustom.cpp

    r201129 r201561  
    9090        return;
    9191
    92     size_t nodeCount = 0;
     92    size_t memoryCost = 0;
    9393    for (Node* node = &document; node; node = NodeTraversal::next(*node))
    94         ++nodeCount;
     94        memoryCost += node->approximateMemoryCost();
    9595
    9696    // FIXME: Adopt reportExtraMemoryVisited, and switch to reportExtraMemoryAllocated.
    9797    // https://bugs.webkit.org/show_bug.cgi?id=142595
    98     state.heap()->deprecatedReportExtraMemory(nodeCount * sizeof(Node));
     98    state.heap()->deprecatedReportExtraMemory(memoryCost);
    9999}
    100100
  • trunk/Source/WebCore/dom/Node.h

    r201305 r201561  
    144144    virtual void setNodeValue(const String&, ExceptionCode&);
    145145    virtual NodeType nodeType() const = 0;
     146    virtual size_t approximateMemoryCost() const { return sizeof(*this); }
    146147    ContainerNode* parentNode() const;
    147148    static ptrdiff_t parentNodeMemoryOffset() { return OBJECT_OFFSETOF(Node, m_parentNode); }
  • trunk/Source/WebCore/platform/graphics/Image.h

    r201424 r201561  
    123123
    124124    SharedBuffer* data() { return m_encodedImageData.get(); }
     125    const SharedBuffer* data() const { return m_encodedImageData.get(); }
    125126
    126127    // Animation begins whenever someone draws the image, so startAnimation() is not normally called.
  • trunk/Source/WebCore/svg/SVGGraphicsElement.h

    r200041 r201561  
    5454    RenderPtr<RenderElement> createElementRenderer(RenderStyle&&, const RenderTreePosition&) override;
    5555
     56    size_t approximateMemoryCost() const override { return sizeof(*this); }
     57
    5658protected:
    5759    SVGGraphicsElement(const QualifiedName&, Document&);
  • trunk/Source/WebCore/svg/SVGPathElement.cpp

    r200041 r201561  
    364364}
    365365
     366size_t SVGPathElement::approximateMemoryCost() const
     367{
     368    // This is an approximation for path memory cost since the path is parsed on demand.
     369    size_t pathMemoryCost = (m_pathByteStream.size() / 10) * sizeof(FloatPoint);
     370    // We need to account for the memory which is allocated by the RenderSVGPath::m_path.
     371    return sizeof(*this) + (renderer() ? pathMemoryCost * 2 + sizeof(RenderSVGPath) : pathMemoryCost);
     372}
     373
    366374void SVGPathElement::pathSegListChanged(SVGPathSegRole role, ListModification listModification)
    367375{
  • trunk/Source/WebCore/svg/SVGPathElement.h

    r200041 r201561  
    102102    void animatedPropertyWillBeDeleted();
    103103
     104    size_t approximateMemoryCost() const override;
     105
    104106private:
    105107    SVGPathElement(const QualifiedName&, Document&);
  • trunk/Source/WebCore/svg/SVGPolyElement.cpp

    r196268 r201561  
    131131}
    132132
     133size_t SVGPolyElement::approximateMemoryCost() const
     134{
     135    size_t pointsCost = pointList().size() * sizeof(FloatPoint);
     136    // We need to account for the memory which is allocated by the RenderSVGPath::m_path.
     137    return sizeof(*this) + (renderer() ? pointsCost * 2 + sizeof(RenderSVGPath) : pointsCost);
    133138}
     139
     140}
  • trunk/Source/WebCore/svg/SVGPolyElement.h

    r197563 r201561  
    3939    static const SVGPropertyInfo* pointsPropertyInfo();
    4040
     41    size_t approximateMemoryCost() const override;
     42
    4143protected:
    4244    SVGPolyElement(const QualifiedName&, Document&);
  • trunk/Source/WebCore/svg/graphics/SVGImage.cpp

    r198655 r201561  
    3030
    3131#include "Chrome.h"
     32#include "DOMWindow.h"
    3233#include "DocumentLoader.h"
    3334#include "ElementIterator.h"
     
    3738#include "ImageObserver.h"
    3839#include "IntRect.h"
     40#include "JSDOMWindowBase.h"
    3941#include "MainFrame.h"
    4042#include "PageConfiguration.h"
     
    4951#include "Settings.h"
    5052#include "TextStream.h"
     53#include <runtime/JSCInlines.h>
     54#include <runtime/JSLock.h>
    5155
    5256namespace WebCore {
     
    354358}
    355359
     360void SVGImage::reportApproximateMemoryCost() const
     361{
     362    Document* document = m_page->mainFrame().document();
     363    size_t decodedImageMemoryCost = 0;
     364
     365    for (Node* node = document; node; node = NodeTraversal::next(*node))
     366        decodedImageMemoryCost += node->approximateMemoryCost();
     367
     368    JSC::VM& vm = JSDOMWindowBase::commonVM();
     369    JSC::JSLockHolder lock(vm);
     370    // FIXME: Adopt reportExtraMemoryVisited, and switch to reportExtraMemoryAllocated.
     371    // https://bugs.webkit.org/show_bug.cgi?id=142595
     372    vm.heap.deprecatedReportExtraMemory(decodedImageMemoryCost + data()->size());
     373}
     374
    356375bool SVGImage::dataChanged(bool allDataReceived)
    357376{
     
    394413        // Set the intrinsic size before a container size is available.
    395414        m_intrinsicSize = containerSize();
     415        reportApproximateMemoryCost();
    396416    }
    397417
  • trunk/Source/WebCore/svg/graphics/SVGImage.h

    r198655 r201561  
    8383    void computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio) override;
    8484
     85    void reportApproximateMemoryCost() const;
    8586    bool dataChanged(bool allDataReceived) override;
    8687
Note: See TracChangeset for help on using the changeset viewer.