Skip to content

Commit

Permalink
When Base has the unwrap function, we can avoid defining generic fall…
Browse files Browse the repository at this point in the history
…back convert methods that cause lots of recompilation of convert methods. This improves the package load times from 1.4s to 1.0s for me. Fixes #177. Failures on nightly are unrelated
  • Loading branch information
quinnj committed Jul 26, 2019
1 parent 579e1e1 commit 847d3d5
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions src/value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,48 +80,39 @@ Base.promote_rule(::Type{C1}, ::Type{C2}) where {C1<:CatValue, C2<:CatValue} =

Base.convert(::Type{Ref}, x::CatValue) = RefValue{leveltype(x)}(x)
Base.convert(::Type{String}, x::CatValue) = convert(String, get(x))
Base.convert(::Type{Any}, x::CatValue) = x

# Defined separately to avoid ambiguities
Base.convert(::Type{AbstractString}, x::CategoricalString) = x
Base.convert(::Type{T}, x::T) where {T <: CatValue} = x
Base.convert(::Type{Union{T, Missing}}, x::T) where {T <: CatValue} = x
Base.convert(::Type{Union{T, Nothing}}, x::T) where {T <: CatValue} = x

@static if isdefined(Base, :unwrap)
Base.unwrap(x::CatValue) = get(x)
else
# General fallbacks
Base.convert(::Type{S}, x::T) where {S, T <: CatValue} =
T <: S ? x : convert(S, get(x))
Base.convert(::Type{Union{S, Missing}}, x::T) where {S, T <: CatValue} =
T <: S ? x : convert(S, get(x))
Base.convert(::Type{Union{S, Nothing}}, x::T) where {S, T <: CatValue} =
T <: S ? x : convert(S, get(x))
Base.convert(::Type{Any}, x::CatValue) = x
Base.convert(::Type{S}, x::T) where {S, T <: CatValue} =
T <: S ? x : convert(S, get(x))
Base.convert(::Type{Union{S, Missing}}, x::T) where {S, T <: CatValue} =
T <: S ? x : convert(S, get(x))
Base.convert(::Type{Union{S, Nothing}}, x::T) where {S, T <: CatValue} =
T <: S ? x : convert(S, get(x))
end

(::Type{T})(x::T) where {T <: CatValue} = x

Base.Broadcast.broadcastable(x::CatValue) = Ref(x)

if VERSION >= v"0.7.0-DEV.2797"
function Base.show(io::IO, x::CatValue)
if Missings.T(get(io, :typeinfo, Any)) === Missings.T(typeof(x))
print(io, repr(x))
elseif isordered(pool(x))
@printf(io, "%s %s (%i/%i)",
typeof(x), repr(x),
order(x), length(pool(x)))
else
@printf(io, "%s %s", typeof(x), repr(x))
end
end
else
function Base.show(io::IO, x::CatValue)
if get(io, :compact, false)
print(io, repr(x))
elseif isordered(pool(x))
@printf(io, "%s %s (%i/%i)",
typeof(x), repr(x),
order(x), length(pool(x)))
else
@printf(io, "%s %s", typeof(x), repr(x))
end
function Base.show(io::IO, x::CatValue)
if Missings.T(get(io, :typeinfo, Any)) === Missings.T(typeof(x))
print(io, repr(x))
elseif isordered(pool(x))
@printf(io, "%s %s (%i/%i)",
typeof(x), repr(x),
order(x), length(pool(x)))
else
@printf(io, "%s %s", typeof(x), repr(x))
end
end

Expand Down

0 comments on commit 847d3d5

Please sign in to comment.