Skip to content

Commit

Permalink
Improve precompilation with concrete typing
Browse files Browse the repository at this point in the history
MSC was being called with non-inferrable argument types and consequently
was an invalidation tigger (and risk). This also specifies concrete types
for some of our bigger functions, which makes them more likely to compile
only once, and then uses small normalization wrappers to handle argument
diversity.

This cuts out more than a dozen invalidations.
  • Loading branch information
timholy committed Aug 20, 2020
1 parent 0e37ed4 commit be8085a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
18 changes: 13 additions & 5 deletions src/colormaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ Extra control is provided by keyword arguments.
You can also use keyword argument names that match the argument names in
[`sequential_palette`](@ref) or [`diverging_palette`](@ref).
"""
function colormap(cname::AbstractString, N::Int=100; mid=0.5, logscale=false, kvs...)
function colormap(cname::String, N::Int=100; mid=0.5, logscale::Bool=false, kvs...)

cname = lowercase(cname)
if haskey(colormaps_sequential, cname)
Expand All @@ -295,8 +295,10 @@ function colormap(cname::AbstractString, N::Int=100; mid=0.5, logscale=false, kv
end
end

return sequential_palette(p[1], N, w=p[2], d=p[3], c=p[4], s=p[5], b=p[6], wcolor=p[7],
dcolor=p[8], logscale=logscale)
# To avoid invalidation risk, it's best to make the call in a manner that inference knows the types
return sequential_palette(f64(p[1]), N, w=f64(p[2]), d=f64(p[3]), c=f64(p[4]),
s=f64(p[5]), b=f64(p[6]), wcolor=rgb8(p[7]),
dcolor=rgb8(p[8]), logscale=logscale)

elseif haskey(colormaps_diverging, cname)
allowedkeys = [:h1, :h2, :w, :d1, :d2, :c, :s, :b, :wcolor, :dcolor1, :dcolor2]
Expand All @@ -310,11 +312,17 @@ function colormap(cname::AbstractString, N::Int=100; mid=0.5, logscale=false, kv
end
end

return diverging_palette(p[1], p[2], N, w=p[3], d1=p[4], d2=p[5], c=p[6], s=p[7],
b=p[8], wcolor=p[9], dcolor1=p[10], dcolor2=p[11], mid=mid,
return diverging_palette(f64(p[1]), f64(p[2]), N, w=f64(p[3]), d1=f64(p[4]), d2=f64(p[5]),
c=f64(p[6]), s=f64(p[7]), b=f64(p[8]), wcolor=rgb8(p[9]),
dcolor1=rgb8(p[10]), dcolor2=rgb8(p[11]), mid=mid,
logscale=logscale)

else
throw(ArgumentError(string("Unknown colormap: ", cname)))
end
end

colormap(cname::AbstractString, args...; kwargs...) = colormap(String(cname), args...; kwargs...)

f64(x) = Float64(x)::Float64
rgb8(c) = RGB{N0f8}(c)::RGB{N0f8}
8 changes: 4 additions & 4 deletions src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function parse_alpha_num(num::AbstractString)
end
end

function _parse_colorant(desc::AbstractString)
function _parse_colorant(desc::String)
n0f8(x) = reinterpret(N0f8, unsafe_trunc(UInt8, x))
mat = match(col_pat_hex, desc)
if mat !== nothing
Expand Down Expand Up @@ -152,8 +152,8 @@ function _parse_colorant(desc::AbstractString)
end

# note: these exist to enable proper dispatch, since super(Colorant) == Any
_parse_colorant(::Type{C}, ::Type{SUP}, desc::AbstractString) where {C<:Colorant,SUP<:Any} = _parse_colorant(desc)
_parse_colorant(::Type{C}, ::Type{SUP}, desc::AbstractString) where {C<:Colorant,SUP<:Colorant} = convert(C, _parse_colorant(desc))::C
_parse_colorant(::Type{C}, ::Type{SUP}, desc::String) where {C<:Colorant,SUP<:Any} = _parse_colorant(desc)
_parse_colorant(::Type{C}, ::Type{SUP}, desc::String) where {C<:Colorant,SUP<:Colorant} = convert(C, _parse_colorant(desc))::C

"""
parse(Colorant, desc)
Expand Down Expand Up @@ -207,7 +207,7 @@ an `RGB`); any more specific choice will return a color of the specified type.
ARGB{N0f8}(0.533,0.0,0.667,1.0)
```
"""
Base.parse(::Type{C}, desc::AbstractString) where {C<:Colorant} = _parse_colorant(C, supertype(C), desc)
Base.parse(::Type{C}, desc::AbstractString) where {C<:Colorant} = _parse_colorant(C, supertype(C), String(desc))
Base.parse(::Type{C}, desc::Symbol) where {C<:Colorant} = parse(C, string(desc))
Base.parse(::Type{C}, c::Colorant) where {C<:Colorant} = c

Expand Down

0 comments on commit be8085a

Please sign in to comment.