Skip to content

Commit

Permalink
copyuntil: reduce over-allocation to start
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vtjnash committed Aug 8, 2024
1 parent 1d7b036 commit fc6047b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,8 @@ julia> rm("my_file.txt")
```
"""
readuntil(filename::AbstractString, delim; kw...) = open(io->readuntil(io, delim; kw...), convert(String, filename)::String)
readuntil(stream::IO, delim::UInt8; kw...) = _unsafe_take!(copyuntil(IOBuffer(sizehint=70), stream, delim; kw...))
readuntil(stream::IO, delim::Union{AbstractChar, AbstractString}; kw...) = String(_unsafe_take!(copyuntil(IOBuffer(sizehint=70), stream, delim; kw...)))
readuntil(stream::IO, delim::UInt8; kw...) = _unsafe_take!(copyuntil(IOBuffer(sizehint=16), stream, delim; kw...))
readuntil(stream::IO, delim::Union{AbstractChar, AbstractString}; kw...) = String(_unsafe_take!(copyuntil(IOBuffer(sizehint=16), stream, delim; kw...)))
readuntil(stream::IO, delim::T; keep::Bool=false) where T = _copyuntil(Vector{T}(), stream, delim, keep)


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

"""
copyline(out::IO, io::IO=stdin; keep::Bool=false)
Expand Down Expand Up @@ -1111,7 +1111,7 @@ function copyuntil(out::IO, io::IO, target::AbstractString; keep::Bool=false)
end

function readuntil(io::IO, target::AbstractVector{T}; keep::Bool=false) where T
out = (T === UInt8 ? resize!(StringVector(70), 0) : Vector{T}())
out = (T === UInt8 ? resize!(StringVector(16), 0) : Vector{T}())
readuntil_vector!(io, target, keep, out)
return out
end
Expand Down

0 comments on commit fc6047b

Please sign in to comment.