# Resize! and empty! immediately, why?

**URL:** <https://discourse.julialang.org/t/resize-and-empty-immediately-why/92093>\
**Category:** General Usage\
**Created:** [December 24, 2022, 8:58pm UTC](https://discourse.julialang.org/t/resize-and-empty-immediately-why/92093 "2022-12-24T20:58:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrey2185](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrey2185/32/9889_2.png) [@andrey2185](https://discourse.julialang.org/u/andrey2185)\
**Post date:** [December 24, 2022, 8:58pm UTC](https://discourse.julialang.org/t/resize-and-empty-immediately-why/92093/1 "2022-12-24T20:58:52Z")

</div>

hello, found someone else’s code like this

```julia
abstract type AbstractFoo{T} end
struct Foo{T} <: AbstractFoo{T}
    a::Vector{T}
    function Foo{T}(size::Int) where T
        foo = new(T[])
        resize!(foo.a, size)
        empty!(foo.a)
        return foo
    end
    Foo{T}() where T = new(T[])
end

```

why is he doing this?

```julia
resize!(foo.a, size)
empty!(foo.a)

```

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [December 24, 2022, 9:08pm UTC](https://discourse.julialang.org/t/resize-and-empty-immediately-why/92093/2 "2022-12-24T21:08:31Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 24, 2022, 9:25pm UTC](https://discourse.julialang.org/t/resize-and-empty-immediately-why/92093/3 "2022-12-24T21:25:31Z")

</div>

Would

```julia
return new(sizehint!(T[],size))

```

be equivalent to all three lines? Which is preferable?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 24, 2022, 10:14pm UTC](https://discourse.julialang.org/t/resize-and-empty-immediately-why/92093/4 "2022-12-24T22:14:55Z")

</div>

Yeah, this seems like an un-idiomatic way to do `sizehint!`

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [December 24, 2022, 11:29pm UTC](https://discourse.julialang.org/t/resize-and-empty-immediately-why/92093/5 "2022-12-24T23:29:55Z")

</div>

Not quite, because you still have the extra allocation of the initial empty array and then reallocation from size hint. The undef method only allocates once.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [December 24, 2022, 11:38pm UTC](https://discourse.julialang.org/t/resize-and-empty-immediately-why/92093/6 "2022-12-24T23:38:06Z")

</div>

> [@quinnj](#):
>
> you still have the extra allocation of the initial empty

That’s what I thought. I like the readability of `sizehint!`. Possible suggestion:  
`sizehint(Vector{T}, size)` to return an empty array with appropriate hint (note no `!` in name).

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 24, 2022, 11:48pm UTC](https://discourse.julialang.org/t/resize-and-empty-immediately-why/92093/7 "2022-12-24T23:48:52Z")

</div>

> [@quinnj](#):
>
> ```julia
> foo = new(Vector{T}(undef, size)
> empty!(foo.a)
> 
> ```

But is this guaranteed? I do not see the documentation saying that the array cannot be garbage collected after `empty!` This is unlike `sizehint!`

---

<div class="post-metadata">

**Author:** ![quinnj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/quinnj/32/11_2.png) [@quinnj](https://discourse.julialang.org/u/quinnj)\
**Post date:** [January 4, 2023, 4:20am UTC](https://discourse.julialang.org/t/resize-and-empty-immediately-why/92093/8 "2023-01-04T04:20:32Z")

</div>

You’re right that I don’t think it’s guaranteed, but currently (\<1.9 release) won’t shrink the array on `empty!`. And to me that makes sense: if I’m calling `sizehint!` on an array w/ a smaller size, I’m signaling “hey, I actually want this array to be smaller now”, so reclaiming the memory makes sense. For `empty!`, it’s often used in loops where I’m `push!`ing or `append!`ing then `empty!`ing and doing it all over again, so it’s more clear that I’m “zeroing out” the array and will probably reuse the memory again later. That makes sense to me that the memory will stay allocated then. Note that `sizehint!` didn’t used to be able to _shrink_ arrays, only _grow_ them, so for a long time, it was only possible to reclaim memory by having the array go away all together.
