Skip to content

Commit 2362826

Browse files
committed
perf: add specializations for mapreduce min/max
1 parent 021083f commit 2362826

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/vector_of_arrays.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ function Base.append!(A::VectorOfArrays{T,N}, B::AbstractVector{<:AbstractArray{
337337
end
338338

339339

340+
Base.mapreduce(::typeof(maximum), ::typeof(max), V::VectorOfArrays; kw...) = maximum(V.data; kw...)
341+
Base.mapreduce(::typeof(minimum), ::typeof(min), V::VectorOfArrays; kw...) = minimum(V.data; kw...)
342+
343+
340344
Base.vcat(V::VectorOfArrays) = V
341345

342346
function Base.vcat(Vs::(VectorOfArrays{U,N} where U)...) where {N}

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import Test
44

5-
Test.@testset "Package ArraysOfArrays" begin
5+
Test.@testset verbose=true "Package ArraysOfArrays" begin
66
include("test_aqua.jl")
77
include("functions.jl")
88
include("array_of_similar_arrays.jl")

test/vector_of_arrays.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,37 @@ using ArraysOfArrays: full_consistency_checks, append_elemptr!, element_ptr
7676
end
7777

7878

79+
@testset "mapreduce maximum/minimum shortcut" begin
80+
A1 = ref_AoA3(Float32, 3); A2 = ref_AoA3(Float32, 0)
81+
A3 = ref_AoA3(Float32, 4); A4 = ref_AoA3(Float64, 2)
82+
83+
B1 = VectorOfArrays(A1); B2 = VectorOfArrays(A2);
84+
B3 = VectorOfArrays(A3); B4 = VectorOfArrays(A4);
85+
86+
@testset "maximum - correctness" begin
87+
@test mapreduce(maximum, max, B1) == maximum(B1.data)
88+
@test mapreduce(maximum, max, B2; init=Float32(0.)) == maximum(B2.data; init=Float32(0.))
89+
@test mapreduce(maximum, max, B3) == maximum(B3.data)
90+
@test mapreduce(maximum, max, B4) == maximum(B4.data)
91+
end
92+
93+
@testset "maximum - performance" begin
94+
@test (@allocated mapreduce(maximum, max, B1)) == (@allocated maximum(B1.data))
95+
end
96+
97+
@testset "minimum - correctness" begin
98+
@test mapreduce(minimum, min, B1) == minimum(B1.data)
99+
@test mapreduce(minimum, min, B2; init=Float32(0.)) == minimum(B2.data; init=Float32(0.))
100+
@test mapreduce(minimum, min, B3) == minimum(B3.data)
101+
@test mapreduce(minimum, min, B4) == minimum(B4.data)
102+
end
103+
104+
@testset "minimum - performance" begin
105+
@test (@allocated mapreduce(minimum, min, B1)) == (@allocated minimum(B1.data))
106+
end
107+
end
108+
109+
79110
@testset "append! and vcat" begin
80111
A1 = ref_AoA3(Float32, 3); A2 = ref_AoA3(Float32, 0)
81112
A3 = ref_AoA3(Float32, 4); A4 = ref_AoA3(Float64, 2)

0 commit comments

Comments
 (0)