[前][次][番号順一覧][スレッド一覧]

ruby-changes:62361

From: Jean <ko1@a...>
Date: Thu, 23 Jul 2020 10:52:48 +0900 (JST)
Subject: [ruby-changes:62361] 6a0cb1d649 (master): Avoid allocating a string when dumping an anonymous module or class

https://git.ruby-lang.org/ruby.git/commit/?id=6a0cb1d649

From 6a0cb1d649ecfc3e2af922c74ce82b3ff95fb12a Mon Sep 17 00:00:00 2001
From: Jean Boussier <jean.boussier@g...>
Date: Wed, 22 Jul 2020 12:47:14 +0200
Subject: Avoid allocating a string when dumping an anonymous module or class


diff --git a/ext/objspace/objspace_dump.c b/ext/objspace/objspace_dump.c
index 95bd8ac..2659404 100644
--- a/ext/objspace/objspace_dump.c
+++ b/ext/objspace/objspace_dump.c
@@ -268,8 +268,11 @@ dump_object(VALUE obj, struct dump_config *dc) https://github.com/ruby/ruby/blob/trunk/ext/objspace/objspace_dump.c#L268
 
       case T_CLASS:
       case T_MODULE:
-	if (dc->cur_obj_klass)
-	    dump_append(dc, ", \"name\":\"%s\"", rb_class2name(obj));
+	if (dc->cur_obj_klass) {
+	    VALUE mod_name = rb_mod_name(obj);
+	    if (!NIL_P(mod_name))
+		dump_append(dc, ", \"name\":\"%s\"", RSTRING_PTR(mod_name));
+	}
 	break;
 
       case T_DATA:
diff --git a/test/objspace/test_objspace.rb b/test/objspace/test_objspace.rb
index 336b30f..de27058 100644
--- a/test/objspace/test_objspace.rb
+++ b/test/objspace/test_objspace.rb
@@ -503,4 +503,31 @@ class TestObjSpace < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/objspace/test_objspace.rb#L503
     assert_equal h[:immortal_symbol], h[:immortal_dynamic_symbol] + h[:immortal_static_symbol], m
     ;;;
   end
+
+  def test_dump_allocations
+    object = Object.new
+    assert_allocations_count(3) { ObjectSpace.dump(object) }
+  end
+
+  def test_anonymous_class_name
+    klass = Class.new
+    assert_allocations_count(4) { ObjectSpace.dump(klass) }
+    assert_allocations_count(3) { ObjectSpace.dump(klass) }
+
+    mod = Module.new
+    assert_allocations_count(3) { ObjectSpace.dump(mod) }
+
+    assert_not_include ObjectSpace.dump(Class.new), '"name"'
+    assert_not_include ObjectSpace.dump(Module.new), '"name"'
+  end
+
+  private
+
+  def assert_allocations_count(count)
+    ObjectSpace.dump(Object.new) # warming up
+
+    before = GC.stat(:total_allocated_objects)
+    yield
+    assert_equal count, GC.stat(:total_allocated_objects) - before
+  end
 end
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]