[ruby-core:99713] [Ruby master Bug#17030] Enumerable#grep{_v} should be optimized for Regexp
From:
daniel@...42.com
Date:
2020-08-26 19:17:58 UTC
List:
ruby-core #99713
Issue #17030 has been updated by Dan0042 (Daniel DeLorme).
> Couldn't static analysis of the code determine in most cases if match data need be generated or not?
scivola20 had a good idea, but this is even better. We can automatically get the best performance without having to manually optimize each case.
But static analysis has other limits besides `const_get(:Regexp).last_match`
```ruby
def foo(v)
/x/ =~ 'x' # needs_last_match? depends on whether 'v' is regexp
case method
when v
$1
end
end
```
So a simpler approach would be to check if the match operation's scope (in this case the method body) contains any of the regexp-related pseudo-globals.
----------------------------------------
Bug #17030: Enumerable#grep{_v} should be optimized for Regexp
https://bugs.ruby-lang.org/issues/17030#change-87203
* 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) }
# about 3x faster and 6x more memory efficient than
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>