How to write @memoize that works in both local and global scope

Recently I have been avoiding the “magic” of macros where I think the explicit code is simple enough, and can benefit from customization: e.g. for memoizing:

  • local vs global scope
  • new function versus adding method to existing function (perhaps in another module)
  • typed Dict for caching
  • custom caching where cache value is function of argument

Is the fib example faster with typed Dict cache?

let
    memo = Dict{Int,BigInt}()
    global function fib2(x)
        get!(memo, x) do
            x <= 2 && return BigInt(1)
            fib2(x-1) + fib2(x-2)
        end
    end
end
1 Like