Project

General

Profile

Actions

Misc #20652

open

Memory allocation for gsub has increased from Ruby 2.7 to 3.3

Added by orisano (Nao Yonashiro) about 10 hours ago. Updated about 6 hours ago.


Description

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:

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?


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #17507: Regexp capture groups ignored sometimes in some multithreaded environments (possible race condition)ClosedActions
Actions #1

Updated by mame (Yusuke Endoh) about 9 hours ago

  • Related to Bug #17507: Regexp capture groups ignored sometimes in some multithreaded environments (possible race condition) added

Updated by mame (Yusuke Endoh) about 9 hours ago

  • Assignee set to jeremyevans0 (Jeremy Evans)

@jeremyevans0 (Jeremy Evans) What do you think?

Updated by jeremyevans0 (Jeremy Evans) about 6 hours ago

It's expected that fixing #17507 caused memory usage to increase. If anyone can come up with an approach that fixes #17507 without causing an increase in memory usage, please submit a pull request for it.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0