Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sizehint keyword argument to IOBuffer #25944

Merged
merged 8 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isempty(xs) && return "" would be more idiomatic (and shorter).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a personal taste rather than an idiom. I'd like to use if in this kind of case because it is easier to read control flow.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The && syntax is widely used in Base to check for small corner cases at the beginning of functions. Hence I would say it is idiomatic in Base. (The word “idiomatic” refers to choices that are questions of taste/usage rather than grammar.)

Obviously the two are technically equivalent, so I won’t insist.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(The question in cases like this is not your personal taste, but rather the dominant style of the project being patched.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I can find lots of if <condition> <newline> <single statement> <newline> end usage in Base (I don't know stats on which is more dominant) and cannot find a guideline on the style, so I used the words "personal taste" here. Anyway, I think this is off-topic.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both styles are definitely acceptable.

# 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