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

Affine expressions are slow #2628

Closed
odow opened this issue Jun 16, 2021 · 1 comment
Closed

Affine expressions are slow #2628

odow opened this issue Jun 16, 2021 · 1 comment

Comments

@odow
Copy link
Member

odow commented Jun 16, 2021

@jd-lara has a problem where creating affine expressions is a bottleneck.

There are two main causes.

This double-hash in _add_or_set!

JuMP.jl/src/aff_expr.jl

Lines 18 to 28 in 2ff880d

# 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/jump-dev/JuMP.jl/issues/1946.
if iszero(v)
return dict # No-op.
end
# TODO: This unnecessarily requires two lookups for k.
dict[k] = get!(dict, k, zero(V)) + v
return dict
end

This size hint:

JuMP.jl/src/aff_expr.jl

Lines 390 to 401 in 2ff880d

function add_to_expression!(
aff::GenericAffExpr{C,V},
coef::_Constant,
other::GenericAffExpr{C,V},
) where {C,V}
sizehint!(aff, length(linear_terms(aff)) + length(linear_terms(other)))
for (term_coef, var) in linear_terms(other)
_add_or_set!(aff.terms, var, coef * term_coef)
end
aff.constant += coef * other.constant
return aff
end

In particular, the size hint is pessimistic in that it suggests reserving enough space for the union of the elements assuming that there are no duplicates.

@odow
Copy link
Member Author

odow commented Jun 19, 2021

Closing because this is not a JuMP problem.

The real issue is that there is something to do with the affine expressions inside ParameterJuMP that are causing a large slow-down: JuliaStochOpt/ParameterJuMP.jl#85

@odow odow closed this as completed Jun 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

1 participant