-
Notifications
You must be signed in to change notification settings - Fork 2k
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
.data
pronouns slow down plots
#5730
Comments
The delay increases with the number of layers in the plot. Once the namespaces have been loaded, the time increase can be easily more than 20%, even more than 200%. Most of the slow down takes place when each layer is rendered. When profiling plots that use The more complex the plot, the proportionally larger the slow-down seems to be. In the two examples below the median time increases by a factor of about 2.4 and 4 times respectively with some small variation between runs. (If a file is actually being read the slowdown is likely to vary with OS and computer hardware.) library(ggplot2)
p1 <- ggplot(mtcars, aes(hp, mpg, colour = factor(cyl))) +
geom_point()
p2 <- ggplot(mtcars, aes(.data[["hp"]],
.data[["mpg"]],
colour = factor(.data[["cyl"]]))) +
geom_point()
gc()
#> used (Mb) gc trigger (Mb) max used (Mb)
#> Ncells 841786 45.0 1432812 76.6 1432812 76.6
#> Vcells 1446284 11.1 8388608 64.0 2317017 17.7
bench::mark(ggplotGrob(p1),
ggplotGrob(p2),
check = FALSE,
min_iterations = 10
)
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 ggplotGrob(p1) 50.8ms 51.7ms 19.4 10.54MB 12.9
#> 2 ggplotGrob(p2) 121.2ms 121.2ms 8.25 9.13MB 74.2
p3 <- ggplot(mtcars, aes(hp, mpg, colour = factor(cyl), label = am)) +
geom_point() +
geom_text()
p4 <- ggplot(mtcars, aes(.data[["hp"]],
.data[["mpg"]],
colour = factor(.data[["cyl"]]),
label = .data[["am"]])) +
geom_point() +
geom_text()
gc()
#> used (Mb) gc trigger (Mb) max used (Mb)
#> Ncells 1151472 61.5 2304648 123.1 2304648 123.1
#> Vcells 2053219 15.7 8388608 64.0 7469216 57.0
bench::mark(ggplotGrob(p3),
ggplotGrob(p4),
check = FALSE,
min_iterations = 10
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
#> # A tibble: 2 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 ggplotGrob(p3) 59.5ms 63.3ms 15.9 444.89KB 7.93
#> 2 ggplotGrob(p4) 241.7ms 246.7ms 3.77 2.19MB 6.78 Created on 2024-02-29 with reprex v2.1.0 |
Just for completeness, the reprex below shows that library(ggplot2)
p5 <- ggplot(mtcars, aes(hp, mpg, colour = factor(vs), label = am)) +
geom_point() +
geom_text() +
facet_wrap(facets = vars(cyl))
p6 <- ggplot(mtcars, aes(hp, mpg, colour = factor(vs), label = am)) +
geom_point() +
geom_text() +
facet_wrap(facets = vars(.data[["cyl"]]))
p7 <- ggplot(mtcars, aes(hp, mpg, colour = factor(vs), label = am)) +
geom_point() +
geom_text() +
facet_wrap(facets = vars(factor(.data[["cyl"]])))
gc()
#> used (Mb) gc trigger (Mb) max used (Mb)
#> Ncells 864372 46.2 1435730 76.7 1435730 76.7
#> Vcells 1480113 11.3 8388608 64.0 2354795 18.0
bench::mark(ggplotGrob(p5),
ggplotGrob(p6),
ggplotGrob(p7),
check = FALSE,
min_iterations = 10
)
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
#> # A tibble: 3 × 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 ggplotGrob(p5) 124ms 129ms 7.34 16.23MB 6.61
#> 2 ggplotGrob(p6) 130ms 131ms 7.44 1.58MB 7.44
#> 3 ggplotGrob(p7) 131ms 135ms 6.92 522.75KB 6.92 Created on 2024-02-29 with reprex v2.1.0 |
Found the culprit: see #5731 for diagnosis. |
This issue is separated from #5729.
Using the
.data
pronoun is about 20% slower than use normal aesthetics, which seems unreasonable to me.All the slowdown is in the build stage, not the gtable stage.
Created on 2024-02-29 with reprex v2.1.0
The text was updated successfully, but these errors were encountered: