dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH] io.c (io_fwrite): thread-local write buffer
Date: Thu, 29 Dec 2016 00:44:17 +0000	[thread overview]
Message-ID: <20161229004417.12304-1-e@80x24.org> (raw)

worthless patch because rb_str_replace creates new frozen
object behind our back, so it doesn't save memory.
---
 internal.h |  1 +
 io.c       | 14 ++++++++++----
 thread.c   | 11 +++++++++++
 vm.c       |  2 ++
 vm_core.h  |  1 +
 5 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/internal.h b/internal.h
index 2346345480..f90463030a 100644
--- a/internal.h
+++ b/internal.h
@@ -1521,6 +1521,7 @@ void rb_mutex_allow_trap(VALUE self, int val);
 VALUE rb_uninterruptible(VALUE (*b_proc)(ANYARGS), VALUE data);
 VALUE rb_mutex_owned_p(VALUE self);
 void ruby_kill(rb_pid_t pid, int sig);
+VALUE rb_thread_iotmp(void);
 
 /* thread_pthread.c, thread_win32.c */
 void Init_native_thread(void);
diff --git a/io.c b/io.c
index 1074689560..02c6025ef0 100644
--- a/io.c
+++ b/io.c
@@ -1423,6 +1423,7 @@ static long
 io_fwrite(VALUE str, rb_io_t *fptr, int nosync)
 {
     int converted = 0;
+    long w;
 #ifdef _WIN32
     if (fptr->mode & FMODE_TTY) {
 	long len = rb_w32_write_console(str, fptr->fd);
@@ -1431,12 +1432,17 @@ io_fwrite(VALUE str, rb_io_t *fptr, int nosync)
 #endif
     str = do_writeconv(str, fptr, &converted);
     if (converted)
-	OBJ_FREEZE(str);
-    else
-	str = rb_str_new_frozen(str);
+	RBASIC_CLEAR_CLASS(str);
+    else if (!OBJ_FROZEN(str))
+	str = rb_str_replace(rb_thread_iotmp(), str);
 
-    return io_binwrite(str, RSTRING_PTR(str), RSTRING_LEN(str),
+    w = io_binwrite(str, RSTRING_PTR(str), RSTRING_LEN(str),
 		       fptr, nosync);
+
+    if (!RBASIC_CLASS(str))
+	rb_str_resize(str, 0);
+
+    return w;
 }
 
 ssize_t
diff --git a/thread.c b/thread.c
index 71e479d38c..360b4c447f 100644
--- a/thread.c
+++ b/thread.c
@@ -2905,6 +2905,17 @@ rb_thread_getname(VALUE thread)
     return th->name;
 }
 
+VALUE
+rb_thread_iotmp(void)
+{
+    rb_thread_t *th = GET_THREAD();
+
+    if (!RTEST(th->iotmp)) {
+	th->iotmp = rb_str_tmp_new(0);
+    }
+    return th->iotmp;
+}
+
 /*
  * call-seq:
  *   thr.name=(name)   -> string
diff --git a/vm.c b/vm.c
index b00a3e3505..66725090f4 100644
--- a/vm.c
+++ b/vm.c
@@ -2342,6 +2342,7 @@ rb_thread_mark(void *ptr)
     }
 
     RUBY_MARK_UNLESS_NULL(th->name);
+    RUBY_MARK_UNLESS_NULL(th->iotmp);
 
     rb_vm_trace_mark_event_hooks(&th->event_hooks);
 
@@ -2474,6 +2475,7 @@ th_init(rb_thread_t *th, VALUE self)
     th->retval = Qundef;
 #endif
     th->name = Qnil;
+    th->iotmp = Qnil;
 }
 
 static VALUE
diff --git a/vm_core.h b/vm_core.h
index ff5ff90a56..3916de8f1b 100644
--- a/vm_core.h
+++ b/vm_core.h
@@ -818,6 +818,7 @@ typedef struct rb_thread_struct {
 #endif
     unsigned long running_time_us;
     VALUE name;
+    VALUE iotmp;
 } rb_thread_t;
 
 typedef enum {
-- 
EW


                 reply	other threads:[~2016-12-29  0:44 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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

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

  git send-email \
    --in-reply-to=20161229004417.12304-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.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.
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).