Right now, the @memoize macro creates the cache dictionary using eval:
|
fcachename = Symbol("##", f, "_memoized_cache") |
|
fcache = isdefined(Main, fcachename) ? |
|
getfield(Main, fcachename) : |
|
Core.eval(Main, :(const $fcachename = ($dicttype)())) |
This is generally considered a bad idea in macros, and until recently, also caused an error here because the code was
evaling into the Memoize package (see
#32).
Right now, using eval gives the following behavior:
- all memoized methods of a given function share the memoization cache
- if a method is overwritten, the cache is cleared, and any other global resources created/held by the previous version of that method are released:
|
finalized = false |
|
@memoize function method_rewrite() |
|
x = [] |
|
finalizer(x->(global finalized; finalized = true),x) |
|
x |
|
end |
|
method_rewrite() |
|
@memoize function method_rewrite() end |
|
GC.gc() |
|
@test finalized |
I made one attempt to simply use a different cache dictionary for each method of a given function (JuliaCollections:1709785...JuliaCollections:a9170e5). This mostly works, but as-is causes the test above to fail, because the old cache is not released when the method is overwritten.
There may be an easy way around this, but it's not obvious to me right now.
Thoughts and/or pull requests welcome!
Cc: @cstjean
Right now, the
@memoizemacro creates the cache dictionary usingeval:Memoize.jl/src/Memoize.jl
Lines 49 to 52 in 1709785
This is generally considered a bad idea in macros, and until recently, also caused an error here because the code was
evaling into the Memoize package (see #32).Right now, using
evalgives the following behavior:Memoize.jl/test/runtests.jl
Lines 257 to 266 in 1709785
I made one attempt to simply use a different cache dictionary for each method of a given function (JuliaCollections:1709785...JuliaCollections:a9170e5). This mostly works, but as-is causes the test above to fail, because the old cache is not released when the method is overwritten.
There may be an easy way around this, but it's not obvious to me right now.
Thoughts and/or pull requests welcome!
Cc: @cstjean