Inspecting memoization in Julia

Viewed 131

There is the package Memoize.jl, with which one can memoize in Julia. Its @memoize macro creates a dictionary. Is there a way to inspect this dictionary?

As an example, after I execute

@memoize f(n) = n ≤ 1 ? n : f(n-1) + f(n-1)

@show f(10)

I would like to inspect the intermediate values of f which have been generated, so f(0), f(1), ..., f(10).

Someone had written Memo.jl as a replacement or enhancement of Memoize.jl allowing for customization and inspection. However, this package seems to be not maintained.

1 Answers

One of the nice things about Julia is that packages like this are just more Julia code instead of doing something in C that's impossible to poke at from the main language. And the code for Memoize is pretty simple. Take a look at this line:

 fcachename = Symbol("##", f, "_memoized_cache")

That tells you the name of the cache dictionary, given f as the name of the function that's being memoized. So let's try accessing the variable with that name after running your example code:

julia> var"##f_memoized_cache"
IdDict{Any,Any} with 10 entries:
  (7,)  => 64
  (6,)  => 32
  (4,)  => 8
  (5,)  => 16
  (9,)  => 256
  (10,) => 512
  (2,)  => 2
  (8,)  => 128
  (1,)  => 1
  (3,)  => 4

Voila! There's the actual cache. It's just an IdDict with a weird name in same module as the method definition. The var"..." syntax is a recently-added custom string literal syntax for an identifier with a "strange" name—it's a shorthand for doing eval(Symbol("##f_memoized_cache")).

Of course, since this isn't an official documented part of the Memoize API, you can't rely on that not changing, but it works currently. I'm not sure if you want a more official API, but if you do you could open an issue asking for that as a new feature.

Related