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 flonum hashing regression from r45384
Date: Wed, 21 Jan 2015 22:03:07 +0000	[thread overview]
Message-ID: <flonum-hash-fixup-r45384-v1@r49365> (raw)

* st.c (st_numhash): use float value for flonum
* hash.c (rb_any_hash): ditto
* benchmark/bm_hash_aref_flo.rb: new benchmark
* benchmark/bm_hash_aref_flo_ident.rb: ditto
  [Bug #10761]
---
 benchmark/bm_hash_aref_flo.rb       | 4 ++++
 benchmark/bm_hash_aref_flo_ident.rb | 4 ++++
 hash.c                              | 7 ++++++-
 st.c                                | 5 +++++
 4 files changed, 19 insertions(+), 1 deletion(-)
 create mode 100644 benchmark/bm_hash_aref_flo.rb
 create mode 100644 benchmark/bm_hash_aref_flo_ident.rb

diff --git a/benchmark/bm_hash_aref_flo.rb b/benchmark/bm_hash_aref_flo.rb
new file mode 100644
index 0000000..a097bb5
--- /dev/null
+++ b/benchmark/bm_hash_aref_flo.rb
@@ -0,0 +1,4 @@
+h = {}
+strs = (1..10000).to_a.map!(&:to_f)
+strs.each { |s| h[s] = s }
+50.times { strs.each { |s| h[s] } }
diff --git a/benchmark/bm_hash_aref_flo_ident.rb b/benchmark/bm_hash_aref_flo_ident.rb
new file mode 100644
index 0000000..0c7edfe
--- /dev/null
+++ b/benchmark/bm_hash_aref_flo_ident.rb
@@ -0,0 +1,4 @@
+h = {}.compare_by_identity
+strs = (1..10000).to_a.map!(&:to_f)
+strs.each { |s| h[s] = s }
+50.times { strs.each { |s| h[s] } }
diff --git a/hash.c b/hash.c
index d54ab8a..3a90fdb 100644
--- a/hash.c
+++ b/hash.c
@@ -137,7 +137,12 @@ rb_any_hash(VALUE a)
 
     if (SPECIAL_CONST_P(a)) {
 	if (a == Qundef) return 0;
-	if (STATIC_SYM_P(a)) a >>= (RUBY_SPECIAL_SHIFT + ID_SCOPE_SHIFT);
+	if (STATIC_SYM_P(a)) {
+	    a >>= (RUBY_SPECIAL_SHIFT + ID_SCOPE_SHIFT);
+	}
+	else if (FLONUM_P(a)) {
+	    a = (st_index_t)rb_float_value(a);
+	}
 	hnum = rb_objid_hash((st_index_t)a);
     }
     else if (BUILTIN_TYPE(a) == T_STRING) {
diff --git a/st.c b/st.c
index b7f66d0..2ffa986 100644
--- a/st.c
+++ b/st.c
@@ -1762,5 +1762,10 @@ st_numhash(st_data_t n)
      * - avoid expensive modulo instructions, it is currently only
      *   shifts and bitmask operations.
      */
+#ifdef USE_FLONUM /* RUBY */
+    if (FLONUM_P(n))
+	return (st_index_t)rb_float_value(n);
+#endif
+
     return (st_index_t)((n>>(RUBY_SPECIAL_SHIFT+3)|(n<<3)) ^ (n>>3));
 }
-- 
EW


             reply	other threads:[~2015-01-21 22:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-21 22:03 Eric Wong [this message]
2015-01-21 22:15 ` flonum hash fix regression benchmark results Eric Wong

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=flonum-hash-fixup-r45384-v1@r49365 \
    --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).