
26 Jul
2024
26 Jul
'24
4:23 a.m.
Issue #20652 has been updated by byroot (Jean Boussier).
it could be better if we had a gsub variant that doesn't touch $~.
Right, but the problem is beyond `gsub`, e.g.: ```ruby
"abba"[/(bb|[^b]{2})/] => "bb" $~ => #<MatchData "bb" 1:"bb">
So introducing `gsub( backref: false)` or `gsub_no_backref` would solve one case but would leave many others.
Hence why I think the only way to handle this general problem without introducing tons of new methods and lots of code churn would be a new Regexp flag.
----------------------------------------
Misc #20652: Memory allocation for gsub has increased from Ruby 2.7 to 3.3
https://bugs.ruby-lang.org/issues/20652#change-109228
* 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/