diff options
author | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-01-07 11:31:53 +0000 |
---|---|---|
committer | eregon <eregon@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-01-07 11:31:53 +0000 |
commit | ac9f8145f1e4fe739128db39be0abe401c6096ea (patch) | |
tree | 1437b08685e31dfbefd9c2b895522cb3e5b16764 | |
parent | 4dbbcc48dec3071d644780fa2bed226589d9e6d7 (diff) | |
download | ruby-ac9f8145f1e4fe739128db39be0abe401c6096ea.tar.gz |
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]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57278 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | hash.c | 4 | ||||
-rw-r--r-- | insns.def | 8 | ||||
-rw-r--r-- | internal.h | 1 | ||||
-rw-r--r-- | test/ruby/test_hash.rb | 13 |
4 files changed, 21 insertions, 5 deletions
@@ -2758,8 +2758,6 @@ rb_hash_compact_bang(VALUE hash) 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) * */ -static VALUE +VALUE rb_hash_compare_by_id_p(VALUE hash) { if (!RHASH(hash)->ntbl) @@ -1881,7 +1881,9 @@ opt_aset_with (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 (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 { diff --git a/internal.h b/internal.h index 2346345480..c44d7bd99c 100644 --- a/internal.h +++ b/internal.h @@ -1098,6 +1098,7 @@ long rb_objid_hash(st_index_t index); 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); diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index b79f9aa0cc..040d25e144 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -237,6 +237,19 @@ class TestHash < Test::Unit::TestCase 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} |