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

ruby-changes:38955

From: normal <ko1@a...>
Date: Fri, 26 Jun 2015 08:07:12 +0900 (JST)
Subject: [ruby-changes:38955] normal:r51036 (trunk): enum.c (enum_minmax): simplify return value creation

normal	2015-06-26 08:06:48 +0900 (Fri, 26 Jun 2015)

  New Revision: 51036

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

  Log:
    enum.c (enum_minmax): simplify return value creation
    
    No need to call three functions on success when one will do.
    This results in less LoC and smaller object code, too:
    
       text    data     bss     dec     hex filename
      33860       0     296   34156    856c gcc/enum.o-before
      33852       0     296   34148    8564 gcc/enum.o
    
    * enum.c (enum_minmax): simplify return value creation
    * test/ruby/test_enum.rb: test behavior on empty

  Modified files:
    trunk/ChangeLog
    trunk/enum.c
    trunk/hash.c
    trunk/test/ruby/test_enum.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 51035)
+++ ChangeLog	(revision 51036)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Fri Jun 26 07:53:56 2015  Eric Wong  <e@8...>
+
+	* enum.c (enum_minmax): simplify return value creation
+	* test/ruby/test_enum.rb: test behavior on empty
+
 Thu Jun 25 21:24:28 2015  Naohisa Goto  <ngotogenome@g...>
 
 	* test/-ext-/popen_deadlock/test_popen_deadlock.rb: test [Bug #11265]
Index: enum.c
===================================================================
--- enum.c	(revision 51035)
+++ enum.c	(revision 51036)
@@ -1715,7 +1715,6 @@ enum_minmax(VALUE obj) https://github.com/ruby/ruby/blob/trunk/enum.c#L1715
 {
     struct MEMO *memo = MEMO_NEW(Qundef, Qundef, Qundef);
     struct minmax_t *m = (struct minmax_t *)&memo->v1;
-    VALUE ary = rb_ary_new3(2, Qnil, Qnil);
 
     m->min = Qundef;
     m->last = Qundef;
@@ -1730,10 +1729,9 @@ enum_minmax(VALUE obj) https://github.com/ruby/ruby/blob/trunk/enum.c#L1729
 	    minmax_i_update(m->last, m->last, m);
     }
     if (m->min != Qundef) {
-	rb_ary_store(ary, 0, m->min);
-	rb_ary_store(ary, 1, m->max);
+	return rb_assoc_new(m->min, m->max);
     }
-    return ary;
+    return rb_assoc_new(Qnil, Qnil);
 }
 
 static VALUE
Index: hash.c
===================================================================
--- hash.c	(revision 51035)
+++ hash.c	(revision 51036)
@@ -821,7 +821,7 @@ rb_hash_fetch_m(int argc, VALUE *argv, V https://github.com/ruby/ruby/blob/trunk/hash.c#L821
     if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val)) {
 	if (block_given) return rb_yield(key);
 	if (argc == 1) {
-	    volatile VALUE desc = rb_protect(rb_inspect, key, 0);
+	    VALUE desc = rb_protect(rb_inspect, key, 0);
 	    if (NIL_P(desc)) {
 		desc = rb_any_to_s(key);
 	    }
@@ -3284,7 +3284,7 @@ env_each_pair(VALUE ehash) https://github.com/ruby/ruby/blob/trunk/hash.c#L3284
 static VALUE
 env_reject_bang(VALUE ehash)
 {
-    volatile VALUE keys;
+    VALUE keys;
     long i;
     int del = 0;
 
@@ -3301,6 +3301,7 @@ env_reject_bang(VALUE ehash) https://github.com/ruby/ruby/blob/trunk/hash.c#L3301
 	    }
 	}
     }
+    RB_GC_GUARD(keys);
     if (del == 0) return Qnil;
     return envtbl;
 }
@@ -3370,6 +3371,7 @@ env_select(VALUE ehash) https://github.com/ruby/ruby/blob/trunk/hash.c#L3371
 	    }
 	}
     }
+    RB_GC_GUARD(keys);
 
     return result;
 }
@@ -3384,7 +3386,7 @@ env_select(VALUE ehash) https://github.com/ruby/ruby/blob/trunk/hash.c#L3386
 static VALUE
 env_select_bang(VALUE ehash)
 {
-    volatile VALUE keys;
+    VALUE keys;
     long i;
     int del = 0;
 
@@ -3401,6 +3403,7 @@ env_select_bang(VALUE ehash) https://github.com/ruby/ruby/blob/trunk/hash.c#L3403
 	    }
 	}
     }
+    RB_GC_GUARD(keys);
     if (del == 0) return Qnil;
     return envtbl;
 }
@@ -3431,7 +3434,7 @@ env_keep_if(VALUE ehash) https://github.com/ruby/ruby/blob/trunk/hash.c#L3434
 VALUE
 rb_env_clear(void)
 {
-    volatile VALUE keys;
+    VALUE keys;
     long i;
 
     keys = env_keys();
@@ -3441,6 +3444,7 @@ rb_env_clear(void) https://github.com/ruby/ruby/blob/trunk/hash.c#L3444
 	    env_delete(Qnil, RARRAY_AREF(keys, i));
 	}
     }
+    RB_GC_GUARD(keys);
     return envtbl;
 }
 
@@ -3820,7 +3824,7 @@ env_replace_i(VALUE key, VALUE val, VALU https://github.com/ruby/ruby/blob/trunk/hash.c#L3824
 static VALUE
 env_replace(VALUE env, VALUE hash)
 {
-    volatile VALUE keys;
+    VALUE keys;
     long i;
 
     keys = env_keys();
@@ -3831,6 +3835,7 @@ env_replace(VALUE env, VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L3835
     for (i=0; i<RARRAY_LEN(keys); i++) {
 	env_delete(env, RARRAY_AREF(keys, i));
     }
+    RB_GC_GUARD(keys);
     return env;
 }
 
Index: test/ruby/test_enum.rb
===================================================================
--- test/ruby/test_enum.rb	(revision 51035)
+++ test/ruby/test_enum.rb	(revision 51036)
@@ -304,6 +304,7 @@ class TestEnumerable < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/test/ruby/test_enum.rb#L304
     assert_equal([1, 3], [2,3,1].minmax)
     assert_equal([3, 1], [2,3,1].minmax {|a,b| b <=> a })
     assert_equal([1, 3], [2,2,3,3,1,1].minmax)
+    assert_equal([nil, nil], [].minmax)
   end
 
   def test_min_by

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

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