Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement performance optimization of promote_operation for *(::Any, ::Zero) #284

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/rewrite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ function Base.:/(z::Zero, x::Any)
end
end

# These methods are used to provide an efficient implementation for the common
# case like `x^2 * sum(f for i in 1:0)`, which lowers to
# `_MA.operate!!(*, x^2, _MA.Zero())`. We don't need the method with reversed
# arguments because MA.Zero is not mutable, and MA never queries the mutablility
# of arguments if the first is not mutable.
promote_operation(::typeof(*), ::Type{<:Any}, ::Type{Zero}) = Zero

function promote_operation(
::typeof(*),
::Type{<:AbstractArray{T}},
::Type{Zero},
) where {T}
return Zero
end

# Needed by `@rewrite(BigInt(1) .+ sum(1 for i in 1:0) * 1^2)`
# since we don't require mutable type to support Zero in
# `mutable_operate!`.
Expand Down
67 changes: 67 additions & 0 deletions test/rewrite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,70 @@ end
b = @allocated MA.operate(LinearAlgebra.dot, x, y)
@test a == b
end

@testset "test_multiply_expr_MA_Zero" begin
x = DummyBigInt(1)
f = DummyBigInt(2)
@test MA.@rewrite(
f * sum(x for i in 1:0),
move_factors_into_sums = false
) == MA.Zero()
@test MA.@rewrite(
sum(x for i in 1:0) * f,
move_factors_into_sums = false
) == MA.Zero()
@test MA.@rewrite(
-f * sum(x for i in 1:0),
move_factors_into_sums = false
) == MA.Zero()
@test MA.@rewrite(
sum(x for i in 1:0) * -f,
move_factors_into_sums = false
) == MA.Zero()
@test MA.@rewrite(
(f + f) * sum(x for i in 1:0),
move_factors_into_sums = false
) == MA.Zero()
@test MA.@rewrite(
sum(x for i in 1:0) * (f + f),
move_factors_into_sums = false
) == MA.Zero()
@test MA.@rewrite(
-[f] * sum(x for i in 1:0),
move_factors_into_sums = false
) == MA.Zero()
@test MA.@rewrite(
sum(x for i in 1:0) * -[f],
move_factors_into_sums = false
) == MA.Zero()
@test MA.isequal_canonical(
MA.@rewrite(f + sum(x for i in 1:0), move_factors_into_sums = false),
f,
)
@test MA.isequal_canonical(
MA.@rewrite(sum(x for i in 1:0) + f, move_factors_into_sums = false),
f,
)
@test MA.isequal_canonical(
MA.@rewrite(-f + sum(x for i in 1:0), move_factors_into_sums = false),
-f,
)
@test MA.isequal_canonical(
MA.@rewrite(sum(x for i in 1:0) + -f, move_factors_into_sums = false),
-f,
)
@test MA.isequal_canonical(
MA.@rewrite(
(f + f) + sum(x for i in 1:0),
move_factors_into_sums = false
),
f + f,
)
@test MA.isequal_canonical(
MA.@rewrite(
sum(x for i in 1:0) + (f + f),
move_factors_into_sums = false
),
f + f,
)
end
Loading