From 31925d90c3d292d0b5f20524082b7d3dc2e08fcb Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 8 Apr 2010 14:36:36 -0700 Subject: ext: use FIX macros instead of generic NUM macros if possible They're slightly faster when we know a number is small enough to be a FIXNUM. --- ext/clogger_ext/clogger.c | 8 ++++---- test/test_clogger.rb | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/ext/clogger_ext/clogger.c b/ext/clogger_ext/clogger.c index 6e14938..f0087fe 100644 --- a/ext/clogger_ext/clogger.c +++ b/ext/clogger_ext/clogger.c @@ -284,7 +284,7 @@ static void append_status(struct clogger *c) } } - nr = NUM2INT(status); + nr = FIX2INT(status); if (nr >= 100 && nr <= 999) { nr = snprintf(buf, sizeof(buf), "%03d", nr); assert(nr == 3); @@ -522,7 +522,7 @@ static VALUE cwrite(struct clogger *c) for (; --i >= 0; ary++) { const VALUE *op = RARRAY_PTR(*ary); - enum clogger_opcode opcode = NUM2INT(op[0]); + enum clogger_opcode opcode = FIX2INT(op[0]); switch (opcode) { case CL_OP_LITERAL: @@ -535,7 +535,7 @@ static VALUE cwrite(struct clogger *c) append_response(c, op[1]); break; case CL_OP_SPECIAL: - special_var(c, NUM2INT(op[1])); + special_var(c, FIX2INT(op[1])); break; case CL_OP_EVAL: append_eval(c, op[1]); @@ -708,7 +708,7 @@ static VALUE ccall(struct clogger *c, VALUE env) rb_ary_store(rv, 1, c->headers); } } else { - c->status = INT2NUM(500); + c->status = INT2FIX(500); c->headers = c->body = rb_ary_new(); cwrite(c); rb_raise(rb_eTypeError, diff --git a/test/test_clogger.rb b/test/test_clogger.rb index 4dab3fc..42cab1c 100644 --- a/test/test_clogger.rb +++ b/test/test_clogger.rb @@ -587,6 +587,24 @@ class TestClogger < Test::Unit::TestCase assert ! cl.reentrant? end + def test_invalid_status + s = [] + body = [] + app = lambda { |env| [ env["force.status"], [ %w(a b) ], body ] } + o = { :logger => s, :format => "$status" } + cl = Clogger.new(app, o) + status, headers, body = cl.call(@req.merge("force.status" => -1)) + assert_equal -1, status + assert_equal "-\n", s.last + status, headers, body = cl.call(@req.merge("force.status" => 1000)) + assert_equal 1000, status + assert_equal "-\n", s.last + u64_max = 0xffffffffffffffff + status, headers, body = cl.call(@req.merge("force.status" => u64_max)) + assert_equal u64_max, status + assert_equal "-\n", s.last + end + # so we don't care about the portability of this test # if it doesn't leak on Linux, it won't leak anywhere else # unless your C compiler or platform is otherwise broken -- cgit v1.2.3-24-ge0c7