kaitai_struct_ruby_runtime icon indicating copy to clipboard operation
kaitai_struct_ruby_runtime copied to clipboard

Ruby 2.4.0 introduced `String#unpack1`, which is faster than `String#unpack[0]`

Open generalmimon opened this issue 3 years ago • 1 comments

We're probably not ready to end support for Ruby versions older than 2.4 (yet), but when we are, we could make methods for reading integers faster by switching to String#unpack1 - see https://github.com/fastruby/fast-ruby#stringunpack1-vs-stringunpack0-code:

Comparison:
      String#unpack1:  4864467.2 i/s
    String#unpack[0]:  3777815.6 i/s - 1.29x  slower

According to the above benchmark, String#unpack1 may be 29% faster.

generalmimon avatar Feb 19 '22 12:02 generalmimon

We can do selective method definition based off Ruby version. Let me give it a try.

GreyCat avatar Apr 21 '25 13:04 GreyCat