You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It is a common idiom to allocate some mutable container, push! or append! values into it, optionally by providing a sizehint!, then resize or empty! it, and reuse between runs.
This leads to performant code with little effort, especially if the number of elements collected cannot be known before each run, but are somewhat similar.
push! and append! methods may (but are not required to) preallocate extra storage. They do preallocate for types in Base, using a heuristic which is optimal for the general use case.
sizehint!may control this preallocation. It does this for types in Base.
empty! is nearly costless (and O(1)) for types that support this kind of preallocation.
None of this is breaking, just documenting the status quo.
Furthermore, we could think about an API to query the current sizehint! setting. For example, imagine that one is collecting random elements, usually less than 10, but very infrequently around 10000. empty! would keep that large buffer around, while one could imagine that the caller could check for this and resize to something smaller.
Cf #24909, as for Vector this documentation would then describe an ideal performance model once that is fixed.
The text was updated successfully, but these errors were encountered:
(opening an issue as a follow-up to this discussion)
It is a common idiom to allocate some mutable container,
push!
orappend!
values into it, optionally by providing asizehint!
, then resize orempty!
it, and reuse between runs.This leads to performant code with little effort, especially if the number of elements collected cannot be known before each run, but are somewhat similar.
However, I learned most of this from discussions like this, not the documentation.
I propose that the following is documented:
push!
andappend!
methods may (but are not required to) preallocate extra storage. They do preallocate for types inBase
, using a heuristic which is optimal for the general use case.sizehint!
may control this preallocation. It does this for types inBase
.empty!
is nearly costless (and O(1)) for types that support this kind of preallocation.None of this is breaking, just documenting the status quo.
Furthermore, we could think about an API to query the current
sizehint!
setting. For example, imagine that one is collecting random elements, usually less than 10, but very infrequently around 10000.empty!
would keep that large buffer around, while one could imagine that the caller could check for this and resize to something smaller.Cf #24909, as for
Vector
this documentation would then describe an ideal performance model once that is fixed.The text was updated successfully, but these errors were encountered: