r-lib / svglite

A lightweight svg graphics device for R
https://svglite.r-lib.org
180 stars 39 forks source link

Improve the performance of svgstring() #70

Closed yixuan closed 8 years ago

yixuan commented 8 years ago

This PR should greatly improve the performance of svgstring() based on the discussion in #58.

The basic idea is to let svgstring_ return a pointer of the string buffer, instead of copying the whole string to R every time. When user requests the SVG content, a reader function will be called on the pointer and the string will be retrieved.

When device is closed, the final string will be copied to R, so users can still retrieve the SVG string even if device is closed.

Based on this PR, below is my result for the same test in #58.

system.time({
    fl <- tempfile(fileext=".svg")
    svglite(file = fl)
    plot(runif(10000), 1:10000)
    dev.off()
})
##   user  system elapsed 
##  0.022   0.008   0.029 

system.time({
    svgstring()
    plot(runif(10000), 1:10000)
    dev.off()
})
##   user  system elapsed 
##  0.025   0.001   0.026 
hadley commented 8 years ago

BTW in the future can you please include Fixes #58 in either the PR comment or one of the commit messages? That way when I merge the PR, the related issue is automatically closed.

yixuan commented 8 years ago

Sure. Will keep in mind in the future. Thanks.