Skip to content

Commit 12ab7e4

Browse files
committed
Try to explicitly heal invalidations
1 parent c98958b commit 12ab7e4

File tree

3 files changed

+56
-43
lines changed

3 files changed

+56
-43
lines changed

Project.toml

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "0.9.0"
66
[deps]
77
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
88
PackageExtensionCompat = "65ce6f38-6b18-4e1d-a461-8949797d7930"
9+
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
910
Tricks = "410a4b4d-49e4-4fbc-ab6d-cb71b17b3775"
1011

1112
[weakdeps]
@@ -25,6 +26,7 @@ Aqua = "0.7"
2526
Compat = "3.42, 4"
2627
Measurements = "2"
2728
PackageExtensionCompat = "1.0.2"
29+
PrecompileTools = "1"
2830
ScientificTypes = "3"
2931
Tricks = "0.1"
3032
Unitful = "1"

src/math.jl

+16-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using PrecompileTools: @recompile_invalidations
2+
13
for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
24
div_base_type = type == AbstractGenericQuantity ? Number : base_type
35
@eval begin
@@ -75,18 +77,20 @@ Base.:/(l::AbstractDimensions, r::AbstractDimensions) = map_dimensions(-, l, r)
7577
# Defines + and -
7678
for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES, op in (:+, :-)
7779
@eval begin
78-
function Base.$op(l::$type, r::$type)
79-
l, r = promote_except_value(l, r)
80-
dimension(l) == dimension(r) || throw(DimensionError(l, r))
81-
return new_quantity(typeof(l), $op(ustrip(l), ustrip(r)), dimension(l))
82-
end
83-
function Base.$op(l::$type, r::$base_type)
84-
iszero(dimension(l)) || throw(DimensionError(l, r))
85-
return new_quantity(typeof(l), $op(ustrip(l), r), dimension(l))
86-
end
87-
function Base.$op(l::$base_type, r::$type)
88-
iszero(dimension(r)) || throw(DimensionError(l, r))
89-
return new_quantity(typeof(r), $op(l, ustrip(r)), dimension(r))
80+
@recompile_invalidations begin
81+
function Base.$op(l::$type, r::$type)
82+
l, r = promote_except_value(l, r)
83+
dimension(l) == dimension(r) || throw(DimensionError(l, r))
84+
return new_quantity(typeof(l), $op(ustrip(l), ustrip(r)), dimension(l))
85+
end
86+
function Base.$op(l::$type, r::$base_type)
87+
iszero(dimension(l)) || throw(DimensionError(l, r))
88+
return new_quantity(typeof(l), $op(ustrip(l), r), dimension(l))
89+
end
90+
function Base.$op(l::$base_type, r::$type)
91+
iszero(dimension(r)) || throw(DimensionError(l, r))
92+
return new_quantity(typeof(r), $op(l, ustrip(r)), dimension(r))
93+
end
9094
end
9195
end
9296
end

src/utils.jl

+38-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import Compat: allequal
1+
using Compat: allequal
2+
using PrecompileTools: @recompile_invalidations
23

34
function map_dimensions(f::F, args::AbstractDimensions...) where {F<:Function}
45
dimension_type = promote_type(typeof(args).parameters...)
@@ -26,7 +27,7 @@ end
2627
end
2728

