Skip to content

Performance: encode_native() does not need an intermediate character vector#37

Open
zeehio wants to merge 10 commits intothomasp85:mainfrom
zeehio:encode-native-avoid-character
Open

Performance: encode_native() does not need an intermediate character vector#37
zeehio wants to merge 10 commits intothomasp85:mainfrom
zeehio:encode-native-avoid-character

Conversation

@zeehio
Copy link

@zeehio zeehio commented Sep 18, 2022

This pull request is applied on top of #36 . Please review that first.

We avoid the intermediate character vector when encoding to native. This makes encode_native() much faster.

This is part of tidyverse/ggplot2#4989, aiming to improve the performance of rasterizing large matrices.

library(farver)
x <- runif(1E7)
ramp <- scales::colour_ramp(c("red", "blue"))
colours_chr <- ramp(x)
spectrum <- farver::decode_colour(colours_chr)

bench::mark(
  before = {
    # This was how encode_native worked before
    cols <- encode_colour(spectrum, alpha = 0.75)
    farver:::encode_native(cols)
  },
  after = {
    farver::encode_native(spectrum,alpha = 0.75)
  }
)
#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 before        1.16s    1.16s     0.866   114.5MB     0   
#> 2 after       83.53ms  86.04ms    11.7      38.1MB     2.34

Created on 2022-09-18 by the reprex package (v2.0.1)

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.

1 participant