From: "jeremyevans0 (Jeremy Evans) via ruby-core" Date: 2024-07-29T19:40:16+00:00 Subject: [ruby-core:118723] [Ruby master Misc#20652] Memory allocation for gsub has increased from Ruby 2.7 to 3.3 Issue #20652 has been updated by jeremyevans0 (Jeremy Evans). Dan0042 (Daniel DeLorme) wrote in #note-19: > After reading over https://github.com/ruby/ruby/pull/4734/files it seems there's two parts to it. > 1. use a `set_match` pointer to return the match (this fixes the race condition) > 2. always allocate a MatchData, never using `rb_backref_get()` > > But it seems to me that #2 is only necessary if `set_match` is used. So what about using `rb_backref_get()` when possible? Like > ```ruby > match = set_match ? Qnil : rb_backref_get(); > ``` > > @jeremyevans0 wdyt? I'm not sure it is thread-safe. This would modify a shared backref in code paths where `set_match` is `NULL`. I haven't audited the related code, so I'm not sure what code calls `rb_reg_search0` and `rb_reg_match`. Feel free to give it a try and see if passes the test and reduces allocations. ---------------------------------------- Misc #20652: Memory allocation for gsub has increased from Ruby 2.7 to 3.3 https://bugs.ruby-lang.org/issues/20652#change-109258 * 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/