|
| 1 | +export @memoize |
| 2 | + |
| 3 | +""" |
| 4 | + @memoize [key::T] [maxlen=...] begin |
| 5 | + # expensive computation |
| 6 | + end::T |
| 7 | +
|
| 8 | +Low-level, no-frills memoization macro that stores values in a thread-local, typed cache. |
| 9 | +The types of the caches are derived from the syntactical type assertions. |
| 10 | +
|
| 11 | +The cache consists of two levels, the outer one indexed with the thread index. If no `key` |
| 12 | +is specified, the second level of the cache is dropped. |
| 13 | +
|
| 14 | +If the the `maxlen` option is specified, the `key` is assumed to be an integer, and the |
| 15 | +secondary cache will be a vector with length `maxlen`. Otherwise, a dictionary is used. |
| 16 | +""" |
| 17 | +macro memoize(ex...) |
| 18 | + code = ex[end] |
| 19 | + args = ex[1:end-1] |
| 20 | + |
| 21 | + # decode the code body |
| 22 | + @assert Meta.isexpr(code, :(::)) |
| 23 | + rettyp = code.args[2] |
| 24 | + code = code.args[1] |
| 25 | + |
| 26 | + # decode the arguments |
| 27 | + key = nothing |
| 28 | + if length(args) >= 1 |
| 29 | + arg = args[1] |
| 30 | + @assert Meta.isexpr(arg, :(::)) |
| 31 | + key = (val=arg.args[1], typ=arg.args[2]) |
| 32 | + end |
| 33 | + options = Dict() |
| 34 | + for arg in args[2:end] |
| 35 | + @assert Meta.isexpr(arg, :(=)) |
| 36 | + options[arg.args[1]] = arg.args[2] |
| 37 | + end |
| 38 | + |
| 39 | + # the global cache is an array with one entry per thread. if we don't have to key on |
| 40 | + # anything, that entry will be the memoized new_value, or else a dictionary of values. |
| 41 | + @gensym global_cache |
| 42 | + |
| 43 | + # in the presence of thread adoption, we need to use the maximum thread ID |
| 44 | + nthreads = :( Threads.maxthreadid() ) |
| 45 | + |
| 46 | + # generate code to access memoized values |
| 47 | + # (assuming the global_cache can be indexed with the thread ID) |
| 48 | + if key === nothing |
| 49 | + # if we don't have to key on anything, use the global cache directly |
| 50 | + global_cache_eltyp = :(Union{Nothing,$rettyp}) |
| 51 | + ex = quote |
| 52 | + cache = get!($(esc(global_cache))) do |
| 53 | + $global_cache_eltyp[nothing for _ in 1:$nthreads] |
| 54 | + end |
| 55 | + cached_value = @inbounds cache[Threads.threadid()] |
| 56 | + if cached_value !== nothing |
| 57 | + cached_value |
| 58 | + else |
| 59 | + new_value = $(esc(code))::$rettyp |
| 60 | + @inbounds cache[Threads.threadid()] = new_value |
| 61 | + new_value |
| 62 | + end |
| 63 | + end |
| 64 | + elseif haskey(options, :maxlen) |
| 65 | + # if we know the length of the cache, use a fixed-size array |
| 66 | + global_cache_eltyp = :(Vector{Union{Nothing,$rettyp}}) |
| 67 | + global_init = :(Union{Nothing,$rettyp}[nothing for _ in 1:$(esc(options[:maxlen]))]) |
| 68 | + ex = quote |
| 69 | + cache = get!($(esc(global_cache))) do |
| 70 | + $global_cache_eltyp[$global_init for _ in 1:$nthreads] |
| 71 | + end |
| 72 | + local_cache = @inbounds begin |
| 73 | + tid = Threads.threadid() |
| 74 | + assume(isassigned(cache, tid)) |
| 75 | + cache[tid] |
| 76 | + end |
| 77 | + cached_value = @inbounds local_cache[$(esc(key.val))] |
| 78 | + if cached_value !== nothing |
| 79 | + cached_value |
| 80 | + else |
| 81 | + new_value = $(esc(code))::$rettyp |
| 82 | + @inbounds local_cache[$(esc(key.val))] = new_value |
| 83 | + new_value |
| 84 | + end |
| 85 | + end |
| 86 | + else |
| 87 | + # otherwise, fall back to a dictionary |
| 88 | + global_cache_eltyp = :(Dict{$(key.typ),$rettyp}) |
| 89 | + global_init = :(Dict{$(key.typ),$rettyp}()) |
| 90 | + ex = quote |
| 91 | + cache = get!($(esc(global_cache))) do |
| 92 | + $global_cache_eltyp[$global_init for _ in 1:$nthreads] |
| 93 | + end |
| 94 | + local_cache = @inbounds begin |
| 95 | + tid = Threads.threadid() |
| 96 | + assume(isassigned(cache, tid)) |
| 97 | + cache[tid] |
| 98 | + end |
| 99 | + cached_value = get(local_cache, $(esc(key.val)), nothing) |
| 100 | + if cached_value !== nothing |
| 101 | + cached_value |
| 102 | + else |
| 103 | + new_value = $(esc(code))::$rettyp |
| 104 | + local_cache[$(esc(key.val))] = new_value |
| 105 | + new_value |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + # define the per-thread cache |
| 111 | + @eval __module__ begin |
| 112 | + const $global_cache = LazyInitialized{Vector{$(global_cache_eltyp)}}() do cache |
| 113 | + length(cache) == $nthreads |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + quote |
| 118 | + $ex |
| 119 | + end |
| 120 | +end |
0 commit comments