Skip to content

Switch digest::digest() to rlang::hash()? #4458

Closed
@yutannihilation

Description

@yutannihilation

As of version 0.4.10, rlang has hash(). We previously discussed about the alternative of digest::digest() (c.f. #4247 (comment)), and this seems the one, while at the moment we don't have strong motivation to migrate. I don't want to include this in the next release, but let's revisit here when it's done. I think rlang::hash() is good in that

  • it seems faster than digest::digest() for our usages, and
  • we'll have less dependencies since we already have rlang in Imports.

Here's the benchmark:

library(base64enc)
library(rlang)
library(digest)

x <- list(
  "z",
  as.character(1:6),
  data.frame(
    colour = scales::viridis_pal()(300),
    value  = seq(1, 6, length.out = 300)
  ),
  "colourbar"
)

microbenchmark::microbenchmark(
  digest_w_colon  = digest::digest(x),
  digest_wo_colon = digest(x),
  digest_xxhash32 = digest::digest(x, "xxhash32"),
  digest_xxhash64 = digest::digest(x, "xxhash64"),
  serialize_ascii = serialize(x, NULL, TRUE),
  serialize_bin   = serialize(x, NULL, FALSE),
  rawToChar       = rawToChar(serialize(x, NULL, TRUE)),
  rlang_w_colon   = rlang::hash(x),
  rlang_wo_colon  = hash(x)
)
#> Unit: microseconds
#>             expr     min       lq      mean   median       uq      max neval
#>   digest_w_colon 113.855 123.9700 132.96479 128.3845 136.9935  224.025   100
#>  digest_wo_colon 101.118 112.5620 123.06313 116.9640 125.2750  197.081   100
#>  digest_xxhash32  85.048  96.9555 111.56161 101.8455 113.0255  699.039   100
#>  digest_xxhash64  86.641  97.4070 109.75329 103.4085 110.7890  223.573   100
#>  serialize_ascii 822.214 840.2070 872.77923 847.3240 867.1200 1448.936   100
#>    serialize_bin  47.652  54.0820  58.62599  56.6065  62.0685   95.946   100
#>        rawToChar 869.960 886.7930 909.63336 890.8435 900.6660 1288.549   100
#>    rlang_w_colon  45.479  52.5935  57.35674  54.9265  59.8355   98.420   100
#>   rlang_wo_colon  34.342  38.1960  41.78910  39.8310  42.0270  156.552   100
#>    cld
#>    c  
#>   bc  
#>   b   
#>   b   
#>     d 
#>  a    
#>      e
#>  a    
#>  a

Created on 2021-04-30 by the reprex package (v2.0.0)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions