about summary refs log tree commit homepage
path: root/ext/sleepy_penguin/eventfd.c
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2013-04-21 03:00:20 +0000
committerEric Wong <normalperson@yhbt.net>2013-04-21 03:05:33 +0000
commit623c6797bcf1f8077c39cdcffb83b9aa25793932 (patch)
treecd28fe30e4185b15051d5f042ba38fc3d7f32fe1 /ext/sleepy_penguin/eventfd.c
parent92b55d65af197218b7653018847b14141cd393de (diff)
downloadsleepy_penguin-623c6797bcf1f8077c39cdcffb83b9aa25793932.tar.gz
When possible, comparisons against zero require one less load
and uses one less register, so this results in smaller code:

  $ ~/linux/scripts/bloat-o-meter before.so after.so
  add/remove: 0/0 grow/shrink: 0/5 up/down: 0/-57 (-57)
  function                                     old     new   delta
  rm_watch                                      84      83      -1
  rb_sp_set_nonblock                            80      79      -1
  add_watch                                    127     126      -1
  epwait                                       692     687      -5
  s_new                                        970     921     -49

This style is favored by major C projects, including glibc.

Note: since file and file descriptor flags may eventually use
more bits of an integer, we continue comparing F_GETFD/F_GETFL
return values against -1 to be future proof.
Diffstat (limited to 'ext/sleepy_penguin/eventfd.c')
-rw-r--r--ext/sleepy_penguin/eventfd.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/ext/sleepy_penguin/eventfd.c b/ext/sleepy_penguin/eventfd.c
index 1713fdd..4f95b0d 100644
--- a/ext/sleepy_penguin/eventfd.c
+++ b/ext/sleepy_penguin/eventfd.c
@@ -30,12 +30,12 @@ static VALUE s_new(int argc, VALUE *argv, VALUE klass)
         flags = rb_sp_get_flags(klass, _flags, RB_SP_CLOEXEC(EFD_CLOEXEC));
 
         fd = eventfd(initval, flags);
-        if (fd == -1) {
+        if (fd < 0) {
                 if (errno == EMFILE || errno == ENFILE || errno == ENOMEM) {
                         rb_gc();
                         fd = eventfd(initval, flags);
                 }
-                if (fd == -1)
+                if (fd < 0)
                         rb_sys_fail("eventfd");
         }
 
@@ -88,7 +88,7 @@ static VALUE incr(int argc, VALUE *argv, VALUE self)
         x.val = (uint64_t)NUM2ULL(value);
 retry:
         w = (ssize_t)rb_sp_fd_region(efd_write, &x, x.fd);
-        if (w == -1) {
+        if (w < 0) {
                 if (errno == EAGAIN && RTEST(nonblock))
                         return Qfalse;
                 if (rb_sp_wait(rb_io_wait_writable, self, &x.fd))
@@ -124,7 +124,7 @@ static VALUE getvalue(int argc, VALUE *argv, VALUE self)
         RTEST(nonblock) ? rb_sp_set_nonblock(x.fd) : blocking_io_prepare(x.fd);
 retry:
         w = (ssize_t)rb_sp_fd_region(efd_read, &x, x.fd);
-        if (w == -1) {
+        if (w < 0) {
                 if (errno == EAGAIN && RTEST(nonblock))
                         return Qnil;
                 if (rb_sp_wait(rb_io_wait_readable, self, &x.fd))