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

Improve sizehint! docstring about shrink arg #52226

Merged
merged 6 commits into from
Dec 11, 2023
Merged
Changes from 1 commit
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
8 changes: 5 additions & 3 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,7 @@ end
"""
sizehint!(s, n; first::Bool=false, shrink::Bool=true) -> s

Suggest that collection `s` reserve capacity for at least `n` elements. That is, if
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
Suggest that collection `s` reserve capacity for `n` elements. That is, if
you expect that you're going to have to push a lot of values onto `s`, you can avoid
the cost of incremental reallocation by doing it once up front; this can improve
performance.
Expand All @@ -1443,6 +1443,10 @@ If `first` is true, then the reserved space is from the start of the collection,
collections. Supplying this keyword may result in an error if the collection is nor ordered
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
or if `pushfirst!` is not supported for this collection.

If `shrink` is `true` (the default), the reserved space may be reduced if it is
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
larger than `n`. Passing `shrink=false` will ensure that the existing space is preserved
even if it is larger than requested.

See also [`resize!`](@ref).

# Notes on the performance model
Expand All @@ -1458,8 +1462,6 @@ For types that support `sizehint!`,

3. `empty!` is nearly costless (and O(1)) for types that support this kind of preallocation.

4. `shrink` controls if the collection can be shrunk.

!!! compat "Julia 1.11"
The `shrink` and `first` arguments were added in Julia 1.11.
"""
Expand Down