
Issue #20652 has been updated by Dan0042 (Daniel DeLorme). byroot (Jean Boussier) wrote in #note-4:
Maybe we could add a new Regexp flag to turn off this behavior?
My first reaction was "Yes! This is exactly was we need!" but after thinking more it feels un-rubyish. We shouldn't have to write code to micro-tweak the performance like that. Ideally the interpreter/jit should handle micro-optimizations. I'd be happier to see something like: ```ruby def foo(str) str if str =~ /rx/ #in this method we don't use $~ and friends, so the interpreter doesn't have to allocate MatchData #yes in theory there's an incompatibility with eval, but in practice I believe that's a non-issue (I'm open to be shown otherwise) end ``` shyouhei (Shyouhei Urabe) wrote in #note-5:
Nobody thinks `gsub` modifies `$~` behind the scene.
I'm not sure this is what you meant, but I definitely expect ANY regexp operation to modify `$~` (except where documented otherwise, like `Regexp#match?`) BTW this includes String#sub; I sometimes write code like: `name, prefix = str.sub(/^(the) /i), $1` ---------------------------------------- Misc #20652: Memory allocation for gsub has increased from Ruby 2.7 to 3.3 https://bugs.ruby-lang.org/issues/20652#change-109255 * Author: orisano (Nao Yonashiro) * Status: Open * Assignee: jeremyevans0 (Jeremy Evans) ---------------------------------------- I recently upgraded from ruby 2.7.7 to 3.3.1 and noticed that the GC load increased. When I used the allocation profiler to investigate, I found that memory allocation from gsub had increased. The problem was code like this: ```ruby s = "foo " s.gsub(/ (\s+)/) { " #{' ' * Regexp.last_match(1).length}" } ``` When I compared the results of heap-profiler between 2.7.7 and 3.3.1, I found that MatchData was increasing. https://gist.github.com/orisano/98792dee260106e9b6fcb45bbabeb1e6 https://github.com/ruby/ruby/commit/abc0304cb28cb9dcc3476993bc487884c139fd11 I discovered that the cause is this commit, which stopped reusing backref to avoid race conditions. Is there a way to reuse backref while still avoiding race conditions? -- https://bugs.ruby-lang.org/