summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTOMITA Masahiro <[email protected]>2014-12-18 00:29:15 +0900
committerTOMITA Masahiro <[email protected]>2014-12-18 00:41:59 +0900
commit42752bffce566cdfe945db34101ff42d406918c9 (patch)
treede7e1626c1182cbfe1e4ef77a529ae8c2a4ab77c
parente7dbe6e87aac11762e35112ff6130f3812c85478 (diff)
downloadmruby-42752bffce566cdfe945db34101ff42d406918c9.tar.gz
mruby-42752bffce566cdfe945db34101ff42d406918c9.zip
FIX: IO#read create a large number of objects.
-rw-r--r--mrblib/io.rb34
1 files changed, 18 insertions, 16 deletions
diff --git a/mrblib/io.rb b/mrblib/io.rb
index fceea1171..be715ba7a 100644
--- a/mrblib/io.rb
+++ b/mrblib/io.rb
@@ -183,29 +183,30 @@ class IO
end
end
- str = ''
+ array = []
+ start_pos = @pos
while 1
begin
_read_buf
rescue EOFError => e
- str = nil if str.empty? and (not length.nil?) and length != 0
+ array = nil if array.empty? and (not length.nil?) and length != 0
break
end
- if length && (str.size + @buf.size) >= length
- len = length - str.size
- str += @buf[0, len]
+ if length && (@pos - start_pos + @buf.size) >= length
+ len = length - (@pos - start_pos)
+ array.push @buf[0, len]
@pos += len
@buf = @buf[len, @buf.size - len]
break
else
- str += @buf
+ array.push @buf
@pos += @buf.size
@buf = ''
end
end
- str
+ array && array.join
end
def readline(arg = $/, limit = nil)
@@ -227,37 +228,38 @@ class IO
rs = $/ + $/
end
- str = ""
+ array = []
+ start_pos = @pos
while 1
begin
_read_buf
rescue EOFError => e
- str = nil if str.empty?
+ array = nil if array.empty?
break
end
- if limit && (str.size + @buf.size) >= limit
- len = limit - str.size
- str += @buf[0, len]
+ if limit && (@pos - start_pos + @buf.size) >= limit
+ len = limit - (@pos - start_pos)
+ array.push @buf[0, len]
@pos += len
@buf = @buf[len, @buf.size - len]
break
elsif idx = @buf.index(rs)
len = idx + rs.size
- str += @buf[0, len]
+ array.push @buf[0, len]
@pos += len
@buf = @buf[len, @buf.size - len]
break
else
- str += @buf
+ array.push @buf
@pos += @buf.size
@buf = ''
end
end
- raise EOFError.new "end of file reached" if str.nil?
+ raise EOFError.new "end of file reached" if array.nil?
- str
+ array.join
end
def gets(*args)