# Size-hinted array declarations

**URL:** <https://discourse.julialang.org/t/size-hinted-array-declarations/100935>\
**Category:** Performance\
**Tags:** memory-allocation\
**Created:** [June 28, 2023, 2:09pm UTC](https://discourse.julialang.org/t/size-hinted-array-declarations/100935 "2023-06-28T14:09:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 28, 2023, 2:09pm UTC](https://discourse.julialang.org/t/size-hinted-array-declarations/100935/1 "2023-06-28T14:09:06Z")

</div>

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

```julia
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?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 28, 2023, 2:23pm UTC](https://discourse.julialang.org/t/size-hinted-array-declarations/100935/2 "2023-06-28T14:23:26Z")

</div>

Maybe

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

```

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

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 28, 2023, 3:07pm UTC](https://discourse.julialang.org/t/size-hinted-array-declarations/100935/3 "2023-06-28T15:07:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 28, 2023, 4:27pm UTC](https://discourse.julialang.org/t/size-hinted-array-declarations/100935/4 "2023-06-28T16:27:48Z")

</div>

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

```julia
Vector{SomeType}(sizehint = n) # like sizehint!(Vector{SomeType}(), n)
Vector{SomeType}(undef, m, sizehint = n) # like sizehint!(Vector{SomeType}(undef, m), n)

```

---

<div class="post-metadata">

**Author:** ![AMJ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amj/32/214096_2.png) [@AMJ](https://discourse.julialang.org/u/AMJ)\
**Post date:** [June 28, 2023, 6:25pm UTC](https://discourse.julialang.org/t/size-hinted-array-declarations/100935/5 "2023-06-28T18:25:39Z")

</div>

I think the [redesigning](https://gist.github.com/JeffBezanson/a25dde3bebb5a734af87bb5ddcf31fb0#gistcomment-4591710) proposal would kinda cover this? But a long way to get there.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [June 28, 2023, 10:02pm UTC](https://discourse.julialang.org/t/size-hinted-array-declarations/100935/6 "2023-06-28T22:02:03Z")

</div>

This sounds like it might be a good fit for [ArrayAllocators.jl](https://github.com/mkitti/ArrayAllocators.jl).

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [September 28, 2023, 10:20am UTC](https://discourse.julialang.org/t/size-hinted-array-declarations/100935/7 "2023-09-28T10:20:49Z")

</div>

Slightly better:

```julia
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.
