[ruby-core:118689] [Ruby master Misc#20652] Memory allocation for gsub has increased from Ruby 2.7 to 3.3
From:
"byroot (Jean Boussier) via ruby-core" <ruby-core@...>
Date:
2024-07-26 06:15:14 UTC
List:
ruby-core #118689
Issue #20652 has been updated by byroot (Jean Boussier).
> If anyone can come up with an approach that fixes #17507 without causing an increase in memory usage
I guess I'm a bit surprised by that because I wouldn't have thought the backref is only accessible by the same fiber, but maybe I'm missing something.
But on a more general note, I very very often wish I'd have a way to not have a `MatchData` created by methods that take a Regexp, it's very rare you need the `MatchData` created by `gsub` or `Regexp===`. Most of the time it's fine, but when you are trying to optimize a hotspot, it's really something you'd want to be able to skip. It's in big part why `Regexp#match?` was added and popularized, but is only usable in a subset of cases.
Maybe we could add a new Regexp flag to turn off this behavior?
e.g.
```ruby
case str
when /^a/
p $~ # => #<MatchData "a">
when /^b/c
p $~ # => nil
end
```
----------------------------------------
Misc #20652: Memory allocation for gsub has increased from Ruby 2.7 to 3.3
https://bugs.ruby-lang.org/issues/20652#change-109225
* 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/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/