clogger RubyGem user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <normalperson@yhbt.net>
To: clogger@librelist.org
Subject: [PATCH 2/2] ext: avoid RARRAY_PTR usage for RGenGC in Ruby 2.1.0
Date: Thu, 26 Sep 2013 18:46:59 +0000	[thread overview]
Message-ID: <1380221219-30566-2-git-send-email-normalperson@yhbt.net> (raw)
In-Reply-To: 1380221219-30566-1-git-send-email-normalperson@yhbt.net

I have not benchmarked this, but this is unlikely to be a bottleneck
at all in older Rubies.  It results in a smaller binary, too, which
will help with icache bloat.  This should also improve performance
under Rubinius, too.
---
 ext/clogger_ext/clogger.c | 72 +++++++++++++++++++++++++----------------------
 1 file changed, 38 insertions(+), 34 deletions(-)

diff --git a/ext/clogger_ext/clogger.c b/ext/clogger_ext/clogger.c
index 04359f9..8995932 100644
--- a/ext/clogger_ext/clogger.c
+++ b/ext/clogger_ext/clogger.c
@@ -365,12 +365,12 @@ static void append_body_bytes_sent(struct clogger *c)
 	rb_str_buf_cat(c->log_buf, buf, nr);
 }
 
-static void append_ts(struct clogger *c, const VALUE *op, struct timespec *ts)
+static void append_ts(struct clogger *c, VALUE op, struct timespec *ts)
 {
 	char buf[sizeof(".000000") + ((sizeof(ts->tv_sec) * 8) / 3)];
 	int nr;
-	char *fmt = RSTRING_PTR(op[1]);
-	int ndiv = NUM2INT(op[2]);
+	char *fmt = RSTRING_PTR(rb_ary_entry(op, 1));
+	int ndiv = NUM2INT(rb_ary_entry(op, 2));
 	int usec = ts->tv_nsec / 1000;
 
 	nr = snprintf(buf, sizeof(buf), fmt,
@@ -379,7 +379,7 @@ static void append_ts(struct clogger *c, const VALUE *op, struct timespec *ts)
 	rb_str_buf_cat(c->log_buf, buf, nr);
 }
 
-static void append_request_time_fmt(struct clogger *c, const VALUE *op)
+static void append_request_time_fmt(struct clogger *c, VALUE op)
 {
 	struct timespec now;
 
@@ -388,7 +388,7 @@ static void append_request_time_fmt(struct clogger *c, const VALUE *op)
 	append_ts(c, op, &now);
 }
 
-static void append_time_fmt(struct clogger *c, const VALUE *op)
+static void append_time_fmt(struct clogger *c, VALUE op)
 {
 	struct timespec now;
 	int r = clock_gettime(CLOCK_REALTIME, &now);
@@ -641,35 +641,38 @@ static void special_var(struct clogger *c, enum clogger_special var)
 static VALUE cwrite(struct clogger *c)
 {
 	const VALUE ops = c->fmt_ops;
-	const VALUE *ary = RARRAY_PTR(ops);
-	long i = RARRAY_LEN(ops);
+	long i;
+	long len = RARRAY_LEN(ops);
 	VALUE dst = c->log_buf;
 
 	rb_str_set_len(dst, 0);
 
-	for (; --i >= 0; ary++) {
-		const VALUE *op = RARRAY_PTR(*ary);
-		enum clogger_opcode opcode = FIX2INT(op[0]);
+	for (i = 0; i < len; i++) {
+		VALUE op = rb_ary_entry(ops, i);
+		enum clogger_opcode opcode = FIX2INT(rb_ary_entry(op, 0));
+		VALUE op1 = rb_ary_entry(op, 1);
 
 		switch (opcode) {
 		case CL_OP_LITERAL:
-			rb_str_buf_append(dst, op[1]);
+			rb_str_buf_append(dst, op1);
 			break;
 		case CL_OP_REQUEST:
-			append_request_env(c, op[1]);
+			append_request_env(c, op1);
 			break;
 		case CL_OP_RESPONSE:
-			append_response(c, op[1]);
+			append_response(c, op1);
 			break;
 		case CL_OP_SPECIAL:
-			special_var(c, FIX2INT(op[1]));
+			special_var(c, FIX2INT(op1));
 			break;
 		case CL_OP_EVAL:
-			append_eval(c, op[1]);
+			append_eval(c, op1);
 			break;
 		case CL_OP_TIME_LOCAL:
-		case CL_OP_TIME_UTC:
-			append_time(c, opcode, op[1], op[2]);
+		case CL_OP_TIME_UTC: {
+			VALUE arg2 = rb_ary_entry(op, 2);
+			append_time(c, opcode, op1, arg2);
+		}
 			break;
 		case CL_OP_REQUEST_TIME:
 			append_request_time_fmt(c, op);
@@ -678,7 +681,7 @@ static VALUE cwrite(struct clogger *c)
 			append_time_fmt(c, op);
 			break;
 		case CL_OP_COOKIE:
-			append_cookie(c, op[1]);
+			append_cookie(c, op1);
 			break;
 		}
 	}
@@ -852,16 +855,15 @@ static VALUE ccall(struct clogger *c, VALUE env)
 	c->cookies = Qfalse;
 	rv = rb_funcall(c->app, call_id, 1, env);
 	if (TYPE(rv) == T_ARRAY && RARRAY_LEN(rv) == 3) {
-		VALUE *tmp = RARRAY_PTR(rv);
-
-		c->status = tmp[0];
-		c->headers = tmp[1];
-		c->body = tmp[2];
+		c->status = rb_ary_entry(rv, 0);
+		c->headers = rb_ary_entry(rv, 1);
+		c->body = rb_ary_entry(rv, 2);
 
-		rv = rb_ary_new4(3, tmp);
+		rv = rb_ary_dup(rv);
 		if (c->need_resp &&
-                    ! rb_obj_is_kind_of(tmp[1], cHeaderHash)) {
-			c->headers = rb_funcall(cHeaderHash, new_id, 1, tmp[1]);
+                    ! rb_obj_is_kind_of(c->headers, cHeaderHash)) {
+			c->headers = rb_funcall(cHeaderHash, new_id, 1,
+			                        c->headers);
 			rb_ary_store(rv, 1, c->headers);
 		}
 	} else {
@@ -919,17 +921,19 @@ static VALUE clogger_call(VALUE self, VALUE env)
 
 static void duplicate_buffers(VALUE ops)
 {
-	long i = RARRAY_LEN(ops);
-	VALUE *ary = RARRAY_PTR(ops);
+	long i;
+	long len = RARRAY_LEN(ops);
 
-	for ( ; --i >= 0; ary++) {
-		VALUE *op = RARRAY_PTR(*ary);
-		enum clogger_opcode opcode = FIX2INT(op[0]);
+	for (i = 0; i < len; i++) {
+		VALUE op = rb_ary_entry(ops, i);
+		enum clogger_opcode opcode = FIX2INT(rb_ary_entry(op, 0));
 
 		if (opcode == CL_OP_TIME_LOCAL || opcode == CL_OP_TIME_UTC) {
-			Check_Type(op[2], T_STRING);
-			op[2] = rb_str_dup(op[2]);
-			rb_str_modify(op[2]); /* trigger copy-on-write */
+			VALUE buf = rb_ary_entry(op, 2);
+			Check_Type(buf, T_STRING);
+			buf = rb_str_dup(buf);
+			rb_str_modify(buf); /* trigger copy-on-write */
+			rb_ary_store(op, 2, buf);
 		}
 	}
 }
-- 
1.8.4



      reply	other threads:[~2013-09-26 18:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-26 18:46 [PATCH 1/2] README: indicate we support all versions of C Ruby Eric Wong
2013-09-26 18:46 ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/clogger/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1380221219-30566-2-git-send-email-normalperson@yhbt.net \
    --to=normalperson@yhbt.net \
    --cc=clogger@librelist.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/clogger.git/

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).