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

ruby-changes:45205

From: eregon <ko1@a...>
Date: Sat, 7 Jan 2017 20:31:58 +0900 (JST)
Subject: [ruby-changes:45205] eregon:r57278 (trunk): fix optimization for hash aset/aref with fstring

eregon	2017-01-07 20:31:53 +0900 (Sat, 07 Jan 2017)

  New Revision: 57278

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57278

  Log:
    fix optimization for hash aset/aref with fstring
    
    Patch by Eric Wong [ruby-core:78797].
    I don't like the idea of making insns.def any bigger to support
    a corner case, and "test_hash_aref_fstring_identity" shows
    how contrived this is.
    
    [ruby-core:78783] [Bug #12855]

  Modified files:
    trunk/hash.c
    trunk/insns.def
    trunk/internal.h
    trunk/test/ruby/test_hash.rb
Index: hash.c
===================================================================
--- hash.c	(revision 57277)
+++ hash.c	(revision 57278)
@@ -2758,8 +2758,6 @@ rb_hash_compact_bang(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L2758
     return Qnil;
 }
 
-static VALUE rb_hash_compare_by_id_p(VALUE hash);
-
 /*
  *  call-seq:
  *     hsh.compare_by_identity -> hsh
@@ -2795,7 +2793,7 @@ rb_hash_compare_by_id(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L2793
  *
  */
 
-static VALUE
+VALUE
 rb_hash_compare_by_id_p(VALUE hash)
 {
     if (!RHASH(hash)->ntbl)
Index: internal.h
===================================================================
--- internal.h	(revision 57277)
+++ internal.h	(revision 57278)
@@ -1098,6 +1098,7 @@ long rb_objid_hash(st_index_t index); https://github.com/ruby/ruby/blob/trunk/internal.h#L1098
 long rb_dbl_long_hash(double d);
 st_table *rb_init_identtable(void);
 st_table *rb_init_identtable_with_size(st_index_t size);
+VALUE rb_hash_compare_by_id_p(VALUE hash);
 
 #define RHASH_TBL_RAW(h) rb_hash_tbl_raw(h)
 VALUE rb_hash_keys(VALUE hash);
Index: insns.def
===================================================================
--- insns.def	(revision 57277)
+++ insns.def	(revision 57278)
@@ -1881,7 +1881,9 @@ opt_aset_with https://github.com/ruby/ruby/blob/trunk/insns.def#L1881
 (VALUE recv, VALUE val)
 (VALUE val)
 {
-    if (!SPECIAL_CONST_P(recv) && RBASIC_CLASS(recv) == rb_cHash && BASIC_OP_UNREDEFINED_P(BOP_ASET, HASH_REDEFINED_OP_FLAG)) {
+    if (!SPECIAL_CONST_P(recv) && RBASIC_CLASS(recv) == rb_cHash &&
+		BASIC_OP_UNREDEFINED_P(BOP_ASET, HASH_REDEFINED_OP_FLAG) &&
+		rb_hash_compare_by_id_p(recv) == Qfalse) {
 	rb_hash_aset(recv, key, val);
     }
     else {
@@ -1903,7 +1905,9 @@ opt_aref_with https://github.com/ruby/ruby/blob/trunk/insns.def#L1905
 (VALUE recv)
 (VALUE val)
 {
-    if (!SPECIAL_CONST_P(recv) && RBASIC_CLASS(recv) == rb_cHash && BASIC_OP_UNREDEFINED_P(BOP_AREF, HASH_REDEFINED_OP_FLAG)) {
+    if (!SPECIAL_CONST_P(recv) && RBASIC_CLASS(recv) == rb_cHash &&
+		BASIC_OP_UNREDEFINED_P(BOP_AREF, HASH_REDEFINED_OP_FLAG) &&
+		rb_hash_compare_by_id_p(recv) == Qfalse) {
 	val = rb_hash_aref(recv, key);
     }
     else {
Index: test/ruby/test_hash.rb
===================================================================
--- test/ruby/test_hash.rb	(revision 57277)
+++ test/ruby/test_hash.rb	(revision 57278)
@@ -237,6 +237,19 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L237
     assert_same a.keys[0], b.keys[0]
   end
 
+  def test_hash_aset_fstring_identity
+    h = {}.compare_by_identity
+    h['abc'] = 1
+    h['abc'] = 2
+    assert_equal 2, h.size, '[ruby-core:78783] [Bug #12855]'
+  end
+
+  def test_hash_aref_fstring_identity
+    h = {}.compare_by_identity
+    h['abc'] = 1
+    assert_nil h['abc'], '[ruby-core:78783] [Bug #12855]'
+  end
+
   def test_NEWHASH_fstring_key
     a = {"ABC" => :t}
     b = {"ABC" => :t}

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

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