diff --git a/src/aff_expr.jl b/src/aff_expr.jl index 53fe7d91581..e4ad815f32a 100644 --- a/src/aff_expr.jl +++ b/src/aff_expr.jl @@ -17,9 +17,12 @@ # Utilities for OrderedDict function _add_or_set!(dict::OrderedDict{K,V}, k::K, v::V) where {K,V} + # Adding zero terms to this dictionary leads to unacceptable performance + # degradations. See, e.g., https://github.com/JuliaOpt/JuMP.jl/issues/1946. + if iszero(v) + return dict # No-op. + end # TODO: This unnecessarily requires two lookups for k. - # TODO: Decide if we want to drop zeros here after understanding the - # performance implications. dict[k] = get!(dict, k, zero(V)) + v return dict end diff --git a/test/expr.jl b/test/expr.jl index 796a319a34c..2a8005e77bb 100644 --- a/test/expr.jl +++ b/test/expr.jl @@ -25,12 +25,12 @@ function expressions_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType @testset "drop_zeros!(::GenericAffExpr)" begin m = ModelType() @variable(m, x[1:2]) - expr = x[1] + 0.0 * x[2] + 1 + expr = x[1] + x[2] - x[2] + 1 @test !isequal(expr, x[1] + 1) JuMP.drop_zeros!(expr) @test isequal(expr, x[1] + 1) end - + @testset "iszero(::GenericAffExpr)" begin m = ModelType() @variable(m, x) @@ -55,12 +55,12 @@ function expressions_test(ModelType::Type{<:JuMP.AbstractModel}, VariableRefType @testset "drop_zeros!(::GenericQuadExpr)" begin m = ModelType() @variable(m, x[1:2]) - expr = x[1]^2 + 0.0 * x[2]^2 + x[1] + 0.0 * x[2] + 1 + expr = x[1]^2 + x[2]^2 - x[2]^2 + x[1] + x[2] - x[2] + 1 @test !isequal(expr, x[1]^2 + x[1] + 1) JuMP.drop_zeros!(expr) @test isequal(expr, x[1]^2 + x[1] + 1) end - + @testset "iszero(::GenericQuadExpr)" begin m = ModelType() @variable(m, x)