diff options
author | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-01-22 07:48:59 +0000 |
---|---|---|
committer | normal <normal@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2015-01-22 07:48:59 +0000 |
commit | 6d56fd067e219803e194b8b95aa00e558250cfec (patch) | |
tree | 71c35bf49f4f6b817ce34af7e8c419031c263405 | |
parent | c73f2d28b41a532a71b9b5000d0d59a4c6f49a91 (diff) |
fix flonum hashing regression from r45384
* st.c (st_numhash): mix float value for flonum
* hash.c (rb_any_hash): ditto
* benchmark/bm_hash_aref_flo.rb: new benchmark
* benchmark/bm_hash_ident_flo.rb: ditto
[Bug #10761]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49376 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | benchmark/bm_hash_aref_flo.rb | 4 | ||||
-rw-r--r-- | benchmark/bm_hash_ident_flo.rb | 4 | ||||
-rw-r--r-- | hash.c | 8 | ||||
-rw-r--r-- | st.c | 9 |
5 files changed, 32 insertions, 1 deletions
@@ -1,3 +1,11 @@ +Thu Jan 22 16:45:24 2015 Eric Wong <e@80x24.org> + + * st.c (st_numhash): mix float value for flonum + * hash.c (rb_any_hash): ditto + * benchmark/bm_hash_aref_flo.rb: new benchmark + * benchmark/bm_hash_ident_flo.rb: ditto + [Bug #10761] + Wed Jan 21 22:33:51 2015 Akinori MUSHA <knu@iDaemons.org> * misc/ruby-electric.el: Import version 2.2.1 from diff --git a/benchmark/bm_hash_aref_flo.rb b/benchmark/bm_hash_aref_flo.rb new file mode 100644 index 0000000000..a097bb5915 --- /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_ident_flo.rb b/benchmark/bm_hash_ident_flo.rb new file mode 100644 index 0000000000..0c7edfed3e --- /dev/null +++ b/benchmark/bm_hash_ident_flo.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] } } @@ -137,7 +137,13 @@ 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)) { + /* prevent pathological behavior: [Bug #10761] */ + a = (st_index_t)rb_float_value(a); + } hnum = rb_objid_hash((st_index_t)a); } else if (BUILTIN_TYPE(a) == T_STRING) { @@ -1761,6 +1761,15 @@ st_numhash(st_data_t n) * - (n << 3) was finally added to avoid losing bits for fixnums * - avoid expensive modulo instructions, it is currently only * shifts and bitmask operations. + * - flonum (on 64-bit) is pathologically bad, mix the actual + * float value in, but do not use the float value as-is since + * many integers get interpreted as 2.0 or -2.0 [Bug #10761] */ +#ifdef USE_FLONUM /* RUBY */ + if (FLONUM_P(n)) { + n ^= (st_data_t)rb_float_value(n); + } +#endif + return (st_index_t)((n>>(RUBY_SPECIAL_SHIFT+3)|(n<<3)) ^ (n>>3)); } |