summaryrefslogtreecommitdiff
path: root/test/ruby
diff options
context:
space:
mode:
authorJean Boussier <byroot@ruby-lang.org>2024-02-29 13:17:22 +0100
committerJean Boussier <jean.boussier@gmail.com>2024-03-01 08:23:38 +0100
commite626da82eae3d437b84d4f9ead0164d436b08e1a (patch)
tree058421f46c3fe9eb0b1f1445cb3f4f0c902127f7 /test/ruby
parent5d76fe6b2a413c71374c9f799c7a1023e2002457 (diff)
Don't pin named structs defined in Ruby
[Bug #20311] `rb_define_class_under` assumes it's called from C and that the reference might be held in a C global variable, so it adds the class to the VM root. In the case of `Struct.new('Name')` it's wasteful and make the struct immortal.
Diffstat (limited to 'test/ruby')
-rw-r--r--test/ruby/test_struct.rb14
1 files changed, 14 insertions, 0 deletions
diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb
index ed750b91f7..a451e400cb 100644
--- a/test/ruby/test_struct.rb
+++ b/test/ruby/test_struct.rb
@@ -534,6 +534,20 @@ module TestStruct
assert_equal [[:req, :_]], klass.instance_method(:c=).parameters
end
+ def test_named_structs_are_not_rooted
+ # [Bug #20311]
+ assert_no_memory_leak([], <<~PREP, <<~CODE, rss: true)
+ code = proc do
+ Struct.new("A")
+ Struct.send(:remove_const, :A)
+ end
+
+ 1_000.times(&code)
+ PREP
+ 300_000.times(&code)
+ CODE
+ end
+
class TopStruct < Test::Unit::TestCase
include TestStruct