librelist archives

« back to archive

[PATCH 1/3] tryopen: remove RARRAY_PTR usage in init

[PATCH 1/3] tryopen: remove RARRAY_PTR usage in init

From:
Eric Wong
Date:
2013-09-25 @ 19:42
From: Eric Wong <e@yhbt.net>

This is trivially non-performance-critical, and can only
help with advanced GCs in Ruby 2.1.0 and Rubinius
---
 ext/kgio/tryopen.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/ext/kgio/tryopen.c b/ext/kgio/tryopen.c
index 5fa6ada..902c745 100644
--- a/ext/kgio/tryopen.c
+++ b/ext/kgio/tryopen.c
@@ -135,8 +135,7 @@ void init_kgio_tryopen(void)
 	VALUE mPipeMethods = rb_const_get(mKgio, rb_intern("PipeMethods"));
 	VALUE cFile;
 	VALUE tmp;
-	VALUE *ptr;
-	long len;
+	long i, len;
 
 	id_path = rb_intern("path");
 	id_for_fd = rb_intern("for_fd");
@@ -161,15 +160,15 @@ void init_kgio_tryopen(void)
 
 	errno2sym = st_init_numtable();
 	tmp = rb_funcall(rb_mErrno, rb_intern("constants"), 0);
-	ptr = RARRAY_PTR(tmp);
 	len = RARRAY_LEN(tmp);
