From: "Eregon (Benoit Daloze) via ruby-core" Date: 2024-07-30T11:37:05+00:00 Subject: [ruby-core:118736] [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 It depends in which situation but generally yes it's able to avoid the allocation. If there is no block around, partial escape analysis avoids the allocation of the Ruby MatchData object as long as it's not leaked/stored globally (like `$m = $~`), even if it is accessed in that method. There might be still be an allocation of the internal data structure representing the group offsets, if the Regexp sometimes matches and sometimes not (but tail duplication can fix this in some cases, e.g. if there is not too much code to duplicate). If there is 1/multiple block(s) around the regexp match, then `$~` is stored in the method's frame and not the block's frame and then it's allocated unless there is a compilation covering the method and inlining everything until that block. The case I checked is: ```ruby def foo "a" =~ /(a)/ $1 end loop { foo() } ``` with `cd truffleruby && chruby truffleruby+graalvm-24.0.2 && jt -u ruby graph test.rb` and that shows the only allocation is for a new Ruby String (the return value). ---------------------------------------- Misc #20652: Memory allocation for gsub has increased from Ruby 2.7 to 3.3 https://bugs.ruby-lang.org/issues/20652#change-109275 * 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/