Skip to content

Remove eval from at-memoize macro #48

Open
@kmsquire

Description

@kmsquire

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:

  1. all memoized methods of a given function share the memoization cache
  2. 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:

    Memoize.jl/test/runtests.jl

    Lines 257 to 266 in 1709785

    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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions