diff --git a/ChangeLog b/ChangeLog index 215f25c522..19d9813395 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Thu Jan 22 16:45:24 2015 Eric Wong + + * 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 * 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] } } diff --git a/hash.c b/hash.c index d54ab8ada5..b7e2c66849 100644 --- a/hash.c +++ b/hash.c @@ -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) { diff --git a/st.c b/st.c index b7f66d08b4..e630c1ec48 100644 --- a/st.c +++ b/st.c @@ -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)); }