Skip to content

Commit

Permalink
Get rid of eval
Browse files Browse the repository at this point in the history
Fix #48, at the cost of putting the variable in a poorly-performing global.

Not sure if this is acceptable. It's frustrating that Julia seemingly lacks the tools to deal with
this elegantly.

- If `const` worked in a local function, we'd just put `const` and be done with it.
- If `typeconst` existed for global variables, that would work too.

Memoization.jl uses generated functions, which causes other problems. And it feels like the
wrong solution too.
  • Loading branch information
cstjean committed Oct 28, 2020
1 parent 919c938 commit 09b8348
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/Memoize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,22 @@ macro memoize(args...)

fcachename = Symbol("##", f, "_memoized_cache")
mod = __module__
fcache = isdefined(mod, fcachename) ?
getfield(mod, fcachename) :
Core.eval(mod, :(const $fcachename = $cache_dict))

if length(kws) == 0
lookup = :($fcache[($(tup...),)]::Core.Compiler.return_type($u, typeof(($(identargs...),))))
lookup = :($fcachename[($(tup...),)]::Core.Compiler.return_type($u, typeof(($(identargs...),))))
else
lookup = :($fcache[($(tup...),)])
lookup = :($fcachename[($(tup...),)])
end

def_dict[:body] = quote
haskey($fcache, ($(tup...),)) ? $lookup :
($fcache[($(tup...),)] = $u($(identargs...),; $(identkws...)))
haskey($fcachename, ($(tup...),)) ? $lookup :
($fcachename[($(tup...),)] = $u($(identargs...),; $(identkws...)))
end
esc(quote
$fcachename = $cache_dict # this should be `const` for performance, but then this
# fails the local-function cache test.
$(combinedef(def_dict_unmemoized))
empty!($fcache)
empty!($fcachename)
Base.@__doc__ $(combinedef(def_dict))
end)

Expand Down

0 comments on commit 09b8348

Please sign in to comment.