From c4ae884f7ed02f64d08f70b44c38bb87c41c2f6c Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Tue, 2 Mar 2021 17:21:51 -0600 Subject: [PATCH] Try reducing n^2 complexity here This should not time out, but if GC takes a while to process weak references the original code will scan over 50M weakrefs and run full GC 10k times before giving up. This change reduces the total number of weakrefs to 1000 and only scans them with GC.start up to 1000 times. If that is not enough GC to trigger weakref evacuation then it should just fail. --- spec/ruby/library/weakref/fixtures/classes.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spec/ruby/library/weakref/fixtures/classes.rb b/spec/ruby/library/weakref/fixtures/classes.rb index 560c58b041a..041afab14da 100644 --- a/spec/ruby/library/weakref/fixtures/classes.rb +++ b/spec/ruby/library/weakref/fixtures/classes.rb @@ -13,9 +13,11 @@ def self.make_weakref(level = 10) def self.make_dead_weakref weaks = [] weak = nil - 10_000.times do + 1000.times do weaks << make_weakref - GC.start + end + + 1000.times do GC.start break if weak = weaks.find { |w| !w.weakref_alive? } end