From: "Eregon (Benoit Daloze) via ruby-core" Date: 2024-07-27T11:05:37+00:00 Subject: [ruby-core:118710] [Ruby master Misc#20652] Memory allocation for gsub has increased from Ruby 2.7 to 3.3 Issue #20652 has been updated by Eregon (Benoit Daloze). byroot (Jean Boussier) wrote in #note-9: > I see what you mean, but such flag would only really be worth using in places where saving that allocation is worth it, where right now you usually use a literal anyway, so I don't think duplication would be a concern. I'm thinking cases of Regexps being stored in constants and potentially composed of other regexps/strings, like https://github.com/ruby/uri/blob/master/lib/uri/rfc3986_parser.rb does it for example. It seems bad to duplicate some of these regexps if (for the same large Regexp) we have some call sites which need the MatchData and some which don't. Also `Regexp#match` (which returns a MatchData) would make no sense with that flag, so it feels the wrong place to specify it. Regarding `gsub` specifically, I think it shouldn't set `$~`, i.e. only set it if a block is passed. For example with `"abc".gsub("a", "d")` I don't see any point to set `$~` after it. That's potential incompatible, but we could warn for a release or so and I'd think few gems rely on that. ---------------------------------------- Misc #20652: Memory allocation for gsub has increased from Ruby 2.7 to 3.3 https://bugs.ruby-lang.org/issues/20652#change-109245 * 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/