about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorSokolov Yura 'funny-falcon <funny.falcon@gmail.com>2012-05-30 17:56:55 +0400
committerEric Wong <normalperson@yhbt.net>2012-05-30 11:58:08 -0700
commita72e6cd0dd3038ae2a1b5ef94780143f5ab041c0 (patch)
treed30334a958f52c80b88419c674416b4c727923b4
parent021eaddbfb41d82c0082657f60021bad52b3a6dc (diff)
downloadkgio-a72e6cd0dd3038ae2a1b5ef94780143f5ab041c0.tar.gz
Use rb_str_subseq for taking string's tail. rb_str_subseq do not allocate
additional memory in this case. And although it prevents from collecting
original string, it seems that tests wins both in performance and in memory
usage.

Use fallback to rb_str_substr on ruby1.8

Signed-off-by: Eric Wong <normalperson@yhbt.net>
-rw-r--r--ext/kgio/extconf.rb1
-rw-r--r--ext/kgio/read_write.c5
2 files changed, 5 insertions, 1 deletions
diff --git a/ext/kgio/extconf.rb b/ext/kgio/extconf.rb
index fb680f7..f6bd0cc 100644
--- a/ext/kgio/extconf.rb
+++ b/ext/kgio/extconf.rb
@@ -49,5 +49,6 @@ have_func('rb_thread_io_blocking_region')
 have_func('rb_str_set_len')
 have_func('rb_time_interval')
 have_func('rb_wait_for_single_fd')
+have_func('rb_str_subseq')
 
 create_makefile('kgio_ext')
diff --git a/ext/kgio/read_write.c b/ext/kgio/read_write.c
index 51d2d16..9924743 100644
--- a/ext/kgio/read_write.c
+++ b/ext/kgio/read_write.c
@@ -4,6 +4,9 @@
 static VALUE sym_wait_readable, sym_wait_writable;
 static VALUE eErrno_EPIPE, eErrno_ECONNRESET;
 static ID id_set_backtrace;
+#ifndef HAVE_RB_STR_SUBSEQ
+#define rb_str_subseq rb_str_substr
+#endif
 
 /*
  * we know MSG_DONTWAIT works properly on all stream sockets under Linux
@@ -338,7 +341,7 @@ done:
                                 a->ptr = RSTRING_PTR(a->buf) + written;
                                 return -1;
                         } else if (written > 0) {
-                                a->buf = rb_str_new(a->ptr, a->len);
+                                a->buf = rb_str_subseq(a->buf, written, a->len);
                         } else {
                                 a->buf = sym_wait_writable;
                         }