dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] io: quick-n-dirty try to use locktmp to avoid alloc in write
@ 2016-12-29  0:57 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2016-12-29  0:57 UTC (permalink / raw)
  To: spew

This causs failures with TestIO#test_threaded_flush
---
 io.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)

diff --git a/io.c b/io.c
index 1074689560..bee2435fad 100644
--- a/io.c
+++ b/io.c
@@ -1222,6 +1222,7 @@ struct binwrite_arg {
     VALUE str;
     const char *ptr;
     long length;
+    int nosync;
 };
 
 struct write_arg {
@@ -1419,10 +1420,20 @@ do_writeconv(VALUE str, rb_io_t *fptr, int *converted)
     return str;
 }
 
+static VALUE
+binwrite_call(VALUE arg)
+{
+    struct binwrite_arg *p = (struct binwrite_arg*)arg;
+
+    p->length = io_binwrite(p->str, p->ptr, p->length, p->fptr, p->nosync);
+    return Qundef;
+}
+
 static long
 io_fwrite(VALUE str, rb_io_t *fptr, int nosync)
 {
     int converted = 0;
+    struct binwrite_arg arg;
 #ifdef _WIN32
     if (fptr->mode & FMODE_TTY) {
 	long len = rb_w32_write_console(str, fptr->fd);
@@ -1432,11 +1443,19 @@ io_fwrite(VALUE str, rb_io_t *fptr, int nosync)
     str = do_writeconv(str, fptr, &converted);
     if (converted)
 	OBJ_FREEZE(str);
-    else
-	str = rb_str_new_frozen(str);
+    if (OBJ_FROZEN(str))
+	return io_binwrite(str, RSTRING_PTR(str), RSTRING_LEN(str),
+		   fptr, nosync);
+
+    arg.fptr = fptr;
+    arg.str = str;
+    arg.ptr = RSTRING_PTR(str);
+    arg.length = RSTRING_LEN(str);
+    arg.nosync = nosync;
+
+    rb_str_locktmp_ensure(str, binwrite_call, (VALUE)&arg);
 
-    return io_binwrite(str, RSTRING_PTR(str), RSTRING_LEN(str),
-		       fptr, nosync);
+    return arg.length;
 }
 
 ssize_t
-- 
EW


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

only message in thread, other threads:[~2016-12-29  0:57 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:57 [PATCH] io: quick-n-dirty try to use locktmp to avoid alloc in write 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).