[ruby-core:99708] [Ruby master Bug#17030] Enumerable#grep{_v} should be optimized for Regexp
From:
marcandre-ruby-core@...
Date:
2020-08-26 17:30:23 UTC
List:
ruby-core #99708
Issue #17030 has been updated by marcandre (Marc-Andre Lafortune).
fatkodima (Dima Fatko) wrote in #note-13:
> In many cases, probably yes, but again, `case-when`, when arguments/consts/etc instead of local vars are used - it is hard to tell if them are regexes or not.
That's not really what I'm proposing. I'm proposing something like an internal `Regexp.needs_last_match?` that would return `true` or `false` depending on the Ruby code, and that could be used to optimize methods. It would return `true` if any subsequent code could be impacted by `$~` and al.
```ruby
def foo
/x/ =~ 'x' # needs_last_match? # => false
case method
when /(foo)/ # needs_last_match? # => false
do_something
when /(bar)/ # needs_last_match? # => true
puts $2
# ... # needs_last_match? # => false
end
end
def bar
# ... # needs_last_match? # => true
case x
when /(foo)/ # needs_last_match? # => true
do_something
end
Regexp.last_match
# ... # needs_last_match? # => false (false negative)
Regexp.send :last_match
# ... # needs_last_match? # => false (false negative)
const_get(:Regexp).last_match
end
```
----------------------------------------
Bug #17030: Enumerable#grep{_v} should be optimized for Regexp
https://bugs.ruby-lang.org/issues/17030#change-87199
* 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>