From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS43317 94.242.0.0/18 X-Spam-Status: No, score=-1.8 required=3.0 tests=BAYES_00,RCVD_IN_MSPIKE_BL, RCVD_IN_MSPIKE_ZBI,RCVD_IN_XBL,RDNS_NONE,SPF_FAIL,SPF_HELO_FAIL, TO_EQ_FM_DOM_SPF_FAIL shortcircuit=no autolearn=no autolearn_force=no version=3.4.0 Received: from 80x24.org (unknown [94.242.55.221]) by dcvr.yhbt.net (Postfix) with ESMTP id 6C97A20441 for ; Thu, 29 Dec 2016 00:57:03 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] io: quick-n-dirty try to use locktmp to avoid alloc in write Date: Thu, 29 Dec 2016 00:57:01 +0000 Message-Id: <20161229005701.9712-1-e@80x24.org> List-Id: 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