[前][次][番号順一覧][スレッド一覧]

ruby-changes:37432

From: naruse <ko1@a...>
Date: Fri, 6 Feb 2015 01:05:24 +0900 (JST)
Subject: [ruby-changes:37432] naruse:r49513 (ruby_2_2): merge revision(s) 49376, 49387, 49389: [Backport #10761]

naruse	2015-02-06 01:05:10 +0900 (Fri, 06 Feb 2015)

  New Revision: 49513

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49513

  Log:
    merge revision(s) 49376,49387,49389: [Backport #10761]
    
    * 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]
    
    * benchmark/bm_marshal_dump_flo.rb: new benchmark for [Bug #10761]
    
    * marshal.c (w_object, marshal_dump): use indetity tables for
      arbitrary VALUE keys, because of performance of FLONUM.
      [Bug #10761]
    
    * marshal.c (obj_alloc_by_klass, marshal_load): ditto.

  Added files:
    branches/ruby_2_2/benchmark/bm_hash_aref_flo.rb
    branches/ruby_2_2/benchmark/bm_hash_ident_flo.rb
    branches/ruby_2_2/benchmark/bm_marshal_dump_flo.rb
  Modified directories:
    branches/ruby_2_2/
  Modified files:
    branches/ruby_2_2/ChangeLog
    branches/ruby_2_2/hash.c
    branches/ruby_2_2/internal.h
    branches/ruby_2_2/marshal.c
    branches/ruby_2_2/st.c
    branches/ruby_2_2/version.h
Index: ruby_2_2/ChangeLog
===================================================================
--- ruby_2_2/ChangeLog	(revision 49512)
+++ ruby_2_2/ChangeLog	(revision 49513)
@@ -1,3 +1,23 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/ChangeLog#L1
+Fri Feb  6 01:03:38 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* marshal.c (w_object, marshal_dump): use indetity tables for
+	  arbitrary VALUE keys, because of performance of FLONUM.
+	  [Bug #10761]
+
+	* marshal.c (obj_alloc_by_klass, marshal_load): ditto.
+
+Fri Feb  6 01:03:38 2015  Eric Wong  <e@8...>
+
+	* benchmark/bm_marshal_dump_flo.rb: new benchmark for [Bug #10761]
+
+Fri Feb  6 01:03:38 2015  Eric Wong  <e@8...>
+
+	* 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]
+
 Thu Feb  5 16:30:09 2015  Nobuyoshi Nakada  <nobu@r...>
 
 	* parse.y (gettable_gen): disable warnings of possible reference
Index: ruby_2_2/st.c
===================================================================
--- ruby_2_2/st.c	(revision 49512)
+++ ruby_2_2/st.c	(revision 49513)
@@ -1761,6 +1761,15 @@ st_numhash(st_data_t n) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/st.c#L1761
      * - (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));
 }
Index: ruby_2_2/hash.c
===================================================================
--- ruby_2_2/hash.c	(revision 49512)
+++ ruby_2_2/hash.c	(revision 49513)
@@ -137,7 +137,13 @@ rb_any_hash(VALUE a) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/hash.c#L137
 
     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) {
@@ -2501,6 +2507,18 @@ rb_hash_compare_by_id_p(VALUE hash) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/hash.c#L2507
     return Qfalse;
 }
 
+st_table *
+rb_init_identtable(void)
+{
+    return st_init_table(&identhash);
+}
+
+st_table *
+rb_init_identtable_with_size(st_index_t size)
+{
+    return st_init_table_with_size(&identhash, size);
+}
+
 static int
 any_p_i(VALUE key, VALUE value, VALUE arg)
 {
Index: ruby_2_2/internal.h
===================================================================
--- ruby_2_2/internal.h	(revision 49512)
+++ ruby_2_2/internal.h	(revision 49513)
@@ -701,6 +701,8 @@ struct st_table *rb_hash_tbl_raw(VALUE h https://github.com/ruby/ruby/blob/trunk/ruby_2_2/internal.h#L701
 VALUE rb_hash_has_key(VALUE hash, VALUE key);
 VALUE rb_hash_set_default_proc(VALUE hash, VALUE proc);
 long rb_objid_hash(st_index_t index);
+st_table *rb_init_identtable(void);
+st_table *rb_init_identtable_with_size(st_index_t size);
 
 #define RHASH_TBL_RAW(h) rb_hash_tbl_raw(h)
 VALUE rb_hash_keys(VALUE hash);
Index: ruby_2_2/version.h
===================================================================
--- ruby_2_2/version.h	(revision 49512)
+++ ruby_2_2/version.h	(revision 49513)
@@ -1,10 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/version.h#L1
 #define RUBY_VERSION "2.2.0"
-#define RUBY_RELEASE_DATE "2015-02-05"
-#define RUBY_PATCHLEVEL 38
+#define RUBY_RELEASE_DATE "2015-02-06"
+#define RUBY_PATCHLEVEL 39
 
 #define RUBY_RELEASE_YEAR 2015
 #define RUBY_RELEASE_MONTH 2
-#define RUBY_RELEASE_DAY 5
+#define RUBY_RELEASE_DAY 6
 
 #include "ruby/version.h"
 
Index: ruby_2_2/benchmark/bm_hash_ident_flo.rb
===================================================================
--- ruby_2_2/benchmark/bm_hash_ident_flo.rb	(revision 0)
+++ ruby_2_2/benchmark/bm_hash_ident_flo.rb	(revision 49513)
@@ -0,0 +1,4 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/benchmark/bm_hash_ident_flo.rb#L1
+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] } }
Index: ruby_2_2/benchmark/bm_marshal_dump_flo.rb
===================================================================
--- ruby_2_2/benchmark/bm_marshal_dump_flo.rb	(revision 0)
+++ ruby_2_2/benchmark/bm_marshal_dump_flo.rb	(revision 49513)
@@ -0,0 +1,2 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/benchmark/bm_marshal_dump_flo.rb#L1
+bug10761 = 10000.times.map { |x| x.to_f }
+100.times { Marshal.dump(bug10761) }
Index: ruby_2_2/benchmark/bm_hash_aref_flo.rb
===================================================================
--- ruby_2_2/benchmark/bm_hash_aref_flo.rb	(revision 0)
+++ ruby_2_2/benchmark/bm_hash_aref_flo.rb	(revision 49513)
@@ -0,0 +1,4 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_2/benchmark/bm_hash_aref_flo.rb#L1
+h = {}
+strs = (1..10000).to_a.map!(&:to_f)
+strs.each { |s| h[s] = s }
+50.times { strs.each { |s| h[s] } }
Index: ruby_2_2/marshal.c
===================================================================
--- ruby_2_2/marshal.c	(revision 49512)
+++ ruby_2_2/marshal.c	(revision 49513)
@@ -751,7 +751,7 @@ w_object(VALUE obj, struct dump_arg *arg https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L751
                 VALUE real_obj = obj;
                 obj = compat->dumper(real_obj);
                 if (!arg->compat_tbl) {
-                    arg->compat_tbl = st_init_numtable();
+                    arg->compat_tbl = rb_init_identtable();
                 }
                 st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
 		if (obj != real_obj && !ivtbl) hasiv = 0;
@@ -1000,7 +1000,7 @@ marshal_dump(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L1000
     wrapper = TypedData_Make_Struct(rb_cData, struct dump_arg, &dump_arg_data, arg);
     arg->dest = 0;
     arg->symbols = st_init_numtable();
-    arg->data    = st_init_numtable();
+    arg->data    = rb_init_identtable();
     arg->infection = 0;
     arg->compat_tbl = 0;
     arg->encodings = 0;
@@ -1510,7 +1510,7 @@ obj_alloc_by_klass(VALUE klass, struct l https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L1510
 	if (oldclass) *oldclass = compat->oldclass;
 
         if (!arg->compat_tbl) {
-            arg->compat_tbl = st_init_numtable();
+            arg->compat_tbl = rb_init_identtable();
         }
         st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj);
         return obj;
@@ -2019,7 +2019,7 @@ marshal_load(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/ruby_2_2/marshal.c#L2019
     arg->src = port;
     arg->offset = 0;
     arg->symbols = st_init_numtable();
-    arg->data    = st_init_numtable();
+    arg->data    = rb_init_identtable();
     arg->compat_tbl = 0;
     arg->proc = 0;
     arg->readable = 0;

Property changes on: ruby_2_2
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r49376,49387,49389


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]