Skip to content

Commit 6d56fd0

Browse files
author
normal
committed
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 ruby#10761] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49376 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent c73f2d2 commit 6d56fd0

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

ChangeLog

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
Thu Jan 22 16:45:24 2015 Eric Wong <[email protected]>
2+
3+
* st.c (st_numhash): mix float value for flonum
4+
* hash.c (rb_any_hash): ditto
5+
* benchmark/bm_hash_aref_flo.rb: new benchmark
6+
* benchmark/bm_hash_ident_flo.rb: ditto
7+
[Bug #10761]
8+
19
Wed Jan 21 22:33:51 2015 Akinori MUSHA <[email protected]>
210

311
* misc/ruby-electric.el: Import version 2.2.1 from

benchmark/bm_hash_aref_flo.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
h = {}
2+
strs = (1..10000).to_a.map!(&:to_f)
3+
strs.each { |s| h[s] = s }
4+
50.times { strs.each { |s| h[s] } }

benchmark/bm_hash_ident_flo.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
h = {}.compare_by_identity
2+
strs = (1..10000).to_a.map!(&:to_f)
3+
strs.each { |s| h[s] = s }
4+
50.times { strs.each { |s| h[s] } }

hash.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,13 @@ rb_any_hash(VALUE a)
137137

138138
if (SPECIAL_CONST_P(a)) {
139139
if (a == Qundef) return 0;
140-
if (STATIC_SYM_P(a)) a >>= (RUBY_SPECIAL_SHIFT + ID_SCOPE_SHIFT);
140+
if (STATIC_SYM_P(a)) {
141+
a >>= (RUBY_SPECIAL_SHIFT + ID_SCOPE_SHIFT);
142+
}
143+
else if (FLONUM_P(a)) {
144+
/* prevent pathological behavior: [Bug #10761] */
145+
a = (st_index_t)rb_float_value(a);
146+
}
141147
hnum = rb_objid_hash((st_index_t)a);
142148
}
143149
else if (BUILTIN_TYPE(a) == T_STRING) {

st.c

+9
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,15 @@ st_numhash(st_data_t n)
17611761
* - (n << 3) was finally added to avoid losing bits for fixnums
17621762
* - avoid expensive modulo instructions, it is currently only
17631763
* shifts and bitmask operations.
1764+
* - flonum (on 64-bit) is pathologically bad, mix the actual
1765+
* float value in, but do not use the float value as-is since
1766+
* many integers get interpreted as 2.0 or -2.0 [Bug #10761]
17641767
*/
1768+
#ifdef USE_FLONUM /* RUBY */
1769+
if (FLONUM_P(n)) {
1770+
n ^= (st_data_t)rb_float_value(n);
1771+
}
1772+
#endif
1773+
17651774
return (st_index_t)((n>>(RUBY_SPECIAL_SHIFT+3)|(n<<3)) ^ (n>>3));
17661775
}

0 commit comments

Comments
 (0)