Size-hinted array declarations

The common way of declaring size-hinted arrays for more efficient append!/push!/etc. seems to be

x = SomeElementType[]
sizehint!(x, expected_size)

But the sizehint! induces an extra allocation. Is there a way to avoid this by declaring a size-hinted array all at once?

1 Like

Maybe

x = resize!(Vector{SomeElementType}(undef, expected_size), 0)

(Though it’s probably rare that one extra allocation has a non-negligible performance impact.)

4 Likes

Cool, that works! Thanks!

Though it’s probably rare that one extra allocation has a non-negligible performance impact

In this case, it’s being used for custom serialization of small data types that has to run in a tight loop that is otherwise pretty inexpensive. The allocation cost isn’t huge, but it is non-negligible.

It might be nice to have a keyword argument for this in the Vector constructor:

Vector{SomeType}(sizehint = n) # like sizehint!(Vector{SomeType}(), n)
Vector{SomeType}(undef, m, sizehint = n) # like sizehint!(Vector{SomeType}(undef, m), n)
7 Likes

I think the redesigning proposal would kinda cover this? But a long way to get there.

1 Like

This sounds like it might be a good fit for ArrayAllocators.jl.

1 Like

Slightly better:

empty!(Vector{SomeElementType}(undef, expected_size))

empty! seems preferable to resize! here, because it’s shorter and easier to remember, and also because it makes the job slightly easier for the compiler/optimizers.

2 Likes