Project

General

Profile

Actions

Bug #20104

closed

Regexp#match returns nil but allocates T_MATCH objects

Added by jeremyevans0 (Jeremy Evans) 10 months ago. Updated 9 months ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.4.0dev (2023-12-30T03:14:38Z master 8e32c01742) [x86_64-openbsd7.4]
[ruby-core:115978]

Description

Between Ruby 3.2 and 3.3, behavior changed so that Regexp#match will allocate a T_MATCH object even when there is no match. Example code:

h = {}
GC.start
GC.disable
ObjectSpace.count_objects(h)
matches = h[:T_MATCH] || 0
md = /\A[A-Z]+\Z/.match('1')
ObjectSpace.count_objects(h)
new_matches = h[:T_MATCH] || 0
puts "/\\A[A-Z]+\\Z/.match('1') => #{md.inspect} generates #{new_matches - matches} T_MATCH objects"

Result with Ruby 1.9-3.2:

/\A[A-Z]+\Z/.match('1') => nil generates 0 T_MATCH objects

Results with Ruby 3.3.0 and current master branch:

/\A[A-Z]+\Z/.match('1') => nil generates 1 T_MATCH objects

This results in a measurable performance decrease for both Sinatra and Roda web applications, as reported at: https://old.reddit.com/r/ruby/comments/18sxtv9/ruby_330_performance_ups_and_downs/

Thanks to GitHub users kiskoza and tagliala for producing a minimal example showing this issue: https://github.com/caxlsx/caxlsx/issues/336

Updated by luke-gru (Luke Gruber) 10 months ago

So I looked into this and it's this commit that introduces that behavior: 7193b404a1a56e50f8046d0382914907020c1559

Edit: create PR here https://github.com/ruby/ruby/pull/9398. I should probably add test for this, I'm just seeing if CI passes first.

Updated by luke-gru (Luke Gruber) 10 months ago

So my PR has been merged but I believe a backport is desirable for 3.3. I'm not sure who to ping for a backport request. Thanks!

Updated by jeremyevans0 (Jeremy Evans) 10 months ago

  • Status changed from Open to Closed

This is already marked for backport to 3.3, it just needs to be closed.

Updated by naruse (Yui NARUSE) 9 months ago

  • Backport changed from 3.0: DONTNEED, 3.1: DONTNEED, 3.2: DONTNEED, 3.3: REQUIRED to 3.0: DONTNEED, 3.1: DONTNEED, 3.2: DONTNEED, 3.3: DONE

ruby_3_3 f585171a6b1d6c20b3c162fd59dc874510ed2a49 merged revision(s) e12d4c654e3cb7a4473014610bc3bae41aaf811e.

Actions

Also available in: Atom PDF

Like1
Like0Like1Like0Like0