Skip to content

Commit 81864cb

Browse files
authored
Remove more type instability (especially in applyreduce) (#84)
* Specify the operator type in `applyreduce` This speeds up all of the area and contour functions. * Type annotation on `_booltype` return type Somehow this was still a bit unstable... * `OT` -> `O`
1 parent 9a7d4c4 commit 81864cb

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

src/primitives.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct _True <: BoolsAsTypes end
2020
struct _False <: BoolsAsTypes end
2121

2222
@inline _booltype(x::Bool)::BoolsAsTypes = x ? _True() : _False()
23-
@inline _booltype(x::BoolsAsTypes) = x
23+
@inline _booltype(x::BoolsAsTypes)::BoolsAsTypes = x
2424

2525
"""
2626
TraitTarget{T}
@@ -260,21 +260,21 @@ If `threaded==true` threads will be used over arrays and iterables,
260260
feature collections and nested geometries.
261261
"""
262262
@inline function applyreduce(
263-
f::F, op, target, geom; threaded=false, init=nothing
264-
) where F
263+
f::F, op::O, target, geom; threaded=false, init=nothing
264+
) where {F, O}
265265
threaded = _booltype(threaded)
266266
_applyreduce(f, op, TraitTarget(target), geom; threaded, init)
267267
end
268268

269-
@inline _applyreduce(f::F, op, target, geom; threaded, init) where F =
269+
@inline _applyreduce(f::F, op::O, target, geom; threaded, init) where {F, O} =
270270
_applyreduce(f, op, target, GI.trait(geom), geom; threaded, init)
271271
# Maybe use threads recucing over arrays
272-
@inline function _applyreduce(f::F, op, target, ::Nothing, A::AbstractArray; threaded, init) where F
272+
@inline function _applyreduce(f::F, op::O, target, ::Nothing, A::AbstractArray; threaded, init) where {F, O}
273273
applyreduce_array(i) = _applyreduce(f, op, target, A[i]; threaded=_False(), init)
274274
_mapreducetasks(applyreduce_array, op, eachindex(A), threaded; init)
275275
end
276276
# Try to applyreduce over iterables
277-
@inline function _applyreduce(f::F, op, target, ::Nothing, iterable; threaded, init) where F
277+
@inline function _applyreduce(f::F, op::O, target, ::Nothing, iterable; threaded, init) where {F, O}
278278
applyreduce_iterable(i) = _applyreduce(f, op, target, x; threaded=_False(), init)
279279
if threaded # Try to `collect` and reduce over the vector with threads
280280
_applyreduce(f, op, target, collect(iterable); threaded, init)
@@ -284,27 +284,27 @@ end
284284
end
285285
end
286286
# Maybe use threads reducing over features of feature collections
287-
@inline function _applyreduce(f::F, op, target, ::GI.FeatureCollectionTrait, fc; threaded, init) where F
287+
@inline function _applyreduce(f::F, op::O, target, ::GI.FeatureCollectionTrait, fc; threaded, init) where {F, O}
288288
applyreduce_fc(i) = _applyreduce(f, op, target, GI.getfeature(fc, i); threaded=_False(), init)
289289
_mapreducetasks(applyreduce_fc, op, 1:GI.nfeature(fc), threaded; init)
290290
end
291291
# Features just applyreduce to their geometry
292-
@inline _applyreduce(f::F, op, target, ::GI.FeatureTrait, feature; threaded, init) where F =
292+
@inline _applyreduce(f::F, op::O, target, ::GI.FeatureTrait, feature; threaded, init) where {F, O} =
293293
_applyreduce(f, op, target, GI.geometry(feature); threaded, init)
294294
# Maybe use threads over components of nested geometries
295-
@inline function _applyreduce(f::F, op, target, trait, geom; threaded, init) where F
295+
@inline function _applyreduce(f::F, op::O, target, trait, geom; threaded, init) where {F, O}
296296
applyreduce_geom(i) = _applyreduce(f, op, target, GI.getgeom(geom, i); threaded=_False(), init)
297297
_mapreducetasks(applyreduce_geom, op, 1:GI.ngeom(geom), threaded; init)
298298
end
299299
# Don't thread over points it won't pay off
300300
@inline function _applyreduce(
301-
f::F, op, target, trait::Union{GI.LinearRing,GI.LineString,GI.MultiPoint}, geom;
301+
f::F, op::O, target, trait::Union{GI.LinearRing,GI.LineString,GI.MultiPoint}, geom;
302302
threaded, init
303-
) where F
303+
) where {F, O}
304304
_applyreduce(f, op, target, GI.getgeom(geom); threaded=_False(), init)
305305
end
306306
# Apply f to the target
307-
@inline function _applyreduce(f::F, op, ::TraitTarget{Target}, ::Trait, x; kw...) where {F,Target,Trait<:Target}
307+
@inline function _applyreduce(f::F, op::O, ::TraitTarget{Target}, ::Trait, x; kw...) where {F,O,Target,Trait<:Target}
308308
f(x)
309309
end
310310
# Fail if we hit PointTrait
@@ -315,7 +315,7 @@ for T in (
315315
GI.PointTrait, GI.LinearRing, GI.LineString,
316316
GI.MultiPoint, GI.FeatureTrait, GI.FeatureCollectionTrait
317317
)
318-
@eval _applyreduce(f::F, op, ::TraitTarget{<:$T}, trait::$T, x; kw...) where F = f(x)
318+
@eval _applyreduce(f::F, op::O, ::TraitTarget{<:$T}, trait::$T, x; kw...) where {F, O} = f(x)
319319
end
320320

321321
"""

0 commit comments

Comments
 (0)