ruby/ext/strscan/extconf.rb
Sutou Kouhei e73e4f2d4c [ruby/strscan] [Feature #21943] Add StringScanner#integer_at
(https://github.com/ruby/strscan/pull/205)

See also: https://bugs.ruby-lang.org/issues/21943

This is semantically equivalent to `scanner[specifier]&.to_i(base)` but
this is faster than `scanner[specifier]&.to_i(base)` because
`integer_at` doesn't create a temporary String when possible.

This PR also includes a benchmark for them:

```console
$ ruby -v -S benchmark-driver benchmark/integer_at.yaml
ruby 4.1.0dev (2026-05-01T19:25:51Z master https://github.com/ruby/strscan/commit/f2845eab29) +PRISM [x86_64-linux]
Warming up --------------------------------------
             [].to_i    24.272M i/s -     25.109M times in 1.034481s (41.20ns/i, 32clocks/i)
          integer_at    61.188M i/s -     62.491M times in 1.021289s (16.34ns/i, 62clocks/i)
Calculating -------------------------------------
             [].to_i    26.831M i/s -     72.816M times in 2.713883s (37.27ns/i, 169clocks/i)
          integer_at    81.331M i/s -    183.564M times in 2.256998s (12.30ns/i, 43clocks/i)

Comparison:
          integer_at:  81331225.5 i/s
             [].to_i:  26831046.3 i/s - 3.03x  slower
```

In this environment, `integer_at` is 3.03x faster than `[].to_i`.

https://github.com/ruby/strscan/commit/8a60879b2d

Co-authored-by: jinroq <jinroq@gmail.com>
2026-06-01 07:55:02 +00:00

15 lines
522 B
Ruby

# frozen_string_literal: true
require 'mkmf'
if RUBY_ENGINE == 'ruby'
$INCFLAGS << " -I$(top_srcdir)" if $extmk
have_func("onig_region_memsize(NULL)")
have_func("rb_reg_onig_match", "ruby/re.h")
have_func("rb_deprecate_constant")
have_func("rb_int_parse_cstr", "ruby.h") # RUBY_VERSION >= 2.5
have_func("rb_gc_location", "ruby.h") # RUBY_VERSION >= 2.7
have_const("RUBY_TYPED_EMBEDDABLE", "ruby.h") # RUBY_VERSION >= 3.3
create_makefile 'strscan'
else
File.write('Makefile', dummy_makefile("").join)
end