Skip to content

Commit

Permalink
Merge d49ae63 into aafc92f
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanBieler committed Aug 12, 2018
2 parents aafc92f + d49ae63 commit 44d7438
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 29 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Expand Up @@ -3,12 +3,12 @@ os:
- linux
- osx
julia:
- 0.5
- 0.6
- 0.7
- 1.0
notifications:
email: false
script:
- if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
- julia --check-bounds=yes -e 'Pkg.clone(pwd()); Pkg.build("Memoize"); Pkg.test("Memoize"; coverage=true)'
- julia --check-bounds=yes -e 'using Pkg; Pkg.clone(pwd()); Pkg.build("Memoize"); Pkg.test("Memoize"; coverage=true)'
after_success:
- julia -e 'cd(Pkg.dir("Memoize")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
- julia -e 'using Pkg; cd(Pkg.dir("Memoize")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
2 changes: 1 addition & 1 deletion REQUIRE
@@ -1 +1 @@
julia 0.5
julia 0.7
18 changes: 10 additions & 8 deletions src/Memoize.jl
Expand Up @@ -3,7 +3,7 @@ export @memoize

macro memoize(args...)
if length(args) == 1
dicttype = :(ObjectIdDict)
dicttype = :(IdDict)
ex = args[1]
elseif length(args) == 2
(dicttype, ex) = args
Expand Down Expand Up @@ -31,11 +31,11 @@ macro memoize(args...)
kws = Any[]
vals = copy(args)
if length(vals) > 0 && isa(vals[1], Expr) && vals[1].head == :parameters
kws = shift!(vals).args
kws = popfirst!(vals).args
end

# Set up arguments for tuple to encode keywords
tup = Array{Any}(length(kws)+length(vals))
tup = Array{Any}(undef,length(kws)+length(vals))
i = 1
for val in vals
tup[i] = if isa(val, Expr)
Expand All @@ -62,7 +62,7 @@ macro memoize(args...)
end

# Set up identity arguments to pass to unmemoized function
identargs = Array{Any}((length(kws) > 0)+length(vals))
identargs = Array{Any}(undef,(length(kws) > 0)+length(vals))
i = (length(kws) > 0) + 1
for val in vals
if isa(val, Expr)
Expand Down Expand Up @@ -92,12 +92,14 @@ macro memoize(args...)
end

fcachename = Symbol("##",f,"_memoized_cache")
mod = current_module()
mod = @__MODULE__
fcache = isdefined(mod, fcachename) ?
getfield(mod, fcachename):
eval(mod, :(const $fcachename = ($dicttype)()))
getfield(mod, fcachename) :
Core.eval(mod, :(const $fcachename = ($dicttype)()))

if length(kws) == 0 && VERSION >= v"0.5.0-dev+5235"
if length(kws) == 0 && VERSION >= v"0.7.0-alpha.0"
lookup = :($fcache[($(tup...),)]::Core.Compiler.return_type($u, typeof(($(identargs...),))))
elseif length(kws) == 0 && VERSION >= v"0.5.0-dev+5235"
lookup = :($fcache[($(tup...),)]::Core.Inference.return_type($u, typeof(($(identargs...),))))
else
lookup = :($fcache[($(tup...),)])
Expand Down
47 changes: 31 additions & 16 deletions test/runtests.jl
@@ -1,4 +1,4 @@
using Memoize, Base.Test
using Memoize, Test

# you can't use test_throws in macros
arun = 0
Expand Down Expand Up @@ -163,18 +163,33 @@ run = 0
global run += 1
a
end
@test kw_ellipsis() == []
@test run == 1
@test kw_ellipsis() == []
@test run == 1
@test kw_ellipsis(a=1) == Any[(:a, 1)]
@test run == 2
@test kw_ellipsis(a=1) == Any[(:a, 1)]
@test run == 2
@test kw_ellipsis(a=1, b=2) == Any[(:a, 1), (:b, 2)]
@test run == 3
@test kw_ellipsis(a=1, b=2) == Any[(:a, 1), (:b, 2)]
@test run == 3
if VERSION >= v"0.7.0-alpha" # PR #24795
@test isempty(kw_ellipsis())
@test run == 1
@test isempty(kw_ellipsis())
@test run == 1
@test kw_ellipsis(a=1) == pairs((a=1,))
@test run == 2
@test kw_ellipsis(a=1) == pairs((a=1,))
@test run == 2
@test kw_ellipsis(a=1, b=2) == pairs((a=1,b=2))
@test run == 3
@test kw_ellipsis(a=1, b=2) == pairs((a=1,b=2))
@test run == 3
else
@test kw_ellipsis() == []
@test run == 1
@test kw_ellipsis() == []
@test run == 1
@test kw_ellipsis(a=1) == Any[(:a, 1)]
@test run == 2
@test kw_ellipsis(a=1) == Any[(:a, 1)]
@test run == 2
@test kw_ellipsis(a=1, b=2) == Any[(:a, 1), (:b, 2)]
@test run == 3
@test kw_ellipsis(a=1, b=2) == Any[(:a, 1), (:b, 2)]
@test run == 3
end

run = 0
@memoize function multiple_dispatch(a::Int)
Expand Down Expand Up @@ -218,7 +233,7 @@ function outer()
@test run == 3
end
outer()
@test !isdefined(:inner)
@test !@isdefined inner

if VERSION >= v"0.5.0-dev+5235"
@memoize function typeinf(x)
Expand All @@ -232,10 +247,10 @@ println("The following method rewrite warnings are normal")
finalized = false
@memoize function method_rewrite()
x = []
finalizer(x, x->(global finalized; finalized = true))
finalizer(x->(global finalized; finalized = true),x)
x
end
method_rewrite()
@memoize function method_rewrite() end
gc()
GC.gc()
@test finalized

0 comments on commit 44d7438

Please sign in to comment.