dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] io.c (io_fwrite): thread-local write buffer
@ 2016-12-29  0:44 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2016-12-29  0:44 UTC (permalink / raw)
  To: spew

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


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2016-12-29  0:44 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-29  0:44 [PATCH] io.c (io_fwrite): thread-local write buffer Eric Wong

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