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

ruby-changes:65560

From: Marc-Andre <ko1@a...>
Date: Thu, 18 Mar 2021 20:35:14 +0900 (JST)
Subject: [ruby-changes:65560] 0ef2923c2b (master): Avoid rehashing in Hash#replace/dup/initialize_copy [Bug #16996]

https://git.ruby-lang.org/ruby.git/commit/?id=0ef2923c2b

From 0ef2923c2b9afb76b3a1cebc56ebabc71c9a2ba8 Mon Sep 17 00:00:00 2001
From: Marc-Andre Lafortune <github@m...>
Date: Mon, 15 Mar 2021 22:15:03 -0400
Subject: Avoid rehashing in Hash#replace/dup/initialize_copy [Bug #16996]

---
 hash.c                 |  2 +-
 test/ruby/test_hash.rb | 10 +++-------
 test/test_set.rb       |  7 +++----
 3 files changed, 7 insertions(+), 12 deletions(-)

diff --git a/hash.c b/hash.c
index 2dd61dc..e9a3ca9 100644
--- a/hash.c
+++ b/hash.c
@@ -2958,7 +2958,7 @@ rb_hash_replace(VALUE hash, VALUE hash2) https://github.com/ruby/ruby/blob/trunk/hash.c#L2958
             RHASH_TBL_RAW(hash)->type = RHASH_ST_TABLE(hash2)->type;
         }
     }
-    rb_hash_foreach(hash2, rb_hash_rehash_i, (VALUE)hash);
+    hash_copy(hash, hash2);
 
     rb_gc_writebarrier_remember(hash);
 
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb
index 25dfaf4..3458053 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -113,13 +113,8 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L113
     assert_equal(2, h[1])
   end
 
-  def test_dup_will_rehash
-    set1 = @cls[]
-    set2 = @cls[set1 => true]
-
-    set1[set1] = true
-
-    assert_equal set2, set2.dup
+  def test_dup_will_not_rehash
+    assert_hash_does_not_rehash(&:dup)
   end
 
   def assert_hash_does_not_rehash
@@ -480,6 +475,7 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L475
     h1 = @cls[h => 1]
     assert_equal(h1, h1.dup)
     h[1] = 2
+    h1.rehash
     assert_equal(h1, h1.dup)
   end
 
diff --git a/test/test_set.rb b/test/test_set.rb
index e62f30d..af9806e 100644
--- a/test/test_set.rb
+++ b/test/test_set.rb
@@ -637,11 +637,10 @@ class TC_Set < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_set.rb#L637
     assert_not_equal(Set[1], [1])
 
     set1 = Class.new(Set)["a", "b"]
-    set2 = Set["a", "b", set1]
-    set1 = set1.add(set1.clone)
+    set1.add(set1).reset # Make recursive
+    set2 = Set["a", "b", Set["a", "b", set1]]
 
-    assert_equal(set2, set2.clone)
-    assert_equal(set1.clone, set1)
+    assert_equal(set1, set2)
 
     assert_not_equal(Set[Exception.new,nil], Set[Exception.new,Exception.new], "[ruby-dev:26127]")
   end
-- 
cgit v1.1


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

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