From bd41374afb8144318656007c7ffef80e9ef35953 Mon Sep 17 00:00:00 2001 From: Harrison Grodin Date: Wed, 12 Dec 2018 21:45:33 -0500 Subject: [PATCH 1/4] Remove unnecessary allocations in term traversal --- src/types.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/types.jl b/src/types.jl index 5d9e0ad..3ed57a2 100644 --- a/src/types.jl +++ b/src/types.jl @@ -18,10 +18,10 @@ struct Term end (f::Term)(xs...) = convert(Term, Expr(:call, f, xs...)) Base.convert(::Type{Term}, ex::Term) = ex -Base.convert(::Type{Term}, ex) = Term(traverse(ex)) -traverse(t::Term) = get(t) -traverse(ex::Expr) = Expr(ex.head, traverse.(ex.args)...) -traverse(x) = x +Base.convert(::Type{Term}, ex) = Term(traverse!(ex)) +traverse!(t::Term) = get(t) +traverse!(ex::Expr) = (@. ex.args = traverse!(ex.args); ex) +traverse!(x) = x Base.:(==)(a::Term, b::Term) = a.ex == b.ex Base.hash(t::Term, h::UInt) = hash(t.ex, hash(Term, h)) From 0432d14cad4f9c7cb205ef530d08bedaee07c7e2 Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Sun, 16 Dec 2018 15:50:56 -0700 Subject: [PATCH 2/4] Memoize (#63) --- Manifest.toml | 12 +++++++++--- Project.toml | 1 + src/rule.jl | 4 ++-- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Manifest.toml b/Manifest.toml index 7d60d96..53ef6e0 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -18,7 +18,7 @@ deps = ["LinearAlgebra", "Random", "Serialization", "Sockets"] uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" [[InteractiveUtils]] -deps = ["LinearAlgebra", "Markdown"] +deps = ["Markdown"] uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240" [[Libdl]] @@ -35,11 +35,17 @@ uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" deps = ["Base64"] uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" +[[Memoize]] +deps = ["Test"] +git-tree-sha1 = "d9683969337ad20dc957122038bd2c0ead4b5209" +uuid = "c03570c3-d221-55d1-a50c-7939bbd78826" +version = "0.3.0" + [[Polynomials]] deps = ["LinearAlgebra", "SparseArrays", "Test"] -git-tree-sha1 = "af883752c4935425a3ab30031a2069254c451b8b" +git-tree-sha1 = "1a1eae52956658a6acae6fa1b6d6c3d488192895" uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45" -version = "0.5.0" +version = "0.5.1" [[Random]] deps = ["Serialization"] diff --git a/Project.toml b/Project.toml index 38c5278..9b1b122 100644 --- a/Project.toml +++ b/Project.toml @@ -6,6 +6,7 @@ version = "0.1.0" [deps] Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b" +Memoize = "c03570c3-d221-55d1-a50c-7939bbd78826" SpecialSets = "c6b63f46-9023-11e8-3b28-1f7d8c94880d" [extras] diff --git a/src/rule.jl b/src/rule.jl index 507ccdd..97a463f 100644 --- a/src/rule.jl +++ b/src/rule.jl @@ -1,4 +1,4 @@ -using DiffRules +using DiffRules, Memoize export Rules export normalize @@ -26,7 +26,7 @@ Base.vcat(rss::Rules...) = Rules([(rs.rules for rs ∈ rss)...;]) normalize(rs::Rules) = Base.Fix2(normalize, rs) -function normalize(t::Term, rs::Rules) +@memoize function normalize(t::Term, rs::Rules) while true t = map(normalize(rs), t) # FIXME: replace with `subexpressions` t′ = foldl(normalize, rs; init=t) From 16ec33a348963f01c192d53a9d1d2f89f92afb4b Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Mon, 17 Dec 2018 16:11:18 -0700 Subject: [PATCH 3/4] remove spurious text --- Manifest.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/Manifest.toml b/Manifest.toml index 52ee891..4d2efe9 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -58,7 +58,6 @@ version = "0.4.4" deps = ["Base64"] uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" - [[Memoize]] deps = ["Test"] git-tree-sha1 = "d9683969337ad20dc957122038bd2c0ead4b5209" @@ -78,7 +77,6 @@ git-tree-sha1 = "1a1eae52956658a6acae6fa1b6d6c3d488192895" uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45" version = "0.5.1" - [[Printf]] deps = ["Unicode"] uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" @@ -86,7 +84,6 @@ uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7" [[REPL]] deps = ["InteractiveUtils", "Markdown", "Sockets"] uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" ->>>>>>> master [[Random]] deps = ["Serialization"] From 69d3d6235c216fafca30a492e36877d9535f206a Mon Sep 17 00:00:00 2001 From: Harrison Grodin Date: Thu, 3 Jan 2019 11:41:19 -0500 Subject: [PATCH 4/4] Revert #63 --- Manifest.toml | 10 +++------- Project.toml | 1 - src/rule.jl | 4 ++-- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/Manifest.toml b/Manifest.toml index 4d2efe9..38c4bac 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -1,3 +1,5 @@ +# This file is machine-generated - editing it directly is not advised + [[Base64]] uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" @@ -28,7 +30,7 @@ uuid = "b552c78f-8df3-52c6-915a-8e097449b14b" version = "0.0.7" [[Distributed]] -deps = ["LinearAlgebra", "Random", "Serialization", "Sockets"] +deps = ["Random", "Serialization", "Sockets"] uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b" [[InteractiveUtils]] @@ -58,12 +60,6 @@ version = "0.4.4" deps = ["Base64"] uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" -[[Memoize]] -deps = ["Test"] -git-tree-sha1 = "d9683969337ad20dc957122038bd2c0ead4b5209" -uuid = "c03570c3-d221-55d1-a50c-7939bbd78826" -version = "0.3.0" - [[Mmap]] uuid = "a63ad114-7e13-5084-954f-fe012c677804" diff --git a/Project.toml b/Project.toml index ccbb503..c5d701c 100644 --- a/Project.toml +++ b/Project.toml @@ -6,7 +6,6 @@ version = "0.1.0" [deps] Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b" -Memoize = "c03570c3-d221-55d1-a50c-7939bbd78826" MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" SpecialSets = "c6b63f46-9023-11e8-3b28-1f7d8c94880d" diff --git a/src/rule.jl b/src/rule.jl index 97a463f..507ccdd 100644 --- a/src/rule.jl +++ b/src/rule.jl @@ -1,4 +1,4 @@ -using DiffRules, Memoize +using DiffRules export Rules export normalize @@ -26,7 +26,7 @@ Base.vcat(rss::Rules...) = Rules([(rs.rules for rs ∈ rss)...;]) normalize(rs::Rules) = Base.Fix2(normalize, rs) -@memoize function normalize(t::Term, rs::Rules) +function normalize(t::Term, rs::Rules) while true t = map(normalize(rs), t) # FIXME: replace with `subexpressions` t′ = foldl(normalize, rs; init=t)