Skip to content

Commit

Permalink
Cache results of subtypes to speed up dispatch function. (#1440)
Browse files Browse the repository at this point in the history
  • Loading branch information
epatters authored Oct 19, 2020
1 parent bb2dd1d commit 0962d06
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Utilities/Selectors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ Selectors.dispatch(MySelector, args...)
```
"""
function dispatch(::Type{T}, x...) where T <: AbstractSelector
for t in (sort(subtypes(T); by = order))
types = get!(selector_subtypes, T) do
sort(subtypes(T); by = order)
end
for t in types
if !disable(t) && matcher(t, x...)
runner(t, x...)
strict(t) && return
Expand All @@ -171,4 +174,9 @@ function dispatch(::Type{T}, x...) where T <: AbstractSelector
runner(T, x...)
end

# Under certain circumstances, the function `subtypes` can be very slow
# (https://github.com/JuliaLang/julia/issues/38079), so to ensure that
# `dispatch` remains fast we cache the results of `subtypes` here.
const selector_subtypes = Dict{Type,Vector}()

end

0 comments on commit 0962d06

Please sign in to comment.