Skip to content

Commit b93a942

Browse files
committed
Add fast path for types that support typemin/typemax
1 parent dbb6f49 commit b93a942

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

base/reducedim.jl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,14 @@ for (f1, f2, initval, typeextreme) in ((:min, :max, :Inf, :typemax), (:max, :min
150150
if v0 isa Number && isnan(v0)
151151
# v0 is NaN
152152
v0 = oftype(v0, $initval)
153-
elseif isunordered(v0) && !all(isunordered, A1)
153+
elseif isunordered(v0)
154154
# v0 is missing or a third-party unordered value
155-
v0 = mapreduce(f, $f2, Iterators.filter(!isunordered, A1))
155+
Tnm = nonmissingtype(Tr)
156+
if Tnm <: Union{BitInteger, IEEEFloat, BigFloat}
157+
v0 = $typeextreme(Tnm)
158+
elseif !all(isunordered, A1)
159+
v0 = mapreduce(f, $f2, Iterators.filter(!isunordered, A1))
160+
end
156161
end
157162
# v0 may have changed type.
158163
Tr = v0 isa T ? T : typeof(v0)
@@ -185,9 +190,16 @@ function reducedim_init(f::ExtremaMap, op::typeof(_extrema_rf), A::AbstractArray
185190
if v0[1] isa Number && isnan(v0[1])
186191
# v0 is NaN
187192
v0 = oftype(v0[1], Inf), oftype(v0[2], -Inf)
188-
elseif isunordered(v0[1]) && !all(isunordered, A1)
193+
elseif isunordered(v0[1])
189194
# v0 is missing or a third-party unordered value
190-
v0 = reverse(mapreduce(f, op, Iterators.filter(!isunordered, A1)))
195+
Tminnm = nonmissingtype(Tmin)
196+
Tmaxnm = nonmissingtype(Tmax)
197+
if Tminnm <: Union{BitInteger, IEEEFloat, BigFloat} &&
198+
Tmaxnm <: Union{BitInteger, IEEEFloat, BigFloat}
199+
v0 = (typemax(Tminnm), typemin(Tmaxnm))
200+
elseif !all(isunordered, A1)
201+
v0 = reverse(mapreduce(f, op, Iterators.filter(!isunordered, A1)))
202+
end
191203
end
192204
# v0 may have changed type.
193205
Tmin = v0[1] isa T ? T : typeof(v0[1])

0 commit comments

Comments
 (0)