Project

General

Profile

Actions

Bug #20104

closed

Regexp#match returns nil but allocates T_MATCH objects

Added by jeremyevans0 (Jeremy Evans) 5 months ago. Updated 4 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

Actions

Also available in: Atom PDF

Like1
Like0Like1Like0Like0