Project

General

Profile

Actions

Feature #17206

open

Introduce new Regexp option to avoid global MatchData allocations

Added by fatkodima (Dima Fatko) about 4 years ago. Updated about 4 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:100239]

Description

Originates from https://bugs.ruby-lang.org/issues/17030

When this option is specified, ruby will not create global MatchData objects, when not explicitly needed by the method.

If the new option is named f, we can write as /o/f, and grep(/o/f) is faster than grep(/o/).

This speeds up not only grep, but also all?, any?, case and so on.

Many people have written code like this:

IO.foreach("foo.txt") do |line|
  case line
  when /^#/
    # do nothing 
  when /^(\d+)/
    # using $1
  when /xxx/
    # using $&
  when /yyy/
    # not using $&
  else
    # ...
  end
end

This is slow, because of the above mentioned problem.
Replacing /^#/ with /^#/f, and /yyy/ with /yyy/f will make it faster.

Some benchmarks - https://bugs.ruby-lang.org/issues/17030#note-9 which show 2.5x to 5x speedup.

PR: https://github.com/ruby/ruby/pull/3455

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0