digest icon indicating copy to clipboard operation
digest copied to clipboard

Digest is ~5x slower on Ruby 3.x then 2.x

Open baseballlover723 opened this issue 4 years ago • 5 comments

Digest is ~5x slower for SHA1, SHA2, and SHA256 on Ruby 3.x compared to Ruby 2.x. See https://gist.github.com/morimori/1330095#gistcomment-3975819 for more details and the benchmark. Note that I tried benchmarking with Ruby 3.0.3 with both Digest 3.0.0 and 3.1.0, as well as Ruby 3.1.0 and still had the same performance. I suspect that it has to do with the extraction of Digest into its own gem.

baseballlover723 avatar Dec 29 '21 23:12 baseballlover723

@rhenium Can you look this?

This performance regression may be intentional. Should we migrate it to like OpenSSL::Digest::SHA256?

hsbt avatar Jan 05 '22 05:01 hsbt

Heres a gist of what I was personally using to benchmark: https://gist.github.com/baseballlover723/c02a6f803ac4147585d183d0430b205e. It might be useful to you all.

baseballlover723 avatar Jan 07 '22 04:01 baseballlover723

My guess would be you get Ruby 3 level of performance (significantly slower) on Ruby 2 if you pass the --with-bundled-md5 --with-bundled-sha1 --with-bundled-sha2 options when configuring. This is because the extracted digest library doesn't appear to use the openssl digest versions if they are available (which Ruby 2 did to improve performance). Compare the ruby 2.7 digest_conf.rb file to the one in this extracted gem, and the code to use the optimized openssl implementation is missing: https://github.com/ruby/ruby/blob/v2_7_5/ext/digest/digest_conf.rb#L66-L80

The only code I can see in the current digest repository that uses openssl is the gemspec: spec.metadata["msys2_mingw_dependencies"] = "openssl", which I'm guessing may be a mistake.

jeremyevans avatar Jan 07 '22 05:01 jeremyevans

Yes, this is likely caused by https://github.com/ruby/ruby/pull/3149.

The performance degradation is not deliberate but somewhat expected because it now uses the bundled C implementation instead of OpenSSL's optimized implementation.

rhenium avatar Jan 07 '22 07:01 rhenium

So people who want the higher performance of the Ruby 2.x version on ruby 3.x should just switch from using digest to openssl to keep using the same implementation as the Ruby 2.x version.

baseballlover723 avatar Jan 08 '22 04:01 baseballlover723