From 623c6797bcf1f8077c39cdcffb83b9aa25793932 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 21 Apr 2013 03:00:20 +0000 Subject: favor comparison against 0 for error checking 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. --- ext/sleepy_penguin/eventfd.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'ext/sleepy_penguin/eventfd.c') 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)) -- cgit v1.2.3-24-ge0c7