CairoSVG icon indicating copy to clipboard operation
CairoSVG copied to clipboard

Stuck Forever Eating Memory

Open p30arena opened this issue 3 years ago • 3 comments

CairoSVG==2.5.2

The Code:

image

The SVG compressed:

test.zip

p30arena avatar Oct 12 '22 07:10 p30arena

Hi!

This image is … strange 😁️. It’s big, it includes a lot of raster images as base64 URLs nested in symbols and drawn by use tags. To be honest, a SVG carefully built to break CairoSVG wouln’t be very different from yours. Even in browsers (and they are among the most optimized pieces of software), it can take more than 5 seconds to be displayed, without the network part!

Technically, it’s not stuck forever (it’s not an endless loop), it’s just incredibly slow, and it tries to eat all the available memory it can find.

If you really want to use CairoSVG to render this document, you can remove these two lines that keep a cache of references and is responsible for the memory hunger. It’s still really slow (more than 1 hour?), the rendering may be broken, but at least it won’t take too much memory.

We will probably not "fix" anything in CairoSVG that could help to render this kind of images. CairoSVG is just not the library you’re looking for 😢️.

(For the record, and if anybody wants to improve this, it doesn’t work because 1- CairoSVG caches all the elements displayed as use tags even if in this case, and contrary to most of the SVG files, they’re never reused and thus take a lot of memory for nothing; 2- use tags referencing a given element for the first time are slow because they create a new tree each time; 3- finding the referenced element in the tree is longer when the element is at the end of the document, so rendering is getting slower and slower as referenced elements are lower in the document.)

liZe avatar Oct 12 '22 19:10 liZe

test

(A bit less than one hour on my laptop.)

liZe avatar Oct 12 '22 20:10 liZe

@liZe aspose handles this image in less than 5 secs! I rather use Cairo instead of that lib, I'd be happy to hear better news from you.

Cheers

import aspose.words as aw
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
shape = builder.insert_image(svg_path)
doc.save(png_path)

p30arena avatar Oct 13 '22 07:10 p30arena