2829
for (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
29-
@eval Base.convert(::Type{$base_type}, q::$type) = q
30+
@eval @recompile_invalidations Base.convert(::Type{$base_type}, q::$type) = q
3031
end
3132
function Base.convert(::Type{T}, q::UnionAbstractQuantity) where {T<:Number}
3233
@assert iszero(dimension(q)) "$(typeof(q)): $(q) has dimensions! Use `ustrip` instead."
@@ -80,14 +81,16 @@ const AMBIGUOUS_NUMERIC_TYPES = (Bool, BigFloat)
8081

8182
for (type, _, _) in ABSTRACT_QUANTITY_TYPES
8283
@eval begin
83-
function Base.convert(::Type{Q}, x::BASE_NUMERIC_TYPES) where {T,D,Q<:$type{T,D}}
84-
return new_quantity(Q, convert(T, x), D())
85-
end
86-
function Base.promote_rule(::Type{Q}, ::Type{T2}) where {T,D,Q<:$type{T,D},T2<:BASE_NUMERIC_TYPES}
87-
return with_type_parameters(promote_quantity(Q, T2), promote_type(T, T2), D)
88-
end
89-
function Base.promote_rule(::Type{T2}, ::Type{Q}) where {T,D,Q<:$type{T,D},T2<:BASE_NUMERIC_TYPES}
90-
return with_type_parameters(promote_quantity(Q, T2), promote_type(T, T2), D)
84+
@recompile_invalidations begin
85+
function Base.convert(::Type{Q}, x::BASE_NUMERIC_TYPES) where {T,D,Q<:$type{T,D}}
86+
return new_quantity(Q, convert(T, x), D())
87+
end
88+
function Base.promote_rule(::Type{Q}, ::Type{T2}) where {T,D,Q<:$type{T,D},T2<:BASE_NUMERIC_TYPES}
89+
return with_type_parameters(promote_quantity(Q, T2), promote_type(T, T2), D)
90+
end
91+
function Base.promote_rule(::Type{T2}, ::Type{Q}) where {T,D,Q<:$type{T,D},T2<:BASE_NUMERIC_TYPES}
92+
return with_type_parameters(promote_quantity(Q, T2), promote_type(T, T2), D)
93+
end
9194
end
9295
end
9396
for numeric_type in AMBIGUOUS_NUMERIC_TYPES
@@ -158,32 +161,36 @@ for op in (:(<=), :(<), :(>=), :(>), :isless, :isgreater),
158161
(type, base_type, _) in ABSTRACT_QUANTITY_TYPES
159162

160163
@eval begin
161-
function Base.$(op)(l::$type, r::$type)
162-
l, r = promote_except_value(l, r)
163-
dimension(l) == dimension(r) || throw(DimensionError(l, r))
164-
return $(op)(ustrip(l), ustrip(r))
165-
end
166-
function Base.$(op)(l::$type, r::$base_type)
167-
iszero(dimension(l)) || throw(DimensionError(l, r))
168-
return $(op)(ustrip(l), r)
169-
end
170-
function Base.$(op)(l::$base_type, r::$type)
171-
iszero(dimension(r)) || throw(DimensionError(l, r))
172-
return $(op)(l, ustrip(r))
164+
@recompile_invalidations begin
165+
function Base.$(op)(l::$type, r::$type)
166+
l, r = promote_except_value(l, r)
167+
dimension(l) == dimension(r) || throw(DimensionError(l, r))
168+
return $(op)(ustrip(l), ustrip(r))
169+
end
170+
function Base.$(op)(l::$type, r::$base_type)
171+
iszero(dimension(l)) || throw(DimensionError(l, r))
172+
return $(op)(ustrip(l), r)
173+
end
174+
function Base.$(op)(l::$base_type, r::$type)
175+
iszero(dimension(r)) || throw(DimensionError(l, r))
176+
return $(op)(l, ustrip(r))
177+
end
173178
end
174179
end
175180
end
176181
for op in (:isequal, :(==)), (type, base_type, _) in ABSTRACT_QUANTITY_TYPES
177182
@eval begin
178-
function Base.$(op)(l::$type, r::$type)
179-
l, r = promote_except_value(l, r)
180-
return $(op)(ustrip(l), ustrip(r)) && dimension(l) == dimension(r)
181-
end
182-
function Base.$(op)(l::$type, r::$base_type)
183-
return $(op)(ustrip(l), r) && iszero(dimension(l))
184-
end
185-
function Base.$(op)(l::$base_type, r::$type)
186-
return $(op)(l, ustrip(r)) && iszero(dimension(r))
183+
@recompile_invalidations begin
184+
function Base.$(op)(l::$type, r::$type)
185+
l, r = promote_except_value(l, r)
186+
return $(op)(ustrip(l), ustrip(r)) && dimension(l) == dimension(r)
187+
end
188+
function Base.$(op)(l::$type, r::$base_type)
189+
return $(op)(ustrip(l), r) && iszero(dimension(l))
190+
end
191+
function Base.$(op)(l::$base_type, r::$type)
192+
return $(op)(l, ustrip(r)) && iszero(dimension(r))
193+
end
187194
end
188195
end
189196
end

0 commit comments

Comments
 (0)