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

ruby-changes:55502

From: Aaron <ko1@a...>
Date: Wed, 24 Apr 2019 21:24:44 +0900 (JST)
Subject: [ruby-changes:55502] Aaron Patterson:75061f46ae (trunk): Fix complex hash keys to work with co=mpaction=

Aaron Patterson	2019-04-24 04:10:52 +0900 (Wed, 24 Apr 2019)

  New Revision: 75061f46ae

  https://git.ruby-lang.org/ruby.git/commit/?id=3D75061f46ae

  Log:
    Fix complex hash keys to work with compaction
    =

    For example when an array containing objects is a hash key, the conte=
nts
    of the array may move which can cause the hash value for the array to=

    change.   This commit makes the default `hash` value based off the
    object id, so the hash value will remain stable.
    =

    Fixes test/shell/test_command_processor.rb

  Modified files:
    hash.c
    test/ruby/test_gc_compact.rb=
From 75061f46ae646e821e9228caa5e3b7f23fa609f0 Mon Sep 17 00:00:00 2001
From: Aaron Patterson <tenderlove@r...>
Date: Tue, 23 Apr 2019 12:10:52 -0700
Subject: Fix complex hash keys to work with compaction

For example when an array containing objects is a hash key, the contents
of the array may move which can cause the hash value for the array to
change.   This commit makes the default `hash` value based off the
object id, so the hash value will remain stable.

Fixes test/shell/test_command_processor.rb

diff --git a/hash.c b/hash.c
index 000c39e..b4b0415 100644
--- a/hash.c
+++ b/hash.c
@@ -272,7 +272,11 @@ rb_objid_hash(st_index_t index) https://github.com/ruby/ruby/blob/trunk/#L272
 static st_index_t
 objid_hash(VALUE obj)
 {
-    return (st_index_t)st_index_hash((st_index_t)obj);
+#if SIZEOF_LONG == SIZEOF_VOIDP
+    return (st_index_t)st_index_hash((st_index_t)NUM2LONG(rb_obj_id(obj)));
+#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
+    return (st_index_t)st_index_hash((st_index_t)NUM2LL(rb_obj_id(obj)));
+#endif
 }
 
 VALUE
diff --git a/test/ruby/test_gc_compact.rb b/test/ruby/test_gc_compact.rb
index a1e037b..b705f60 100644
--- a/test/ruby/test_gc_compact.rb
+++ b/test/ruby/test_gc_compact.rb
@@ -121,4 +121,11 @@ class TestGCCompact < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/#L121
     skip "couldn't get objects to collide" if collisions == 0
     assert_operator collisions, :>, 0
   end
+
+  def test_complex_hash_keys
+    list_of_objects = big_list
+    hash = list_of_objects.hash
+    GC.verify_compaction_references
+    assert_equal hash, list_of_objects.hash
+  end
 end
-- 
cgit v0.10.2


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

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