Skip to content

Commit

Permalink
feat(Core): introduce a QualifiedType cache
Browse files Browse the repository at this point in the history
While it seems like this wouldn't be needed, on a large data collection
this change produced a ~60% decrease in parsing/construction (11s → 4s).
  • Loading branch information
tecosaur committed Oct 13, 2024
1 parent 2b18db6 commit 24ffbe8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Core/src/model/globals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ const DATASET_REFERENCE_REGEX =
"(.+)", DATASET_REFERENCE_WRAPPER[2],
"\$"))

"""
QUALIFIED_TYPE_CACHE
A cache of [`QualifiedType`](@ref) instances, indexed by the type they represent.
While one would hope that `QualifiedType(::Type)` calls would be constant-folded,
in practice this is not the case, and so this cache is used to avoid an unfortunate
large performance hit when constructing many `QualifiedType` instances.
"""
const QUALIFIED_TYPE_CACHE = Dict{Type, QualifiedType}()

# For plugins / general information

"""
Expand Down
6 changes: 5 additions & 1 deletion Core/src/model/qualifiedtype.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ QualifiedType(m::Symbol, parents::Vector{Symbol}, name::Symbol) =
QualifiedType(m, parents, name, ())

function QualifiedType(::Type{T0}) where {T0}
let qt = get(QUALIFIED_TYPE_CACHE, T0, nothing)
!isnothing(qt) && return qt
end
T = Base.unwrap_unionall(T0)
alias = Base.make_typealias(T)
root, name, params = if isnothing(alias)
Expand All @@ -22,7 +25,8 @@ function QualifiedType(::Type{T0}) where {T0}
push!(parents, nameof(root))
root = parentmodule(root)
end
QualifiedType(nameof(root), parents, name, Tuple(params))
QUALIFIED_TYPE_CACHE[T0] =
QualifiedType(nameof(root), parents, name, Tuple(params))
end

Base.:(==)(a::QualifiedType, b::QualifiedType) =
Expand Down

0 comments on commit 24ffbe8

Please sign in to comment.