From: eregontp@... Date: 2020-09-30T18:18:26+00:00 Subject: [ruby-core:100242] [Ruby master Feature#17206] Introduce new Regexp option to avoid global MatchData allocations Issue #17206 has been updated by Eregon (Benoit Daloze). IMHO hardcoding such knowledge in the pattern feels wrong (vs in the matching method like `Regexp#match?` which is fine). It seems to me that it could cause confusing bugs, e.g. when using `/f` in the `case` above if a `when` clause starts to use one of the `$~`-derived variables. Then it would unexpectedly always be `nil`, causing a potentially very subtle bug. I have a hard time to believe that allocating the MatchData is so expensive. If that's the case, then there must be a lot of optimization potential for faster allocation of MatchData in CRuby. What I think rather is this is due to having to set $~ in the caller, and maybe to compute group offsets. I think it would be worth investigating more in details where does the performance overhead from `$~` & friends come from in CRuby. ---------------------------------------- Feature #17206: Introduce new Regexp option to avoid global MatchData allocations https://bugs.ruby-lang.org/issues/17206#change-87829 * Author: fatkodima (Dima Fatko) * Status: Open * Priority: Normal ---------------------------------------- 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: ```ruby 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 -- https://bugs.ruby-lang.org/ Unsubscribe: