# Is Dict shrinking feature planned?

**URL:** https://discourse.julialang.org/t/is-dict-shrinking-feature-planned/68681
**Category:** Internals & Design
**Tags:** dictionary, memory
**Created:** [September 24, 2021, 9:30am UTC](https://discourse.julialang.org/t/is-dict-shrinking-feature-planned/68681 "2021-09-24T09:30:47Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [September 24, 2021, 9:30am UTC](https://discourse.julialang.org/t/is-dict-shrinking-feature-planned/68681/1 "2021-09-24T09:30:47Z")

</div>

I’ve written a program in Julia involving heavy insertion and deletion of elements from many separate `Dict` containers. The memory usage gets bad, and I tracked down the cause (I believe) to the fact that Julia Dicts cannot be shrunk by either deleting keys or calling `sizehint!()`, so all the Dicts in my program are consuming a maximum amount of RAM needed over the whole duration of the run. Does Julia plan to implement the shrinking feature for Dict in the near future? If not I’ll try to restructure my program.

P.S. Are there alternative data structures (from third-party packages) which implements the `AbstractDict` interface while allowing shrinking?

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [September 24, 2021, 9:44am UTC](https://discourse.julialang.org/t/is-dict-shrinking-feature-planned/68681/2 "2021-09-24T09:44:39Z")

</div>

> [@greatpet](#):
>
> P.S. Are there alternative data structures (from third-party packages) which implements the `AbstractDict` interface while allowing shrinking?

> **[GitHub - andyferris/Dictionaries.jl: An alternative interface for...](https://github.com/andyferris/Dictionaries.jl)**
>
> An alternative interface for dictionaries in Julia, for improved productivity and performance - GitHub - andyferris/Dictionaries.jl: An alternative interface for dictionaries in Julia, for improved...

---

<div class="post-metadata">

### Author: ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)
#### Post date: [September 24, 2021, 10:01am UTC](https://discourse.julialang.org/t/is-dict-shrinking-feature-planned/68681/3 "2021-09-24T10:01:49Z")

</div>

The docs for Dictionaries.jl don’t mention shrinking. I tried inserting ~10^8 key-value pairs and then deleting all but one key. The memory usage didn’t decrease.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 24, 2021, 10:28am UTC](https://discourse.julialang.org/t/is-dict-shrinking-feature-planned/68681/4 "2021-09-24T10:28:10Z")

</div>

Looks like shrinking is still a todo:

[https://github.com/JuliaLang/julia/blob/2c9e051c460dd9700e6814c8e49cc1f119ed8b41/base/dict.jl#L238-L242](https://github.com/JuliaLang/julia/blob/2c9e051c460dd9700e6814c8e49cc1f119ed8b41/base/dict.jl#L238-L242)

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [September 24, 2021, 10:29am UTC](https://discourse.julialang.org/t/is-dict-shrinking-feature-planned/68681/5 "2021-09-24T10:29:13Z")

</div>

Right, but contrary to the standard `Dict`, removing entries from a `Dictionary` actually removes entries from its datastructure. You can manually run `sizehint!` on its internal fields to shrink memory. Maybe a method `sizehint!(::Dictionary, ::Int)` would help to make things less manual?

---

<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: [September 24, 2021, 10:50am UTC](https://discourse.julialang.org/t/is-dict-shrinking-feature-planned/68681/6 "2021-09-24T10:50:12Z")

</div>

> [@kristoffer.carlsson](#):
>
> Looks like shrinking is still a todo:

A cursory glance through `rehash!` makes me think it should already work, as it’s literally allocating new buffers and moving elements into them - is there a good way to confirm this? Maybe I’m missing something…

---

<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: [September 24, 2021, 10:58am UTC](https://discourse.julialang.org/t/is-dict-shrinking-feature-planned/68681/7 "2021-09-24T10:58:48Z")

</div>

Ah, I just checked the git log - seems that code is still from 2012 and hasn’t really seen all that many changes since then.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [September 24, 2021, 11:25am UTC](https://discourse.julialang.org/t/is-dict-shrinking-feature-planned/68681/8 "2021-09-24T11:25:42Z")

</div>

> [@Sukera](#):
>
> is there a good way to confirm this

If you compile Julia from source, you can do `Revise.track(Base)`, then edit the file and see the changes.

---

<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: [September 24, 2021, 11:28am UTC](https://discourse.julialang.org/t/is-dict-shrinking-feature-planned/68681/9 "2021-09-24T11:28:02Z")

</div>

Thanks, I already looked at the source 🙂 I was talking about confirming whether or not the existing `rehash!` would already work for the purpose of shrinking the `Dict`. The relevant parts of the code (both of `rehash!` as well as `sizehint!`) hasn’t changed in the last 9 years and the `todo` comment is still there, so I’d rather err on the side of caution and say jeff had a failure mode in mind when writing that code. Exhaustively checking all possible usages of dict shrinking isn’t feasible though…

---

<div class="post-metadata">

### Author: ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)
#### Post date: [September 24, 2021, 3:25pm UTC](https://discourse.julialang.org/t/is-dict-shrinking-feature-planned/68681/10 "2021-09-24T15:25:21Z")

</div>

If I recall correctly, we also don’t shrink the allocated memory of an array when it shrinks, so even if a Dict were to shrink it’s backing arrays, it might not free up any memory until that’s also done. As a workaround, the simplest fix for this at the moment might be just copying the Dict when it’s at its final size.

---

<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: [September 24, 2021, 4:18pm UTC](https://discourse.julialang.org/t/is-dict-shrinking-feature-planned/68681/11 "2021-09-24T16:18:15Z")

</div>

> [@StefanKarpinski](#):
>
> If I recall correctly, we also don’t shrink the allocated memory of an array when it shrinks

`sizehint!` will shrink the allocated memory (if the decrease is sufficiently large); see [sizehint! now supports shrinking arrays #2879 by adrianisuru · Pull Request #26201 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/26201) (which closed an issue you filed in 2013 😉). However, this isn’t done automatically by `pop!`, `empty!`, `delete!`, etcetera, all of which keep the allocated memory fixed.

So it would nowadays make sense for `sizehint!` of a `Dict` to also support shrinking the storage.
