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

Conversation

bicycle1885
Copy link
Member

This adds sizehint keyword argument to IOBuffer as suggested by @stevengj in #25872 (comment).

base/iobuffer.jl Outdated
size = maxsize == typemax(Int) ? 32 : Int(maxsize)
maxsize::Integer=typemax(Int),
sizehint::Union{Integer,Nothing}=nothing)
size = maxsize != typemax(Int) ? Int(maxsize) : sizehint !== nothing ? Int(sizehint) : 32
Copy link
Member

Choose a reason for hiding this comment

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

It seems like sizehint should take precedence over maxsize here.

Copy link
Member

@stevengj stevengj Feb 9, 2018

Choose a reason for hiding this comment

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

e.g. consider a case where maxsize is 2GB (because the buffer will be sent someplace 32-bit) but sizehint is 8. We probably don't want to allocate 2GB.

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 forgot to fix that. Now fixed. Thank you.

@@ -525,7 +525,7 @@ function map(f, s::AbstractString)
end

function filter(f, s::AbstractString)
out = IOBuffer(StringVector(sizeof(s)), read=true, write=true)
out = IOBuffer(sizehint=sizeof(s), read=true, write=true)
Copy link
Member

Choose a reason for hiding this comment

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

We no longer have to pass read=true, write=true, since this is the default when no data buffer is passed to the constructor. (Similarly elsewhere.)

Copy link
Member

Choose a reason for hiding this comment

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

i.e. just call IOBuffer(sizehint=sizeof(s)).

@@ -100,7 +100,7 @@ tostr_sizehint(x::Float32) = 12

function print_to_string(xs...; env=nothing)
# specialized for performance reasons
s = IOBuffer(StringVector(tostr_sizehint(xs[1])), read=true, write=true)
s = IOBuffer(sizehint=tostr_sizehint(xs[1]), read=true, write=true)
Copy link
Member

@stevengj stevengj Feb 8, 2018

Choose a reason for hiding this comment

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

Shouldn't there be an isempty(xs) && return "" check? Or is this an internal function that can never be called on an empty argument list, in which case there should be an @assert !isempty(xs)?

@@ -81,7 +81,7 @@ julia> sprint(showcompact, 66.66666)
```
"""
function sprint(f::Function, args...; context=nothing, sizehint::Integer=0)
s = IOBuffer(sizehint=sizehint, read=true, write=true)
s = IOBuffer(sizehint=sizehint)
# specialized version of truncate(s,0)
s.size = 0
s.ptr = 1
Copy link
Member

Choose a reason for hiding this comment

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

Can't the these three lines be deleted?

@@ -100,7 +100,7 @@ tostr_sizehint(x::Float32) = 12

function print_to_string(xs...; env=nothing)
# specialized for performance reasons
s = IOBuffer(sizehint=tostr_sizehint(xs[1]), read=true, write=true)
s = IOBuffer(sizehint=tostr_sizehint(xs[1]))
# specialized version of truncate(s,0)
s.size = 0
s.ptr = 1
Copy link
Member

Choose a reason for hiding this comment

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

Can't the these three lines be deleted?

@@ -96,6 +96,9 @@ 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.

@JeffBezanson JeffBezanson merged commit e0c93aa into JuliaLang:master Feb 9, 2018
@bicycle1885 bicycle1885 deleted the iobuffer-sizehint branch February 10, 2018 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants