dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH 1/2] enum.c: remove volatile, use RB_GC_GUARD
@ 2015-06-24  3:37 Eric Wong
  2015-06-24  3:37 ` [PATCH 2/2] enum.c (enum_minmax): simplify return value creation Eric Wong
  0 siblings, 1 reply; 2+ messages in thread
From: Eric Wong @ 2015-06-24  3:37 UTC (permalink / raw)
  To: spew

volatile appears unnecessary in most cases as the VALUEs are used as
arguments of uninlined functions.  Even worse, volatile can be
insufficient in places where RB_GC_GUARD is necessary.
---
 enum.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/enum.c b/enum.c
index 4b1e119..8a3a463 100644
--- a/enum.c
+++ b/enum.c
@@ -2351,10 +2351,10 @@ static VALUE
 zip_ary(RB_BLOCK_CALL_FUNC_ARGLIST(val, memoval))
 {
     struct MEMO *memo = (struct MEMO *)memoval;
-    volatile VALUE result = memo->v1;
-    volatile VALUE args = memo->v2;
+    VALUE result = memo->v1;
+    VALUE args = memo->v2;
     long n = memo->u3.cnt++;
-    volatile VALUE tmp;
+    VALUE tmp;
     int i;
 
     tmp = rb_ary_new2(RARRAY_LEN(args) + 1);
@@ -2375,6 +2375,9 @@ zip_ary(RB_BLOCK_CALL_FUNC_ARGLIST(val, memoval))
     else {
 	rb_ary_push(result, tmp);
     }
+
+    RB_GC_GUARD(args);
+
     return Qnil;
 }
 
@@ -2394,9 +2397,9 @@ static VALUE
 zip_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, memoval))
 {
     struct MEMO *memo = (struct MEMO *)memoval;
-    volatile VALUE result = memo->v1;
-    volatile VALUE args = memo->v2;
-    volatile VALUE tmp;
+    VALUE result = memo->v1;
+    VALUE args = memo->v2;
+    VALUE tmp;
     int i;
 
     tmp = rb_ary_new2(RARRAY_LEN(args) + 1);
@@ -2423,6 +2426,9 @@ zip_i(RB_BLOCK_CALL_FUNC_ARGLIST(val, memoval))
     else {
 	rb_ary_push(result, tmp);
     }
+
+    RB_GC_GUARD(args);
+
     return Qnil;
 }
 
-- 
EW


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/2] enum.c (enum_minmax): simplify return value creation
  2015-06-24  3:37 [PATCH 1/2] enum.c: remove volatile, use RB_GC_GUARD Eric Wong
@ 2015-06-24  3:37 ` Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2015-06-24  3:37 UTC (permalink / raw)
  To: spew

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                 | 6 ++----
 test/ruby/test_enum.rb | 1 +
 2 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/enum.c b/enum.c
index 8a3a463..87ce217 100644
--- a/enum.c
+++ b/enum.c
@@ -1715,7 +1715,6 @@ enum_minmax(VALUE obj)
 {
     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)
 	    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
diff --git a/test/ruby/test_enum.rb b/test/ruby/test_enum.rb
index db03b7b..9260c43 100644
--- a/test/ruby/test_enum.rb
+++ b/test/ruby/test_enum.rb
@@ -304,6 +304,7 @@ class TestEnumerable < Test::Unit::TestCase
     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
-- 
EW


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-06-24  3:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-24  3:37 [PATCH 1/2] enum.c: remove volatile, use RB_GC_GUARD Eric Wong
2015-06-24  3:37 ` [PATCH 2/2] enum.c (enum_minmax): simplify return value creation Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).