Skip to content

Commit

Permalink
Fix some major invalidations to improve compile times (#509)
Browse files Browse the repository at this point in the history
* Fix some major invalidations to improve compile times

Unitful.jl was flagged as a major invalidator of compilation downstream. Test:

```julia
# From: https://timholy.github.io/SnoopCompile.jl/stable/snoopr/
using SnoopCompile
invalidations = @Snoopr begin
    using DifferentialEquations

    function lorenz(du,u,p,t)
     du[1] = 10.0(u[2]-u[1])
     du[2] = u[1]*(28.0-u[3]) - u[2]
     du[3] = u[1]*u[2] - (8/3)*u[3]
    end
    u0 = [1.0;0.0;0.0]
    tspan = (0.0,100.0)
    prob = ODEProblem(lorenz,u0,tspan)
    alg = Rodas5()
    tinf = solve(prob,alg)
end;

trees = SnoopCompile.invalidation_trees(invalidations);

@show length(SnoopCompile.uinvalidated(invalidations)) # show total invalidations

show(trees[end]) # show the most invalidated method
```

This method won the prize for the absolute most invalidations 🎉. But I think the bigger issue is that it simply doesn't follow Julia semantics. It fixes the types for issue #127 in a way that gives a stricter type than Julia would do in the normal cases (which is why the invalidation occurs).

After this PR, heterogeneous arrays of numbers with Quantity in there act normally, and compile times are back to normal. Here's a showcase of it being normal:

```julia

using Unitful, Test
m = u"m"
cm = u"cm"

b = Union{Complex,Float64}[0 + 0im, 0.0]
@test b + b == b
@test b .+ b == b
@test eltype(b+b) === Number

b = Number[0 + 0im, 0.0]
@test b + b == b
@test b .+ b == b
@test eltype(b+b) === Number

b = [0.0, 0.0m]
@test b + b == b
@test b .+ b == b
@test eltype(b+b) === Number
```

* Update promotion.jl
  • Loading branch information
ChrisRackauckas authored Dec 27, 2021
1 parent c504c5f commit 0d3b586
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 3 deletions.
2 changes: 0 additions & 2 deletions src/promotion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,3 @@ Base.promote_rule(::Type{Quantity{T}}, ::Type{Quantity{S,D,U}}) where {T,S,D,U}

Base.promote_rule(::Type{Quantity{S,D,U}}, ::Type{Quantity{T}}) where {T,S,D,U} =
Quantity{promote_type(T,S)}

Base.promote_typejoin(::Type{T}, ::Type{Quantity{T,D,U}}) where {T,D,U} = Quantity{T}
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1263,7 +1263,7 @@ end
b = [0.0, 0.0m]
@test b + b == b
@test b .+ b == b
@test eltype(b+b) === Quantity{Float64}
@test eltype(b+b) === Number

# Dimensionless quantities
@test @inferred([1mm/m] + [1.0cm/m]) == [0.011]
Expand Down

1 comment on commit 0d3b586

@hustf
Copy link

@hustf hustf commented on 0d3b586 Aug 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, but is this enough? Adding b +b now changes the type.

julia> b = [0.0, 0.0m]
2-element Vector{Quantity{Float64}}:
   0.0
 0.0 m

julia> b+b
2-element Vector{Number}:
     0.0
 0.0 m

Please sign in to comment.