Skip to content

add sizehint keyword argument to IOBuffer#25944

Merged
JeffBezanson merged 8 commits into
JuliaLang:masterfrom
bicycle1885:iobuffer-sizehint
Feb 9, 2018
Merged

add sizehint keyword argument to IOBuffer#25944
JeffBezanson merged 8 commits into
JuliaLang:masterfrom
bicycle1885:iobuffer-sizehint

Conversation

@bicycle1885

Copy link
Copy Markdown
Member

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

Comment thread 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
Copy Markdown
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.

@stevengj stevengj Feb 9, 2018

Copy link
Copy Markdown
Member

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
Copy Markdown
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.

Comment thread base/strings/basic.jl Outdated

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
Copy Markdown
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
Copy Markdown
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)).

Comment thread base/strings/io.jl Outdated
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)

@stevengj stevengj Feb 8, 2018

Copy link
Copy Markdown
Member

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)?

Comment thread base/strings/io.jl Outdated
s = IOBuffer(sizehint=sizehint)
# specialized version of truncate(s,0)
s.size = 0
s.ptr = 1

Copy link
Copy Markdown
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?

Comment thread base/strings/io.jl Outdated
s = IOBuffer(sizehint=tostr_sizehint(xs[1]))
# specialized version of truncate(s,0)
s.size = 0
s.ptr = 1

Copy link
Copy Markdown
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?

Comment thread base/strings/io.jl
function print_to_string(xs...; env=nothing)
if isempty(xs)
return ""
end

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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