Skip to content

Conversation

teunbrand
Copy link
Collaborator

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

Comment on lines +51 to +54
x = c(0, 1, 1000, 1001, 0, 1, 1000, 1001),
y = c(-1, 1, -1, 1, -1, 1, -1, 1),
f = c("A", "A", "B", "B", "A", "A", "B", "B"),
g = c("a", "a", "b", "b", "c", "c", "d", "d")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to ensure >=2 groups per panel here for this test to test the thing it is supposed to test.

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand teunbrand merged commit 0b348cd into tidyverse:main May 20, 2024
@teunbrand teunbrand deleted the skip_alignment branch May 20, 2024 12:20
@teunbrand teunbrand mentioned this pull request Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

geom_ribbon() > geom_area() speed difference

2 participants