Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip alignment/stacking when possible #5789

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

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.

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
1 participant