Skip to content

Commit

Permalink
Merge b9b5fd3 into aafc92f
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanBieler committed Aug 23, 2018
2 parents aafc92f + b9b5fd3 commit 5fc1fcf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 24 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.build(); Pkg.test()'
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())'
12 changes: 12 additions & 0 deletions Project.toml
@@ -0,0 +1,12 @@
authors = ["Simon Kornblith <simon@simonster.com>"]
name = "Memoize"
uuid = "0fb7230c-a6dd-11e8-35c5-25eda89965fd"
version = "0.3.0"

[deps]

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
1 change: 0 additions & 1 deletion REQUIRE

This file was deleted.

18 changes: 9 additions & 9 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,13 +92,13 @@ 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"
lookup = :($fcache[($(tup...),)]::Core.Inference.return_type($u, typeof(($(identargs...),))))
if length(kws) == 0
lookup = :($fcache[($(tup...),)]::Core.Compiler.return_type($u, typeof(($(identargs...),))))
else
lookup = :($fcache[($(tup...),)])
end
Expand Down
20 changes: 10 additions & 10 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,17 +163,17 @@ run = 0
global run += 1
a
end
@test kw_ellipsis() == []
@test isempty(kw_ellipsis())
@test run == 1
@test kw_ellipsis() == []
@test isempty(kw_ellipsis())
@test run == 1
@test kw_ellipsis(a=1) == Any[(:a, 1)]
@test kw_ellipsis(a=1) == pairs((a=1,))
@test run == 2
@test kw_ellipsis(a=1) == Any[(:a, 1)]
@test kw_ellipsis(a=1) == pairs((a=1,))
@test run == 2
@test kw_ellipsis(a=1, b=2) == Any[(:a, 1), (:b, 2)]
@test kw_ellipsis(a=1, b=2) == pairs((a=1,b=2))
@test run == 3
@test kw_ellipsis(a=1, b=2) == Any[(:a, 1), (:b, 2)]
@test kw_ellipsis(a=1, b=2) == pairs((a=1,b=2))
@test run == 3

run = 0
Expand Down Expand Up @@ -218,7 +218,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 +232,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 5fc1fcf

Please sign in to comment.