-	for (; --len >= 0; ptr++) {
+	for (i = 0; i < len; i++) {
 		VALUE error;
+		VALUE err = rb_ary_entry(tmp, i);
 		ID const_id;
 
-		switch (TYPE(*ptr)) {
-		case T_SYMBOL: const_id = SYM2ID(*ptr); break;
-		case T_STRING: const_id = rb_intern(RSTRING_PTR(*ptr)); break;
+		switch (TYPE(err)) {
+		case T_SYMBOL: const_id = SYM2ID(err); break;
+		case T_STRING: const_id = rb_intern(RSTRING_PTR(err)); break;
 		default: rb_bug("constant not a symbol or string");
 		}
 
-- 
1.8.3.2.701.g8c4e4ec

[PATCH 1/3] tryopen: remove RARRAY_PTR usage in init

From:
Eric Wong
Date:
2013-09-25 @ 19:43
From: Eric Wong <e@yhbt.net>

This is trivially non-performance-critical, and can only
help with advanced GCs in Ruby 2.1.0 and Rubinius
---
 ext/kgio/tryopen.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/ext/kgio/tryopen.c b/ext/kgio/tryopen.c
index 5fa6ada..902c745 100644
--- a/ext/kgio/tryopen.c
+++ b/ext/kgio/tryopen.c
@@ -135,8 +135,7 @@ void init_kgio_tryopen(void)
 	VALUE mPipeMethods = rb_const_get(mKgio, rb_intern("PipeMethods"));
 	VALUE cFile;
 	VALUE tmp;
-	VALUE *ptr;
-	long len;
+	long i, len;
 
 	id_path = rb_intern("path");
 	id_for_fd = rb_intern("for_fd");
@@ -161,15 +160,15 @@ void init_kgio_tryopen(void)
 
 	errno2sym = st_init_numtable();
 	tmp = rb_funcall(rb_mErrno, rb_intern("constants"), 0);
-	ptr = RARRAY_PTR(tmp);
 	len = RARRAY_LEN(tmp);
-	for (; --len >= 0; ptr++) {
+	for (i = 0; i < len; i++) {
 		VALUE error;
+		VALUE err = rb_ary_entry(tmp, i);
 		ID const_id;
 
-		switch (TYPE(*ptr)) {
-		case T_SYMBOL: const_id = SYM2ID(*ptr); break;
-		case T_STRING: const_id = rb_intern(RSTRING_PTR(*ptr)); break;
+		switch (TYPE(err)) {
+		case T_SYMBOL: const_id = SYM2ID(err); break;
+		case T_STRING: const_id = rb_intern(RSTRING_PTR(err)); break;
 		default: rb_bug("constant not a symbol or string");
 		}
 
-- 
1.8.3.2.701.g8c4e4ec

Re: [kgio] [PATCH 1/3] tryopen: remove RARRAY_PTR usage in init

From:
Eric Wong
Date:
2013-09-25 @ 19:44
Oops, sorry for the double git-send-email. Too much coffee makes
me twitchy :x

Re: [kgio] [PATCH 1/3] tryopen: remove RARRAY_PTR usage in init

From:
Aeon
Date:
2013-09-25 @ 21:37
How can I unsubscribe from this list?

I am not even sure where I got signed up.

-Aaron


On Wed, Sep 25, 2013 at 2:43 PM, Eric Wong <normalperson@yhbt.net> wrote:

> From: Eric Wong <e@yhbt.net>
>
> This is trivially non-performance-critical, and can only
> help with advanced GCs in Ruby 2.1.0 and Rubinius
> ---
>  ext/kgio/tryopen.c | 13 ++++++-------
>  1 file changed, 6 insertions(+), 7 deletions(-)
>
> diff --git a/ext/kgio/tryopen.c b/ext/kgio/tryopen.c
> index 5fa6ada..902c745 100644
> --- a/ext/kgio/tryopen.c
> +++ b/ext/kgio/tryopen.c
> @@ -135,8 +135,7 @@ void init_kgio_tryopen(void)
>         VALUE mPipeMethods = rb_const_get(mKgio, rb_intern("PipeMethods"));
>         VALUE cFile;
>         VALUE tmp;
> -       VALUE *ptr;
> -       long len;
> +       long i, len;
>
>         id_path = rb_intern("path");
>         id_for_fd = rb_intern("for_fd");
> @@ -161,15 +160,15 @@ void init_kgio_tryopen(void)
>
>         errno2sym = st_init_numtable();
>         tmp = rb_funcall(rb_mErrno, rb_intern("constants"), 0);
> -       ptr = RARRAY_PTR(tmp);
>         len = RARRAY_LEN(tmp);
> -       for (; --len >= 0; ptr++) {
> +       for (i = 0; i < len; i++) {
>                 VALUE error;
> +               VALUE err = rb_ary_entry(tmp, i);
>                 ID const_id;
>
> -               switch (TYPE(*ptr)) {
> -               case T_SYMBOL: const_id = SYM2ID(*ptr); break;
> -               case T_STRING: const_id = rb_intern(RSTRING_PTR(*ptr));
> break;
> +               switch (TYPE(err)) {
> +               case T_SYMBOL: const_id = SYM2ID(err); break;
> +               case T_STRING: const_id = rb_intern(RSTRING_PTR(err));
> break;
>                 default: rb_bug("constant not a symbol or string");
>                 }
>
> --
> 1.8.3.2.701.g8c4e4ec
>
>

Re: [kgio] [PATCH 1/3] tryopen: remove RARRAY_PTR usage in init

From:
Eric Wong
Date:
2013-09-25 @ 22:00
Aeon <aaron.bryson@gmail.com> wrote:
> How can I unsubscribe from this list?

Email kgio-unsubscribe@librelist.org

Instructions should be on librelist.com somewhere, too.

Re: [kgio] [PATCH 1/3] tryopen: remove RARRAY_PTR usage in init

From:
Aeon
Date:
2013-09-26 @ 03:02
Thanks!


On Wed, Sep 25, 2013 at 5:00 PM, Eric Wong <normalperson@yhbt.net> wrote:

> Aeon <aaron.bryson@gmail.com> wrote:
> > How can I unsubscribe from this list?
>
> Email kgio-unsubscribe@librelist.org
>
> Instructions should be on librelist.com somewhere, too.
>

[PATCH 3/3] my_writev: stylistic fixes

From:
Eric Wong
Date:
2013-09-25 @ 19:43
From: Eric Wong <e@yhbt.net>

Rename 'str' to 'ary', as that's the whole point of supporting
writev...  Also, long lines need to be wrapped at 80 columns
---
 ext/kgio/read_write.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/ext/kgio/read_write.c b/ext/kgio/read_write.c
index 34c619f..fcb48bb 100644
--- a/ext/kgio/read_write.c
+++ b/ext/kgio/read_write.c
@@ -606,12 +606,12 @@ static int writev_check(struct io_args_v *a, long n,
const char *msg, int io_wai
 	return 0;
 }
 
-static VALUE my_writev(VALUE io, VALUE str, int io_wait)
+static VALUE my_writev(VALUE io, VALUE ary, int io_wait)
 {
 	struct io_args_v a;
 	long n;
 
-	prepare_writev(&a, io, str);
+	prepare_writev(&a, io, ary);
 	set_nonblocking(a.fd);
 
 	do {
@@ -619,12 +619,15 @@ static VALUE my_writev(VALUE io, VALUE str, int io_wait)
 		if (a.iov_cnt == 0)
 			n = 0;
 		else if (a.iov_cnt == 1)
-			n = (long)write(a.fd, a.vec[0].iov_base, a.vec[0].iov_len);
+			n = (long)write(a.fd, a.vec[0].iov_base,
+			                a.vec[0].iov_len);
 		/* for big strings use library function */
-		else if (USE_WRITEV && a.batch_len / WRITEV_IMPL_THRESHOLD > a.iov_cnt)
+		else if (USE_WRITEV &&
+		        ((a.batch_len / WRITEV_IMPL_THRESHOLD) > a.iov_cnt))
 			n = (long)writev(a.fd, a.vec, a.iov_cnt);
 		else
-			n = (long)custom_writev(a.fd, a.vec, a.iov_cnt, a.batch_len);
+			n = (long)custom_writev(a.fd, a.vec, a.iov_cnt,
+			                        a.batch_len);
 	} while (writev_check(&a, n, "writev", io_wait) != 0);
 	rb_str_resize(a.vec_buf, 0);
 
-- 
1.8.3.2.701.g8c4e4ec

[PATCH 2/3] read_write: remove the rest of RARRAY_PTR usage

From:
Eric Wong
Date:
2013-09-25 @ 19:43
From: Eric Wong <e@yhbt.net>

I have not benchmarked this, but this should not make a difference
as far as performance goes.  This should also allow better
performance of better GCs in Ruby 2.1.0 and Rubinius.
---
 ext/kgio/ancient_ruby.h |  3 ---
 ext/kgio/read_write.c   | 12 +++++-------
 2 files changed, 5 insertions(+), 10 deletions(-)

diff --git a/ext/kgio/ancient_ruby.h b/ext/kgio/ancient_ruby.h
index cda0917..fb0a80b 100644
--- a/ext/kgio/ancient_ruby.h
+++ b/ext/kgio/ancient_ruby.h
@@ -17,9 +17,6 @@ static void my_str_set_len(VALUE str, long len)
 #  define RSTRING_LEN(s) (RSTRING(s)->len)
 #endif /* !defined(RSTRING_LEN) */
 
-#ifndef RARRAY_PTR
-#  define RARRAY_PTR(s) (RARRAY(s)->ptr)
-#endif /* !defined(RARRAY_PTR) */
 #ifndef RARRAY_LEN
 #  define RARRAY_LEN(s) (RARRAY(s)->len)
 #endif /* !defined(RARRAY_LEN) */
diff --git a/ext/kgio/read_write.c b/ext/kgio/read_write.c
index f4d658c..34c619f 100644
--- a/ext/kgio/read_write.c
+++ b/ext/kgio/read_write.c
@@ -521,9 +521,7 @@ static void fill_iovec(struct io_args_v *a)
 	curvec = a->vec = (struct iovec*)RSTRING_PTR(a->vec_buf);
 
 	for (i=0; i < a->iov_cnt; i++, curvec++) {
-		/* rb_ary_store could reallocate array,
-		 * so that ought to use RARRAY_PTR */
-		VALUE str = RARRAY_PTR(a->buf)[i];
+		VALUE str = rb_ary_entry(a->buf, i);
 		long str_len, next_len;
 
 		if (TYPE(str) != T_STRING) {
@@ -551,14 +549,14 @@ static long trim_writev_buffer(struct io_args_v *a, long n)
 {
 	long i;
 	long ary_len = RARRAY_LEN(a->buf);
-	VALUE *elem = RARRAY_PTR(a->buf);
 
 	if (n == (long)a->batch_len) {
 		i = a->iov_cnt;
 		n = 0;
 	} else {
-		for (i = 0; n && i < ary_len; i++, elem++) {
-			n -= RSTRING_LEN(*elem);
+		for (i = 0; n && i < ary_len; i++) {
+			VALUE entry = rb_ary_entry(a->buf, i);
+			n -= RSTRING_LEN(entry);
 			if (n < 0) break;
 		}
 	}
@@ -576,7 +574,7 @@ static long trim_writev_buffer(struct io_args_v *a, long n)
 
 	/* setup+replace partially written buffer */
 	if (n < 0) {
-		VALUE str = RARRAY_PTR(a->buf)[0];
+		VALUE str = rb_ary_entry(a->buf, 0);
 		long str_len = RSTRING_LEN(str);
 		str = rb_str_subseq(str, str_len + n, -n);
 		rb_ary_store(a->buf, 0, str);
-- 
1.8.3.2.701.g8c4e4ec

[PATCH 2/3] read_write: remove the rest of RARRAY_PTR usage

From:
Eric Wong
Date:
2013-09-25 @ 19:42
From: Eric Wong <e@yhbt.net>

I have not benchmarked this, but this should not make a difference
as far as performance goes.  This should also allow better
performance of better GCs in Ruby 2.1.0 and Rubinius.
---
 ext/kgio/ancient_ruby.h |  3 ---
 ext/kgio/read_write.c   | 12 +++++-------
 2 files changed, 5 insertions(+), 10 deletions(-)

diff --git a/ext/kgio/ancient_ruby.h b/ext/kgio/ancient_ruby.h
index cda0917..fb0a80b 100644
--- a/ext/kgio/ancient_ruby.h
+++ b/ext/kgio/ancient_ruby.h
@@ -17,9 +17,6 @@ static void my_str_set_len(VALUE str, long len)
 #  define RSTRING_LEN(s) (RSTRING(s)->len)
 #endif /* !defined(RSTRING_LEN) */
 
-#ifndef RARRAY_PTR
-#  define RARRAY_PTR(s) (RARRAY(s)->ptr)
-#endif /* !defined(RARRAY_PTR) */
 #ifndef RARRAY_LEN
 #  define RARRAY_LEN(s) (RARRAY(s)->len)
 #endif /* !defined(RARRAY_LEN) */
diff --git a/ext/kgio/read_write.c b/ext/kgio/read_write.c
index f4d658c..34c619f 100644
--- a/ext/kgio/read_write.c
+++ b/ext/kgio/read_write.c
@@ -521,9 +521,7 @@ static void fill_iovec(struct io_args_v *a)
 	curvec = a->vec = (struct iovec*)RSTRING_PTR(a->vec_buf);
 
 	for (i=0; i < a->iov_cnt; i++, curvec++) {
-		/* rb_ary_store could reallocate array,
-		 * so that ought to use RARRAY_PTR */
-		VALUE str = RARRAY_PTR(a->buf)[i];
+		VALUE str = rb_ary_entry(a->buf, i);
 		long str_len, next_len;
 
 		if (TYPE(str) != T_STRING) {
@@ -551,14 +549,14 @@ static long trim_writev_buffer(struct io_args_v *a, long n)
 {
 	long i;
 	long ary_len = RARRAY_LEN(a->buf);
-	VALUE *elem = RARRAY_PTR(a->buf);
 
 	if (n == (long)a->batch_len) {
 		i = a->iov_cnt;
 		n = 0;
 	} else {
-		for (i = 0; n && i < ary_len; i++, elem++) {
-			n -= RSTRING_LEN(*elem);
+		for (i = 0; n && i < ary_len; i++) {
+			VALUE entry = rb_ary_entry(a->buf, i);
+			n -= RSTRING_LEN(entry);
 			if (n < 0) break;
 		}
 	}
@@ -576,7 +574,7 @@ static long trim_writev_buffer(struct io_args_v *a, long n)
 
 	/* setup+replace partially written buffer */
 	if (n < 0) {
-		VALUE str = RARRAY_PTR(a->buf)[0];
+		VALUE str = rb_ary_entry(a->buf, 0);
 		long str_len = RSTRING_LEN(str);
 		str = rb_str_subseq(str, str_len + n, -n);
 		rb_ary_store(a->buf, 0, str);
-- 
1.8.3.2.701.g8c4e4ec

[PATCH 3/3] my_writev: stylistic fixes

From:
Eric Wong
Date:
2013-09-25 @ 19:42
From: Eric Wong <e@yhbt.net>

Rename 'str' to 'ary', as that's the whole point of supporting
writev...  Also, long lines need to be wrapped at 80 columns
---
 ext/kgio/read_write.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/ext/kgio/read_write.c b/ext/kgio/read_write.c
index 34c619f..fcb48bb 100644
--- a/ext/kgio/read_write.c
+++ b/ext/kgio/read_write.c
@@ -606,12 +606,12 @@ static int writev_check(struct io_args_v *a, long n,
const char *msg, int io_wai
 	return 0;
 }
 
-static VALUE my_writev(VALUE io, VALUE str, int io_wait)
+static VALUE my_writev(VALUE io, VALUE ary, int io_wait)
 {
 	struct io_args_v a;
 	long n;
 
-	prepare_writev(&a, io, str);
+	prepare_writev(&a, io, ary);
 	set_nonblocking(a.fd);
 
 	do {
@@ -619,12 +619,15 @@ static VALUE my_writev(VALUE io, VALUE str, int io_wait)
 		if (a.iov_cnt == 0)
 			n = 0;
 		else if (a.iov_cnt == 1)
-			n = (long)write(a.fd, a.vec[0].iov_base, a.vec[0].iov_len);
+			n = (long)write(a.fd, a.vec[0].iov_base,
+			                a.vec[0].iov_len);
 		/* for big strings use library function */
-		else if (USE_WRITEV && a.batch_len / WRITEV_IMPL_THRESHOLD > a.iov_cnt)
+		else if (USE_WRITEV &&
+		        ((a.batch_len / WRITEV_IMPL_THRESHOLD) > a.iov_cnt))
 			n = (long)writev(a.fd, a.vec, a.iov_cnt);
 		else
-			n = (long)custom_writev(a.fd, a.vec, a.iov_cnt, a.batch_len);
+			n = (long)custom_writev(a.fd, a.vec, a.iov_cnt,
+			                        a.batch_len);
 	} while (writev_check(&a, n, "writev", io_wait) != 0);
 	rb_str_resize(a.vec_buf, 0);
 
-- 
1.8.3.2.701.g8c4e4ec