dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH] fix optimization for hash aset/aref with fstring
Date: Thu, 22 Dec 2016 00:47:52 +0000	[thread overview]
Message-ID: <20161222004752.4086-1-e@80x24.org> (raw)

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]
---
 hash.c                 |  4 +---
 insns.def              |  8 ++++++--
 internal.h             |  1 +
 test/ruby/test_hash.rb | 13 +++++++++++++
 4 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/hash.c b/hash.c
index 1867c09..b128cb3 100644
--- a/hash.c
+++ b/hash.c
@@ -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)
diff --git a/insns.def b/insns.def
index b64a168..41e4498 100644
--- a/insns.def
+++ b/insns.def
@@ -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 7aefcda..63f5d36 100644
--- a/internal.h
+++ b/internal.h
@@ -1088,6 +1088,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 b79f9aa..040d25e 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -237,6 +237,19 @@ def test_ASET_fstring_key
     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}
-- 
EW


                 reply	other threads:[~2016-12-22  0:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161222004752.4086-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).