Skip to content

Commit

Permalink
add sizehint keyword argument to IOBuffer (#25944)
Browse files Browse the repository at this point in the history
  • Loading branch information
bicycle1885 authored and JeffBezanson committed Feb 9, 2018
1 parent 94197a4 commit e0c93aa
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,9 @@ Library improvements
like other `AbstractDict` subtypes and its constructors mirror the
ones of `Dict`. ([#25210])

* `IOBuffer` can take the `sizehint` keyword argument to suggest a capacity of
the buffer ([#25944]).

Compiler/Runtime improvements
-----------------------------

Expand Down
12 changes: 9 additions & 3 deletions base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ It may take optional keyword arguments:
- `read`, `write`, `append`: restricts operations to the buffer; see `open` for details.
- `truncate`: truncates the buffer size to zero length.
- `maxsize`: specifies a size beyond which the buffer may not be grown.
- `sizehint`: suggests a capacity of the buffer (`data` must implement `sizehint!(data, size)`).
When `data` is not given, the buffer will be both readable and writable by default.
Expand Down Expand Up @@ -84,10 +85,14 @@ function IOBuffer(
write::Union{Bool,Nothing}=nothing,
append::Union{Bool,Nothing}=nothing,
truncate::Union{Bool,Nothing}=nothing,
maxsize::Integer=typemax(Int))
maxsize::Integer=typemax(Int),
sizehint::Union{Integer,Nothing}=nothing)
if maxsize < 0
throw(ArgumentError("negative maxsize: $(maxsize)"))
end
if sizehint !== nothing
sizehint!(data, sizehint)
end
flags = open_flags(read=read, write=write, append=append, truncate=truncate)
buf = GenericIOBuffer(data, flags.read, flags.write, true, flags.append, Int(maxsize))
if flags.truncate
Expand All @@ -101,8 +106,9 @@ function IOBuffer(;
write::Union{Bool,Nothing}=true,
append::Union{Bool,Nothing}=nothing,
truncate::Union{Bool,Nothing}=true,
maxsize::Integer=typemax(Int))
size = maxsize == typemax(Int) ? 32 : Int(maxsize)
maxsize::Integer=typemax(Int),
sizehint::Union{Integer,Nothing}=nothing)
size = sizehint !== nothing ? Int(sizehint) : maxsize != typemax(Int) ? Int(maxsize) : 32
flags = open_flags(read=read, write=write, append=append, truncate=truncate)
buf = IOBuffer(
StringVector(size),
Expand Down
6 changes: 2 additions & 4 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,7 @@ isascii(s::AbstractString) = all(isascii, s)
## string map, filter, has ##

function map(f, s::AbstractString)
out = IOBuffer(StringVector(sizeof(s)), read=true, write=true)
truncate(out, 0)
out = IOBuffer(sizehint=sizeof(s))
for c in s
c′ = f(c)
isa(c′, Char) || throw(ArgumentError(
Expand All @@ -525,8 +524,7 @@ function map(f, s::AbstractString)
end

function filter(f, s::AbstractString)
out = IOBuffer(StringVector(sizeof(s)), read=true, write=true)
truncate(out, 0)
out = IOBuffer(sizehint=sizeof(s))
for c in s
f(c) && write(out, c)
end
Expand Down
16 changes: 6 additions & 10 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ julia> sprint(showcompact, 66.66666)
```
"""
function sprint(f::Function, args...; context=nothing, sizehint::Integer=0)
s = IOBuffer(StringVector(sizehint), read=true, write=true)
# specialized version of truncate(s,0)
s.size = 0
s.ptr = 1
s = IOBuffer(sizehint=sizehint)
if context !== nothing
f(IOContext(s, context), args...)
else
Expand All @@ -99,11 +96,11 @@ tostr_sizehint(x::Float64) = 20
tostr_sizehint(x::Float32) = 12

function print_to_string(xs...; env=nothing)
if isempty(xs)
return ""
end
# specialized for performance reasons
s = IOBuffer(StringVector(tostr_sizehint(xs[1])), read=true, write=true)
# specialized version of truncate(s,0)
s.size = 0
s.ptr = 1
s = IOBuffer(sizehint=tostr_sizehint(xs[1]))
if env !== nothing
env_io = IOContext(s, env)
for x in xs
Expand Down Expand Up @@ -436,8 +433,7 @@ Returns:
function unindent(str::AbstractString, indent::Int; tabwidth=8)
indent == 0 && return str
# Note: this loses the type of the original string
buf = IOBuffer(StringVector(sizeof(str)), read=true, write=true)
truncate(buf,0)
buf = IOBuffer(sizehint=sizeof(str))
cutting = true
col = 0 # current column (0 based)
for ch in str
Expand Down
4 changes: 1 addition & 3 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,7 @@ function replace(str::String, pat_repl::Pair; count::Integer=typemax(Int))
i = a = firstindex(str)
r = coalesce(findnext(pattern,str,i), 0)
j, k = first(r), last(r)
out = IOBuffer(StringVector(floor(Int, 1.2sizeof(str))), read=true, write=true)
out.size = 0
out.ptr = 1
out = IOBuffer(sizehint=floor(Int, 1.2sizeof(str)))
while j != 0
if i == a || i <= k
unsafe_write(out, pointer(str, i), UInt(j-i))
Expand Down

0 comments on commit e0c93aa

Please sign in to comment.