From 2ffff67f40ee8a32fbf8bc0f64f2675f8051fec3 Mon Sep 17 00:00:00 2001 From: Evan Patterson Date: Sun, 18 Oct 2020 17:43:27 -0700 Subject: [PATCH] Cache results of `subtypes` to speed up `dispatch` function. --- src/Utilities/Selectors.jl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Utilities/Selectors.jl b/src/Utilities/Selectors.jl index 7ac79bf056..c90b500626 100644 --- a/src/Utilities/Selectors.jl +++ b/src/Utilities/Selectors.jl @@ -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 @@ -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