# \#memoize

**URL:** https://discourse.julialang.org/tag/memoize/1052.md

[Latest](https://discourse.julialang.org/latest.md) · [Categories](https://discourse.julialang.org/categories.md) · [Tags](https://discourse.julialang.org/tags.md)

---

## [Dictionary for thread-safe, limited memoization](https://discourse.julialang.org/t/dictionary-for-thread-safe-limited-memoization/137072)

<div class="topic-metadata">

**Author:** [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Replies:** 25\
**Last updated:** [May 27, 2026, 10:14pm UTC](https://discourse.julialang.org/t/dictionary-for-thread-safe-limited-memoization/137072 "2026-05-27T22:14:14Z")

</div>

I am looking for a Dict-like type which allows me to keep only the last N values inserted, for some value of N::Int, is thread-safe. Background: memoization, where the resulting objects are large and I am unlike…

---

## [Can I get away with memoizing a :foldable method?](https://discourse.julialang.org/t/can-i-get-away-with-memoizing-a-foldable-method/136951)

<div class="topic-metadata">

**Author:** [@Benny](https://discourse.julialang.org/u/Benny)\
**Replies:** 0\
**Last updated:** [May 1, 2026, 3:48pm UTC](https://discourse.julialang.org/t/can-i-get-away-with-memoizing-a-foldable-method/136951 "2026-05-01T15:48:59Z")

</div>

Starting to dabble in moving long calls from runtime into compile-time, and I’m finding that repeated calls in compile-time are also annoying. Naively, I tried compile-time memoization, also avoiding cache access overhea…

---

## [Memoizing a function using only a subset of arguments as an identifier](https://discourse.julialang.org/t/memoizing-a-function-using-only-a-subset-of-arguments-as-an-identifier/107426)

<div class="topic-metadata">

**Author:** [@Aleksandr\_Mikheev](https://discourse.julialang.org/u/Aleksandr_Mikheev)\
**Replies:** 3\
**Last updated:** [December 11, 2023, 12:16pm UTC](https://discourse.julialang.org/t/memoizing-a-function-using-only-a-subset-of-arguments-as-an-identifier/107426 "2023-12-11T12:16:20Z")

</div>

Suppose I have a function of two arguments. For instance, function foo(a::Float64,b::Float64) return a^2 - b end Is it possible to memoize it based on the first argument only? I understand how I can partially achiev…

---

## [Memoize functions defined in \`SymPy.jl\`](https://discourse.julialang.org/t/memoize-functions-defined-in-sympy-jl/102070)

<div class="topic-metadata">

**Author:** [@jlbosse](https://discourse.julialang.org/u/jlbosse)\
**Replies:** 3\
**Last updated:** [July 26, 2023, 2:21pm UTC](https://discourse.julialang.org/t/memoize-functions-defined-in-sympy-jl/102070 "2023-07-26T14:21:13Z")

</div>

Is there a way to memoize a function that is defined in another module? Ideally, I would like to to something along the lines of import OtherModule: func using Memoization # I want this to replace \`func\` with a memoiz…

---

## [How to make a function store data to avoid repeating computation](https://discourse.julialang.org/t/how-to-make-a-function-store-data-to-avoid-repeating-computation/88396)

<div class="topic-metadata">

**Author:** [@natema](https://discourse.julialang.org/u/natema)\
**Replies:** 45\
**Last updated:** [October 17, 2022, 12:22pm UTC](https://discourse.julialang.org/t/how-to-make-a-function-store-data-to-avoid-repeating-computation/88396 "2022-10-17T12:22:49Z")

</div>

I have a function in a module that performs some heavy computation to produce a fixed output. It is hard to predict if the function will be called, and we want to avoid computing it if unnecessary. So, we would like th…

---

## [Another implementation of \`@memoize\` which is type inference friendly & generic functions](https://discourse.julialang.org/t/another-implementation-of-memoize-which-is-type-inference-friendly-generic-functions/3133)

<div class="topic-metadata">

**Author:** [@FANG\_Colin](https://discourse.julialang.org/u/FANG_Colin)\
**Replies:** 0\
**Last updated:** [April 9, 2017, 11:37pm UTC](https://discourse.julialang.org/t/another-implementation-of-memoize-which-is-type-inference-friendly-generic-functions/3133 "2017-04-09T23:37:38Z")

</div>

I made another Memoize.jl package for daily use of Julia. Suggestions & criticisms are welcom. Overview This package provides basic support for type inference friendly generic function memoization. It allows to customi…

---

## [Precompute some values of a function](https://discourse.julialang.org/t/precompute-some-values-of-a-function/82079)

<div class="topic-metadata">

**Author:** [@raphaelchinchilla](https://discourse.julialang.org/u/raphaelchinchilla)\
**Replies:** 7\
**Last updated:** [June 1, 2022, 8:46pm UTC](https://discourse.julialang.org/t/precompute-some-values-of-a-function/82079 "2022-06-01T20:46:20Z")

</div>

Consider the following MWE: d=rand(10^8); f(x,d) = x + sum(d) g(x) = f(x,d) If I run @benchmark f(1,d) and @benchmark g(1) (in the REPL), both take the same amount of time to run (which is what I kind of excepted). Is …

---

## [Memoization and function calls](https://discourse.julialang.org/t/memoization-and-function-calls/63218)

<div class="topic-metadata">

**Author:** [@Alex\_Tantos](https://discourse.julialang.org/u/Alex_Tantos)\
**Replies:** 4\
**Last updated:** [June 20, 2021, 3:44pm UTC](https://discourse.julialang.org/t/memoization-and-function-calls/63218 "2021-06-20T15:44:12Z")

</div>

Hi there. This is a newbie question. I need to call the following function, lexicalgravitypair(), within a for loop, such as the one below the function definition of lexicalgravitypair(), that depends on the output of …

---

## [\[ANN\] Memoization.jl](https://discourse.julialang.org/t/ann-memoization-jl/31556)

<div class="topic-metadata">

**Author:** [@marius311](https://discourse.julialang.org/u/marius311)\
**Replies:** 12\
**Last updated:** [April 16, 2021, 9:41pm UTC](https://discourse.julialang.org/t/ann-memoization-jl/31556 "2021-04-16T21:41:20Z")

</div>

I created Memoization.jl to address some of the limitations of Memoize.jl. It’s API compatible with Memoize.jl (just @memoize your function definitions): julia\> using Memoization julia\> @memoize f(x) = (println("Compu…

---

## [How to write @memoize that works in both local and global scope](https://discourse.julialang.org/t/how-to-write-memoize-that-works-in-both-local-and-global-scope/49727)

<div class="topic-metadata">

**Author:** [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Replies:** 6\
**Last updated:** [November 8, 2020, 7:51am UTC](https://discourse.julialang.org/t/how-to-write-memoize-that-works-in-both-local-and-global-scope/49727 "2020-11-08T07:51:23Z")

</div>

Let’s write a @memoize f(x) = ... macro in Julia. Glossing over gensyms etc. it can just macroexpand into memo = Dict() f(x) = get(memo, x) do ... end At the global scope memo will be a slow global variable, so let’s i…

---

## [Minimizing allocations from memoized functions](https://discourse.julialang.org/t/minimizing-allocations-from-memoized-functions/49221)

<div class="topic-metadata">

**Author:** [@ash](https://discourse.julialang.org/u/ash)\
**Replies:** 10\
**Last updated:** [October 31, 2020, 7:05am UTC](https://discourse.julialang.org/t/minimizing-allocations-from-memoized-functions/49221 "2020-10-31T07:05:17Z")

</div>

I have the following MWE using a memoized function: using Memoization using FFTW struct Operators{F} K::F # More operators end function run(N, α) M = 512 ω = rand(M) # Cache the kinetic factor …

---

## [Memoize Pkg? Bad use of Metaprogramming?](https://discourse.julialang.org/t/memoize-pkg-bad-use-of-metaprogramming/46874)

<div class="topic-metadata">

**Author:** [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Replies:** 14\
**Last updated:** [September 19, 2020, 5:21pm UTC](https://discourse.julialang.org/t/memoize-pkg-bad-use-of-metaprogramming/46874 "2020-09-19T17:21:19Z")

</div>

Been doing some long-running computations. I mean … long. So I am doing the obvious thing, storing results to disk along the way. For reproducibility reasons I’d like to be able to memoize lots of intermediate results al…

---

## [@memoize macro](https://discourse.julialang.org/t/memoize-macro/39968)

<div class="topic-metadata">

**Author:** [@A.U.M](https://discourse.julialang.org/u/A.U.M)\
**Replies:** 3\
**Last updated:** [May 26, 2020, 8:01am UTC](https://discourse.julialang.org/t/memoize-macro/39968 "2020-05-26T08:01:35Z")

</div>

Hello. I am working on implement macro @memoize. it works well but i want to generalize it and make it more robust (still for functions with a single argument) by checking whether the argument and return types are specif…

---

## [Memoize a vararg function](https://discourse.julialang.org/t/memoize-a-vararg-function/11151)

<div class="topic-metadata">

**Author:** [@gmouts](https://discourse.julialang.org/u/gmouts)\
**Replies:** 3\
**Last updated:** [May 26, 2018, 5:26am UTC](https://discourse.julialang.org/t/memoize-a-vararg-function/11151 "2018-05-26T05:26:58Z")

</div>

I tried to use Memoize.jl fro a vararg function. The following code @memoize function test(lst::Vararg{Int}) return length(lst) end test(1,2) gives TypeError: test: in typeassert, expected Vararg{Int64,N} where N…

---

## [Understanding memoization.jl](https://discourse.julialang.org/t/understanding-memoization-jl/5394)

<div class="topic-metadata">

**Author:** [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Replies:** 5\
**Last updated:** [August 15, 2017, 8:17pm UTC](https://discourse.julialang.org/t/understanding-memoization-jl/5394 "2017-08-15T20:17:52Z")

</div>

I am trying to understand memoization.jl by simonster to obtain a good understanding of macros. First I tried to macroexpand the output: julia\> using Memoize julia\> macroexpand(:(@memoize function x(a) a end)) quote #…
