diff --git a/test/big.jl b/test/big.jl index 7c1b823..7205ea2 100644 --- a/test/big.jl +++ b/test/big.jl @@ -26,8 +26,8 @@ function allocation_test( A = MA.copy_if_mutable(a) @test A === short(A, b) @test g == A - alloc_test_le(() -> short(A, b), n) - alloc_test_le(() -> short_to(c, a, b), n) + alloc_test(() -> short(A, b), n) + alloc_test(() -> short_to(c, a, b), n) @test g == MA.buffered_operate!(nothing, op, MA.copy_if_mutable(a), b) @test g == MA.buffered_operate_to!(nothing, c, op, a, b) buffer = MA.buffer_for(op, typeof(a), typeof(b)) diff --git a/test/broadcast.jl b/test/broadcast.jl index 53b57ee..ba7858a 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -31,7 +31,7 @@ end @test y == 5 # FIXME This should not allocate but I couldn't figure out where these # allocations come from. - n = (VERSION >= v"1.11" ? 42 : 30) * sizeof(Int) + n = 6 * @allocated(BigInt(1)) alloc_test(() -> MA.broadcast!!(+, a, b), n) alloc_test(() -> MA.broadcast!!(+, a, c), 0) end diff --git a/test/dispatch.jl b/test/dispatch.jl index 946405a..272ea8c 100644 --- a/test/dispatch.jl +++ b/test/dispatch.jl @@ -15,12 +15,13 @@ function dispatch_tests(::Type{T}) where {T} c = one(T) x = convert.(T, [1, 2, 3]) # Need to allocate 1 BigInt for the result and one for the buffer - alloc_test(() -> MA.fused_map_reduce(MA.add_mul, x, x), 2BIGINT_ALLOC) - alloc_test(() -> MA.fused_map_reduce(MA.add_dot, x, x), 2BIGINT_ALLOC) + nalloc = 3 * @allocated(BigInt(1)) + alloc_test(() -> MA.fused_map_reduce(MA.add_mul, x, x), nalloc) + alloc_test(() -> MA.fused_map_reduce(MA.add_dot, x, x), nalloc) if T <: MA.AbstractMutable - alloc_test(() -> x'x, 2BIGINT_ALLOC) - alloc_test(() -> transpose(x) * x, 2BIGINT_ALLOC) - alloc_test(() -> LinearAlgebra.dot(x, x), 2BIGINT_ALLOC) + alloc_test(() -> x'x, nalloc) + alloc_test(() -> transpose(x) * x, nalloc) + alloc_test(() -> LinearAlgebra.dot(x, x), nalloc) end end diff --git a/test/matmul.jl b/test/matmul.jl index 49ef20f..9737dbf 100644 --- a/test/matmul.jl +++ b/test/matmul.jl @@ -204,7 +204,7 @@ end ) alloc_test(() -> MA.mutability(y, MA.add_mul, y, A, x), 0) end - + BIGINT_ALLOC = 2 * sizeof(Int) + @allocated(BigInt(1)) alloc_test(() -> MA.add_mul!!(y, A, x), BIGINT_ALLOC) alloc_test( () -> MA.operate_fallback!!(MA.IsMutable(), MA.add_mul, y, A, x), @@ -277,7 +277,7 @@ end ) alloc_test(() -> MA.mutability(C, MA.add_mul, C, A, B), 0) end - + BIGINT_ALLOC = 2 * sizeof(Int) + @allocated(BigInt(1)) alloc_test(() -> MA.add_mul!!(C, A, B), BIGINT_ALLOC) alloc_test(() -> MA.operate!!(MA.add_mul, C, A, B), BIGINT_ALLOC) alloc_test(() -> MA.operate!(MA.add_mul, C, A, B), BIGINT_ALLOC) @@ -473,8 +473,8 @@ function test_sparse_vector_sum(::Type{T}) where {T} y = copy(x) z = copy(y) # FIXME not sure what is allocating - alloc_test_le(() -> MA.operate!(+, y, z), 200) - alloc_test_le(() -> MA.operate!(-, y, z), 200) + alloc_test(() -> MA.operate!(+, y, z), 200) + alloc_test(() -> MA.operate!(-, y, z), 200) alloc_test(() -> MA.operate_to!(x, +, y, z), 0) alloc_test(() -> MA.operate_to!(x, -, y, z), 0) alloc_test(() -> MA.operate_to!(x, +, y), 0) diff --git a/test/rewrite_generic.jl b/test/rewrite_generic.jl index e3a465c..9b86c07 100644 --- a/test/rewrite_generic.jl +++ b/test/rewrite_generic.jl @@ -480,20 +480,18 @@ function test_allocations_rewrite_unary_minus() MA.@rewrite(-sum(x[i] for i in 1:N), move_factors_into_sums = false) MA.@rewrite(sum(a * x[i] for i in 1:N), move_factors_into_sums = false) sum(-x[i] for i in 1:N) - total = @allocated sum(-x[i] for i in 1:N) - # actual value = @allocated( MA.@rewrite(sum(-x[i] for i in 1:N), move_factors_into_sums = false), ) - @test value < total + @test value < @allocated sum(-x[i] for i in 1:N) value = @allocated( MA.@rewrite(-sum(x[i] for i in 1:N), move_factors_into_sums = false), ) - @test value < total + @test value < @allocated -sum(x[i] for i in 1:N) value = @allocated( MA.@rewrite(sum(a * x[i] for i in 1:N), move_factors_into_sums = false), ) - @test value < total + @test value < @allocated sum(a * x[i] for i in 1:N) return end diff --git a/test/utilities.jl b/test/utilities.jl index eb8c5c3..93db66e 100644 --- a/test/utilities.jl +++ b/test/utilities.jl @@ -6,21 +6,9 @@ include("dummy.jl") -# Allocating size for allocating a `BigInt`. Half size on 32-bit. -const BIGINT_ALLOC = @static if VERSION >= v"1.12-beta1" - Sys.WORD_SIZE == 64 ? 72 : 36 -elseif VERSION >= v"1.11" - Sys.WORD_SIZE == 64 ? 56 : 28 -else - Sys.WORD_SIZE == 64 ? 48 : 24 -end - -function alloc_test(f, n) - f() # compile - @test n == @allocated f() -end - -function alloc_test_le(f, n) +function alloc_test(f::F, expected_upper_bound::Integer) where {F<:Function} f() # compile - @test n >= @allocated f() + measured_allocations = @allocated f() + @test measured_allocations <= expected_upper_bound + return end