1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* io.c (copy_stream_fallback): write directly (bypassing write method)

if possible.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16090 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2008-04-20 04:59:04 +00:00
parent f0fbfeda51
commit 01c433b58c
3 changed files with 35 additions and 12 deletions

View file

@ -1,7 +1,12 @@
Sun Apr 20 13:55:37 2008 Tanaka Akira <akr@fsij.org>
* io.c (copy_stream_fallback): write directly (bypassing write method)
if possible.
Sun Apr 20 12:49:03 2008 Tanaka Akira <akr@fsij.org> Sun Apr 20 12:49:03 2008 Tanaka Akira <akr@fsij.org>
* io.c (copy_stream_fallback): read directly (bypassing readpartial) * io.c (copy_stream_fallback): read directly (bypassing readpartial
if possible. method) if possible.
Sun Apr 20 04:45:13 2008 Tanaka Akira <akr@fsij.org> Sun Apr 20 04:45:13 2008 Tanaka Akira <akr@fsij.org>

11
io.c
View file

@ -6579,7 +6579,7 @@ copy_stream_fallback_body(VALUE arg)
while (1) { while (1) {
long numwrote; long numwrote;
long l = buflen < rest ? buflen : rest; long l;
if (stp->copy_length == (off_t)-1) { if (stp->copy_length == (off_t)-1) {
l = buflen; l = buflen;
} }
@ -6604,9 +6604,18 @@ copy_stream_fallback_body(VALUE arg)
if (off != (off_t)-1) if (off != (off_t)-1)
off += ss; off += ss;
} }
if (stp->dst_fd == -1) {
n = rb_io_write(stp->dst, buf); n = rb_io_write(stp->dst, buf);
numwrote = NUM2LONG(n); numwrote = NUM2LONG(n);
stp->total += numwrote; stp->total += numwrote;
}
else {
ssize_t ss;
numwrote = RSTRING_LEN(buf);
ss = copy_stream_write(stp, RSTRING_PTR(buf), numwrote);
if (ss == -1)
return Qnil;
}
rest -= numwrote; rest -= numwrote;
} }

View file

@ -424,25 +424,30 @@ class TestIO < Test::Unit::TestCase
} }
end end
def test_copy_stream_non_io def test_copy_stream_fname_to_strio
mkcdtmpdir {|d| mkcdtmpdir {|d|
# filename to StringIO
File.open("foo", "w") {|f| f << "abcd" } File.open("foo", "w") {|f| f << "abcd" }
src = "foo" src = "foo"
dst = StringIO.new dst = StringIO.new
ret = IO.copy_stream(src, dst, 3) ret = IO.copy_stream(src, dst, 3)
assert_equal(3, ret) assert_equal(3, ret)
assert_equal("abc", dst.string) assert_equal("abc", dst.string)
}
end
def test_copy_stream_strio_to_fname
mkcdtmpdir {|d|
# StringIO to filename # StringIO to filename
src = StringIO.new("abcd") src = StringIO.new("abcd")
ret = File.open("fooo", "w") {|dst| ret = IO.copy_stream(src, "fooo", 3)
IO.copy_stream(src, dst, 3)
}
assert_equal(3, ret) assert_equal(3, ret)
assert_equal("abc", dst.string) assert_equal("abc", File.read("fooo"))
assert_equal(3, src.pos) assert_equal(3, src.pos)
}
end
def test_copy_stream_io_to_strio
mkcdtmpdir {|d|
# IO to StringIO # IO to StringIO
File.open("bar", "w") {|f| f << "abcd" } File.open("bar", "w") {|f| f << "abcd" }
File.open("bar") {|src| File.open("bar") {|src|
@ -452,7 +457,11 @@ class TestIO < Test::Unit::TestCase
assert_equal("abc", dst.string) assert_equal("abc", dst.string)
assert_equal(3, src.pos) assert_equal(3, src.pos)
} }
}
end
def test_copy_stream_strio_to_io
mkcdtmpdir {|d|
# StringIO to IO # StringIO to IO
src = StringIO.new("abcd") src = StringIO.new("abcd")
ret = File.open("baz", "w") {|dst| ret = File.open("baz", "w") {|dst|