# Do we have a "sizeforce!"?

**URL:** <https://discourse.julialang.org/t/do-we-have-a-sizeforce/94496>\
**Category:** Performance\
**Created:** [February 12, 2023, 8:22am UTC](https://discourse.julialang.org/t/do-we-have-a-sizeforce/94496 "2023-02-12T08:22:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 12, 2023, 8:22am UTC](https://discourse.julialang.org/t/do-we-have-a-sizeforce/94496/1 "2023-02-12T08:22:59Z")

</div>

Hello!

Reading in the help of `sizehint!`:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/0/e06633f27be37309d5c45f0defec93838e77cdd0.png)

I notice how it is only a “suggestive” procedure and that it **may** do it. Is there a way to force it?

I know I can preallocate arrays my self. For less code complexity, I had hoped I could get away without doing that.

Kind regards

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [February 12, 2023, 8:40am UTC](https://discourse.julialang.org/t/do-we-have-a-sizeforce/94496/2 "2023-02-12T08:40:26Z")

</div>

> [@Ahmed\_Salih](#):
>
> I notice how it is only a “suggestive” procedure and that it **may** do it. Is there a way to force it?
> 
> I know I can preallocate arrays my self. For less code complexity, I had hoped I could get away without doing that.

For `Array`, `sizehint!` does exactly that. We don’t have a way to force this in a generic way because we don’t really have an interface for it.

> [@Ahmed\_Salih](#):
>
> I know I can preallocate arrays my self. For less code complexity, I had hoped I could get away without doing that.

Not sure what you mean here - `sizehint!` does not change the actual useable size of an array, only how much memory it should reserve in the background:

```julia
julia> a = [1,2,3]
3-element Vector{Int64}:
 1
 2
 3

julia> length(a)
3

julia> sizehint!(a, 100)
3-element Vector{Int64}:
 1
 2
 3

julia> length(a)
3

```

This ends up making things like `push!` or `append!` faster as long as the length of the array doesn’t reach that requested capacity, because they don’t have to reallocate the array & copy the elements around under the hood.

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 12, 2023, 8:59am UTC](https://discourse.julialang.org/t/do-we-have-a-sizeforce/94496/3 "2023-02-12T08:59:26Z")

</div>

Thank you, that makes a lot of sense 🙂
