Issue #17030 has been updated by Eregon (Benoit Daloze).


This is something that a JIT with inlining and escape analysis can optimize=
 and always be correct.
Static analysis doesn't cut it for Ruby.

On TruffleRuby (master + a fix I'll merge soon) for the benchmark above:
```
Calculating -------------------------------------
       select.match?      2.503M (=B1 2.9%) i/s -     12.677M in   5.068796s
                grep      2.502M (=B1 2.8%) i/s -     12.558M in   5.022837s
       select.match?      2.519M (=B1 2.6%) i/s -     12.677M in   5.036105s
                grep      2.498M (=B1 2.2%) i/s -     12.558M in   5.030485s

Comparison:
       select.match?:  2518943.6 i/s
                grep:  2497618.0 i/s - same-ish: difference falls within er=
ror

```

MRI 2.6 for comparison:
```
Calculating -------------------------------------
       select.match?    943.017k (=B1 0.6%) i/s -      4.770M in   5.058962s
                grep    470.844k (=B1 0.8%) i/s -      2.389M in   5.074917s
       select.match?    944.326k (=B1 0.7%) i/s -      4.770M in   5.052020s
                grep    471.122k (=B1 2.5%) i/s -      2.389M in   5.074969s

Comparison:
       select.match?:   944325.5 i/s
                grep:   471122.3 i/s - 2.00x  (=B1 0.00) slower
```

----------------------------------------
Bug #17030: Enumerable#grep{_v} should be optimized for Regexp
https://bugs.ruby-lang.org/issues/17030#change-87251

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Currently,

```ruby
array.select { |e| e.match?(REGEXP) }
```

is about three times faster and six times more memory efficient than

```ruby
array.grep(REGEXP)
```

This is because `grep` calls `Regexp#=3D=3D=3D`, which creates useless `Mat=
chData`.



-- =

https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=3Dunsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>