From 71a912d1ff1ede18468742864999c5f5d3c4d348 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Mon, 23 Dec 2024 23:21:55 +0530 Subject: [PATCH 1/4] Specialize common reductions for ranges with `init` specified --- base/range.jl | 7 +++++++ test/ranges.jl | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/base/range.jl b/base/range.jl index 39428ab741955..718382c95aa81 100644 --- a/base/range.jl +++ b/base/range.jl @@ -1425,6 +1425,13 @@ function sum(r::AbstractRange{<:Real}) : (step(r) * l) * ((l-1)>>1)) end +# common reductions with init specified +for (fred, f) in [(maximum, max), (minimum, min), (sum, add_sum)] + @eval function _foldl_impl(op::typeof(BottomRF($f)), init, r::AbstractRange) + isempty(r) ? init : op(init, $fred(r)) + end +end + function _in_range(x, r::AbstractRange) isempty(r) && return false f, l = first(r), last(r) diff --git a/test/ranges.jl b/test/ranges.jl index 89134be897ddd..ba54f6cebcd7d 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -1554,8 +1554,8 @@ end (range(10, stop=20, length=5), 1, 5), (range(10.3, step=-2, length=7), 7, 1), ] - @test minimum(r) === r[imin] - @test maximum(r) === r[imax] + @test minimum(r) === minimum(r, init=typemax(eltype(r))) === r[imin] + @test maximum(r) === maximum(r, init=typemin(eltype(r))) === r[imax] @test imin === argmin(r) @test imax === argmax(r) @test extrema(r) === (r[imin], r[imax]) From 13458aaa0f8991c4369248170ed73a60446ff026 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Mon, 23 Dec 2024 23:44:08 +0530 Subject: [PATCH 2/4] Fix build --- base/range.jl | 7 ------- base/reduce.jl | 8 ++++++++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/base/range.jl b/base/range.jl index 718382c95aa81..39428ab741955 100644 --- a/base/range.jl +++ b/base/range.jl @@ -1425,13 +1425,6 @@ function sum(r::AbstractRange{<:Real}) : (step(r) * l) * ((l-1)>>1)) end -# common reductions with init specified -for (fred, f) in [(maximum, max), (minimum, min), (sum, add_sum)] - @eval function _foldl_impl(op::typeof(BottomRF($f)), init, r::AbstractRange) - isempty(r) ? init : op(init, $fred(r)) - end -end - function _in_range(x, r::AbstractRange) isempty(r) && return false f, l = first(r), last(r) diff --git a/base/reduce.jl b/base/reduce.jl index 25466eed4a105..154d98a167de6 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -61,6 +61,13 @@ function _foldl_impl(op, init, itr::Union{Tuple,NamedTuple}) @invoke _foldl_impl(op, init, itr::Any) end +# common reductions for ranges with init specified +for (fred, f) in ((maximum, max), (minimum, min), (sum, add_sum)) + @eval function _foldl_impl(op::typeof(BottomRF($f)), init, r::AbstractRange) + isempty(r) ? init : op(init, $fred(r)) + end +end + struct _InitialValue end """ @@ -1155,3 +1162,4 @@ function _simple_count(::typeof(identity), x::Array{Bool}, init::T=0) where {T} end return n end + From adea4f1b219267b1056a417caac685328cf20bf3 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Mon, 23 Dec 2024 23:45:03 +0530 Subject: [PATCH 3/4] Remove newline --- base/reduce.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/base/reduce.jl b/base/reduce.jl index 154d98a167de6..87251912e8d95 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -1162,4 +1162,3 @@ function _simple_count(::typeof(identity), x::Array{Bool}, init::T=0) where {T} end return n end - From 077907827d2171fe71e39afceb427e06971f44c5 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Tue, 24 Dec 2024 09:24:43 +0530 Subject: [PATCH 4/4] Ensure that `BottomRF` is defined before the methods are added --- base/reduce.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/base/reduce.jl b/base/reduce.jl index 87251912e8d95..60bff3acb52ed 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -61,13 +61,6 @@ function _foldl_impl(op, init, itr::Union{Tuple,NamedTuple}) @invoke _foldl_impl(op, init, itr::Any) end -# common reductions for ranges with init specified -for (fred, f) in ((maximum, max), (minimum, min), (sum, add_sum)) - @eval function _foldl_impl(op::typeof(BottomRF($f)), init, r::AbstractRange) - isempty(r) ? init : op(init, $fred(r)) - end -end - struct _InitialValue end """ @@ -1162,3 +1155,10 @@ function _simple_count(::typeof(identity), x::Array{Bool}, init::T=0) where {T} end return n end + +# A few common reductions for ranges with init specified +for (fred, f) in ((maximum, max), (minimum, min), (sum, add_sum)) + @eval function _foldl_impl(op::typeof(BottomRF($f)), init, r::AbstractRange) + isempty(r) ? init : op(init, $fred(r)) + end +end