# Dictionary for thread-safe, limited memoization

**URL:** <https://discourse.julialang.org/t/dictionary-for-thread-safe-limited-memoization/137072>\
**Category:** General Usage\
**Tags:** question, multithreading, dictionaries, memoize\
**Created:** [May 11, 2026, 12:23pm UTC](https://discourse.julialang.org/t/dictionary-for-thread-safe-limited-memoization/137072 "2026-05-11T12:23:52Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [May 27, 2026, 10:17am UTC](https://discourse.julialang.org/t/dictionary-for-thread-safe-limited-memoization/137072/21 "2026-05-27T10:17:25Z")

</div>

> [@Tamas\_Papp](#):
>
> My use case is probably something like 20–100 values in the cache, each about 100KiB, so a few KiB of overhead is irrelevant for my purposes.

Oh sure, it’s just from:

- Fast
- Small
- Concurrent
- Good cache policy

I want all of the above 😁

The idea with thread-local storage is batching access time updates, rather than cache presence. So there shouldn’t be any spurious cache misses.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 27, 2026, 12:26pm UTC](https://discourse.julialang.org/t/dictionary-for-thread-safe-limited-memoization/137072/22 "2026-05-27T12:26:26Z")

</div>

> [@tecosaur](#):
>
> The idea with thread-local storage is batching access time updates, rather than cache presence. So there shouldn’t be any spurious cache misses.

Thanks for the answer. I understand that your planned implementation would provide this, but I am also interested in the answer in general.

That is, if eg I used a dictionary created from `OhMyThreads.TaskLocalValue`, how can I avoid switches like the one described above? Or is that not a concern?

---

<div class="post-metadata">

**Author:** ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)\
**Post date:** [May 27, 2026, 1:45pm UTC](https://discourse.julialang.org/t/dictionary-for-thread-safe-limited-memoization/137072/23 "2026-05-27T13:45:01Z")

</div>

> [@Tamas\_Papp](#):
>
> That is, if eg I used a dictionary created from `OhMyThreads.TaskLocalValue`, how can I avoid switches like the one described above? Or is that not a concern?

Unless I’m misunderstanding you, I think the underwhelming answer is you simply need _some_ form of thread synchronisation to make a shared concurrent cache work.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [May 27, 2026, 2:29pm UTC](https://discourse.julialang.org/t/dictionary-for-thread-safe-limited-memoization/137072/24 "2026-05-27T14:29:47Z")

</div>

> [@Tamas\_Papp](#):
>
> Also, I just realized that if I could ensure that each computation remains on its own thread, I could make my caches _thread local_.

Did you really mean _thread-local_ as opposed to _task-local_? Usually talking about threads in Julia is somewhat problematic since we don’t have access to them directly.  
If you meant to say _task-local_ then your concern about migrations between threads is moot 🙂

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 27, 2026, 2:52pm UTC](https://discourse.julialang.org/t/dictionary-for-thread-safe-limited-memoization/137072/25 "2026-05-27T14:52:48Z")

</div>

> [@abraemer](#):
>
> Did you really mean _thread-local_ as opposed to _task-local_?

It is probably apparent that I don’t know much about these things 😉

To make the example concrete: suppose the sketch of the computation is

```julia
using OhMyThreads

Base.@kwdef struct Foo{C}
    cache::C = TaskLocalStorage{D}(() -> make_foo_dict()) # assume types are figure out
    ...
end

let foo = Foo(...)
    tmap(_ -> do_computation(foo), 1:5) # mcmc
end

```

then each `cache` is task local so the issue is moot?

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [May 27, 2026, 10:14pm UTC](https://discourse.julialang.org/t/dictionary-for-thread-safe-limited-memoization/137072/26 "2026-05-27T22:14:14Z")

</div>

Yes I think so 🙂 Julia’s concurrency is generally build around `Task`s and which thread runs a `Task` is somewhat of an implementation detail. That’s basically the bottomline of the (in)famous

> **[PSA: Thread-local state is no longer recommended](https://julialang.org/blog/2023/07/PSA-dont-use-threadid/)**
>
> PSA: Thread-local state is no longer recommended; Common misconceptions about threadid() and nthreads()

[Previous page](https://discourse.julialang.org/t/dictionary-for-thread-safe-limited-memoization/137072.md?page=1)
