Skip to content

Commit f8b9a52

Browse files
authored
Document performance model of push! & friends. (#37853)
1 parent de3a70a commit f8b9a52

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

base/array.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,8 @@ julia> push!([1, 2, 3], 4, 5, 6)
904904
If `collection` is ordered, use [`append!`](@ref) to add all the elements of another
905905
collection to it. The result of the preceding example is equivalent to `append!([1, 2, 3], [4,
906906
5, 6])`. For `AbstractSet` objects, [`union!`](@ref) can be used instead.
907+
908+
See [`sizehint!`](@ref) for notes about the performance model.
907909
"""
908910
function push! end
909911

@@ -951,6 +953,8 @@ julia> append!([1, 2, 3], [4, 5], [6])
951953
Use [`push!`](@ref) to add individual items to `collection` which are not already
952954
themselves in another collection. The result of the preceding example is equivalent to
953955
`push!([1, 2, 3], 4, 5, 6)`.
956+
957+
See [`sizehint!`](@ref) for notes about the performance model.
954958
"""
955959
function append!(a::Vector, items::AbstractVector)
956960
itemindices = eachindex(items)
@@ -1097,6 +1101,19 @@ end
10971101
sizehint!(s, n)
10981102
10991103
Suggest that collection `s` reserve capacity for at least `n` elements. This can improve performance.
1104+
1105+
# Notes on the performance model
1106+
1107+
For types that support `sizehint!`,
1108+
1109+
1. `push!` and `append!` methods generally may (but are not required to) preallocate extra
1110+
storage. For types implemented in `Base`, they typically do, using a heuristic optimized for
1111+
a general use case.
1112+
1113+
2. `sizehint!` may control this preallocation. Again, it typically does this for types in
1114+
`Base`.
1115+
1116+
3. `empty!` is nearly costless (and O(1)) for types that support this kind of preallocation.
11001117
"""
11011118
function sizehint! end
11021119

0 commit comments

Comments
 (0)