[ruby-core:99751] [Ruby master Bug#17030] Enumerable#grep{_v} should be optimized for Regexp
From:
eregontp@...
Date:
2020-08-28 15:01:05 UTC
List:
ruby-core #99751
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 (2.9%) i/s - 12.677M in 5.068796s
grep 2.502M (ア 2.8%) i/s - 12.558M in 5.022837s
select.match? 2.519M (ア 2.6%) i/s - 12.677M in 5.036105s
grep 2.498M (ア 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 error
```
MRI 2.6 for comparison:
```
Calculating -------------------------------------
select.match? 943.017k (ア 0.6%) i/s - 4.770M in 5.058962s
grep 470.844k (ア 0.8%) i/s - 2.389M in 5.074917s
select.match? 944.326k (ア 0.7%) i/s - 4.770M in 5.052020s
grep 471.122k (ア 2.5%) i/s - 2.389M in 5.074969s
Comparison:
select.match?: 944325.5 i/s
grep: 471122.3 i/s - 2.00x (ア 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#===`, which creates useless `MatchData`.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>