summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
authornaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-03-12 18:15:33 +0000
committernaruse <naruse@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-03-12 18:15:33 +0000
commitbaf330be0a49c4764bcff89335c4ce9ba2beee8d (patch)
treee69a7e5c7032f0f6c2204da598efc3d6a50b4665 /io.c
parentfb6d5b906037ef0e957446fc696e6a7d07790325 (diff)
merge revision(s) 57469,57471,57472,57503,57508: [Backport #13299]
io.c: recycle garbage on write * string.c (STR_IS_SHARED_M): new flag to mark shared mulitple times (STR_SET_SHARED): set STR_IS_SHARED_M (rb_str_tmp_frozen_acquire, rb_str_tmp_frozen_release): new functions (str_new_frozen): set/unset STR_IS_SHARED_M as appropriate * internal.h: declare new functions * io.c (fwrite_arg, fwrite_do, fwrite_end): new (io_fwrite): use new functions Introduce rb_str_tmp_frozen_acquire and rb_str_tmp_frozen_release to manage a hidden, frozen string. Reuse one bit of the embed length for shared strings as STR_IS_SHARED_M to indicate a string has been shared multiple times. In the common case, the string is only shared once so the object slot can be reclaimed immediately. minimum results in each 3 measurements. (time and size) Execution time (sec) name trunk built io_copy_stream_write 0.682 0.254 io_copy_stream_write_socket 1.225 0.751 Speedup ratio: compare with the result of `trunk' (greater is better) name built io_copy_stream_write 2.680 io_copy_stream_write_socket 1.630 Memory usage (last size) (B) name trunk built io_copy_stream_write 95436800.000 6512640.000 io_copy_stream_write_socket 117628928.000 7127040.000 Memory consuming ratio (size) with the result of `trunk' (greater is better) name built io_copy_stream_write 14.654 io_copy_stream_write_socket 16.505 string.c (rb_str_tmp_frozen_release): release embedded strings Handle the embedded case first, since we may have an embedded duplicate and non-embedded original string. * string.c (rb_str_tmp_frozen_release): handled embedded strings * test/ruby/test_io.rb (test_write_no_garbage): new test [ruby-core:78898] [Bug #13085] io.c (rb_io_syswrite): avoid leaving garbage after write As with IO#write, IO#syswrite also generates garbage which can be harmful in hand-coded read-write loops. * io.c (swrite_arg, swrite_do, swrite_end): new (rb_io_syswrite): use new functions to cleanup garbage [ruby-core:78898] [Bug #13085] Add class name to assert messages io.c: remove rb_ensure usage for rb_str_tmp_frozen_* calls Using rb_ensure pessimizes the common case and makes the code more difficult to read and follow. If we hit an exceptions during write, just let the GC handle cleanup as the exception is already bad for garbage. * io.c (io_fwrite): call rb_str_tmp_frozen{acquire,release} directly (rb_io_syswrite): ditto (fwrite_do, fwrite_end, swrite_do, swrite_end): remove git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@57941 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'io.c')
-rw-r--r--io.c26
1 files changed, 16 insertions, 10 deletions
diff --git a/io.c b/io.c
index cca4b55675..8bd5977df3 100644
--- a/io.c
+++ b/io.c
@@ -1423,6 +1423,9 @@ static long
io_fwrite(VALUE str, rb_io_t *fptr, int nosync)
{
int converted = 0;
+ VALUE tmp;
+ long n, len;
+ const char *ptr;
#ifdef _WIN32
if (fptr->mode & FMODE_TTY) {
long len = rb_w32_write_console(str, fptr->fd);
@@ -1432,11 +1435,13 @@ 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);
- return io_binwrite(str, RSTRING_PTR(str), RSTRING_LEN(str),
- fptr, nosync);
+ tmp = rb_str_tmp_frozen_acquire(str);
+ RSTRING_GETMEM(tmp, ptr, len);
+ n = io_binwrite(tmp, ptr, len, fptr, nosync);
+ rb_str_tmp_frozen_release(str, tmp);
+
+ return n;
}
ssize_t
@@ -4727,8 +4732,10 @@ rb_io_sysseek(int argc, VALUE *argv, VALUE io)
static VALUE
rb_io_syswrite(VALUE io, VALUE str)
{
+ VALUE tmp;
rb_io_t *fptr;
- long n;
+ long n, len;
+ const char *ptr;
if (!RB_TYPE_P(str, T_STRING))
str = rb_obj_as_string(str);
@@ -4737,16 +4744,15 @@ rb_io_syswrite(VALUE io, VALUE str)
GetOpenFile(io, fptr);
rb_io_check_writable(fptr);
- str = rb_str_new_frozen(str);
-
if (fptr->wbuf.len) {
rb_warn("syswrite for buffered IO");
}
- n = rb_write_internal(fptr->fd, RSTRING_PTR(str), RSTRING_LEN(str));
- RB_GC_GUARD(str);
-
+ tmp = rb_str_tmp_frozen_acquire(str);
+ RSTRING_GETMEM(tmp, ptr, len);
+ n = rb_write_internal(fptr->fd, ptr, len);
if (n == -1) rb_sys_fail_path(fptr->pathv);
+ rb_str_tmp_frozen_release(str, tmp);
return LONG2FIX(n);
}