CairoSVG icon indicating copy to clipboard operation
CairoSVG copied to clipboard

cache XML trees for SVG use tags to accelerate chart rendering

Open n1ywb opened this issue 7 years ago • 0 comments

I was rendering a document with weasyprint with a complex SVG scatter chart with ~500 points which are all symbolized by SVG use tags which reference a g in the defs section by xlink.

I profiled with cProfile and I found that it was parsing the symbol XML for every single point on the chart. This was consuming 40-50% of the overall program execution time.

So I used cachetools to cache use tag trees from the raw XML. This cut the time to render the document almost in half. Re-profiling revealed no further XML parsing bottlenecks.

I noticed there is already a tree caching mechanism in place here, but clearly it's not being very efficient or aggressive in it's caching, since it's missing on the use tags' xlink-ed tree. So rather than merge this and add the dependency you or somebody might want to try tuning that existing cache.

this increases memory usage by the size of the unique children of all the defs sections in all the charts in the doc + their parsed trees. this should generally be a non-issue since defs is typically used for chart symbology and so are likely to be small and identical across charts. Even if there were large complex graphics in the defs it would probably still be worth the gain in CPU time since putting something in defs usually implies you'll be referencing it with use multiple times.

before/after profiles attached

cprofileoutput.zip

n1ywb avatar Dec 17 '18 16:12 n1ywb