forked from FluxML/Functors.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve type stability of cached walks (FluxML#82)
* improve type stability of cached walks * fix doctest format * handle old julia version
- Loading branch information
1 parent
c0936a5
commit 2945731
Showing
4 changed files
with
67 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
struct WalkCache{K, V, W <: AbstractWalk, C <: AbstractDict{K, V}} <: AbstractDict{K, V} | ||
walk::W | ||
cache::C | ||
WalkCache(walk, cache::AbstractDict{K, V} = IdDict()) where {K, V} = new{K, V, typeof(walk), typeof(cache)}(walk, cache) | ||
end | ||
Base.length(cache::WalkCache) = length(cache.cache) | ||
Base.empty!(cache::WalkCache) = empty!(cache.cache) | ||
Base.haskey(cache::WalkCache, x) = haskey(cache.cache, x) | ||
Base.get(cache::WalkCache, x, default) = haskey(cache.cache, x) ? cache[x] : default | ||
Base.iterate(cache::WalkCache, state...) = iterate(cache.cache, state...) | ||
Base.setindex!(cache::WalkCache, value, key) = setindex!(cache.cache, value, key) | ||
Base.getindex(cache::WalkCache, x) = cache.cache[x] | ||
|
||
@static if VERSION >= v"1.10.0-DEV.609" | ||
function __cacheget_generator__(world, source, self, cache, x, args #= for `return_type` only =#) | ||
# :(return cache.cache[x]::(return_type(cache.walk, typeof(args)))) | ||
walk = cache.parameters[3] | ||
RT = Core.Compiler.return_type(Tuple{walk, args...}, world) | ||
body = Expr(:call, GlobalRef(Base, :getindex), Expr(:., :cache, QuoteNode(:cache)), :x) | ||
if RT != Any | ||
body = Expr(:(::), body, RT) | ||
end | ||
expr = Expr(:lambda, [Symbol("#self#"), :cache, :x, :args], | ||
Expr(Symbol("scope-block"), Expr(:block, Expr(:meta, :inline), Expr(:return, body)))) | ||
ci = ccall(:jl_expand, Any, (Any, Any), expr, @__MODULE__) | ||
ci.inlineable = true | ||
return ci | ||
end | ||
@eval function cacheget(cache::WalkCache, x, args...) | ||
$(Expr(:meta, :generated, __cacheget_generator__)) | ||
$(Expr(:meta, :generated_only)) | ||
end | ||
else | ||
@generated function cacheget(cache::WalkCache, x, args...) | ||
walk = cache.parameters[3] | ||
world = typemax(UInt) | ||
@static if VERSION >= v"1.8" | ||
RT = Core.Compiler.return_type(Tuple{walk, args...}, world) | ||
else | ||
if isdefined(walk, :instance) | ||
RT = Core.Compiler.return_type(walk.instance, Tuple{args...}, world) | ||
else | ||
RT = Any | ||
end | ||
end | ||
body = Expr(:call, GlobalRef(Base, :getindex), Expr(:., :cache, QuoteNode(:cache)), :x) | ||
if RT != Any | ||
body = Expr(:(::), body, RT) | ||
end | ||
expr = Expr(:lambda, [Symbol("#self#"), :cache, :x, :args], | ||
Expr(Symbol("scope-block"), Expr(:block, Expr(:meta, :inline), Expr(:return, body)))) | ||
ci = ccall(:jl_expand, Any, (Any, Any), expr, @__MODULE__) | ||
ci.inlineable = true | ||
return ci | ||
end | ||
end | ||
# fallback behavior that only lookup for `x` | ||
@inline cacheget(cache::AbstractDict, x, args...) = cache[x] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters