Resize! and empty! immediately, why?

This is somewhat the same idea in Go as slice’s having a different “length” than “capacity”. When you call empty! on a Julia Vector, it sets the length to zero, but the underlying memory stays the same. This means that later when you do push! or append, they avoid reallocating the array by trying to “right size” it once up front. They could skip one step here, however, by doing:

foo = new(Vector{T}(undef, size)
empty!(foo.a)
return foo

which avoids allocating an empty array, resizing it (which mostly likely allocates a new array to the full size), then emptying by just allocating the array to the full size to begin with.

4 Likes