Skip to content

Commit fc6047b

Browse files
committed
copyuntil: reduce over-allocation to start
This fits into a 32-byte allocation pool, saving up to 64 bytes when repeatedly reading small chunks of data (e.g. tokenizing a CSV file). In some local `@btime` measurements, this seems to take <10% more time across a range of output lengths.
1 parent 1d7b036 commit fc6047b

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

base/io.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,8 @@ julia> rm("my_file.txt")
543543
```
544544
"""
545545
readuntil(filename::AbstractString, delim; kw...) = open(io->readuntil(io, delim; kw...), convert(String, filename)::String)
546-
readuntil(stream::IO, delim::UInt8; kw...) = _unsafe_take!(copyuntil(IOBuffer(sizehint=70), stream, delim; kw...))
547-
readuntil(stream::IO, delim::Union{AbstractChar, AbstractString}; kw...) = String(_unsafe_take!(copyuntil(IOBuffer(sizehint=70), stream, delim; kw...)))
546+
readuntil(stream::IO, delim::UInt8; kw...) = _unsafe_take!(copyuntil(IOBuffer(sizehint=16), stream, delim; kw...))
547+
readuntil(stream::IO, delim::Union{AbstractChar, AbstractString}; kw...) = String(_unsafe_take!(copyuntil(IOBuffer(sizehint=16), stream, delim; kw...)))
548548
readuntil(stream::IO, delim::T; keep::Bool=false) where T = _copyuntil(Vector{T}(), stream, delim, keep)
549549

550550

@@ -617,7 +617,7 @@ Logan
617617
readline(filename::AbstractString; keep::Bool=false) =
618618
open(io -> readline(io; keep), filename)
619619
readline(s::IO=stdin; keep::Bool=false) =
620-
String(_unsafe_take!(copyline(IOBuffer(sizehint=70), s; keep)))
620+
String(_unsafe_take!(copyline(IOBuffer(sizehint=16), s; keep)))
621621

622622
"""
623623
copyline(out::IO, io::IO=stdin; keep::Bool=false)
@@ -1111,7 +1111,7 @@ function copyuntil(out::IO, io::IO, target::AbstractString; keep::Bool=false)
11111111
end
11121112

11131113
function readuntil(io::IO, target::AbstractVector{T}; keep::Bool=false) where T
1114-
out = (T === UInt8 ? resize!(StringVector(70), 0) : Vector{T}())
1114+
out = (T === UInt8 ? resize!(StringVector(16), 0) : Vector{T}())
11151115
readuntil_vector!(io, target, keep, out)
11161116
return out
11171117
end

0 commit comments

Comments
 (0)