ggplot2
ggplot2 copied to clipboard
Skip alignment/stacking when possible
This PR aims to fix #5788.
Briefly, when there is only 1 group with unique x variables, stat_align()
+ position_stack()
can be slow. By skipping alignment when there is only 1 group, we go down from ~2 seconds to ~700 milliseconds for 10e4 observations. By skipping stacking when all x positions are unique, we go from ~700 milliseconds to ~70 milliseconds. All in all, I'd say that 1 group, all unique x is common enough to make the skipping worth it.
devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
dat <- data.frame(
x = 1:1e4,
y = rnorm(1e4) + 5
)
area <- ggplot(dat) + geom_area(aes(x, y))
ribbon <- ggplot(dat) + geom_ribbon(aes(x, ymin = 0, ymax = y))
ragg::agg_png(tempfile(fileext = ".png"))
res <- bench::mark(
area = print(area),
ribbon = print(ribbon),
check = FALSE,
min_iterations = 5
)
print(res)
#> # A tibble: 2 × 13
#> expression min median `itr/sec` mem_alloc `gc/sec` n_itr n_gc total_time
#> <bch:expr> <bch:tm> <bch:> <dbl> <bch:byt> <dbl> <int> <dbl> <bch:tm>
#> 1 area 76.5ms 76.8ms 12.1 28.3MB 9.05 4 3 331ms
#> 2 ribbon 74.1ms 75.1ms 13.3 23.6MB 9.94 4 3 302ms
#> # ℹ 4 more variables: result <list>, memory <list>, time <list>, gc <list>
Created on 2024-03-21 with reprex v2.1.0