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

potential performance issue without move_factors_into_sums #224

Closed
blegat opened this issue Aug 31, 2023 · 1 comment · Fixed by #225
Closed

potential performance issue without move_factors_into_sums #224

blegat opened this issue Aug 31, 2023 · 1 comment · Fixed by #225

Comments

@blegat
Copy link
Member

blegat commented Aug 31, 2023

I thought move_factors_into_sums is about x + 2 * sum(...) to be rewritten as x + sum(2 * ...) but I didn't expect it to also have the following effect:

julia> MA.rewrite(:(x + 2x))
(Symbol("##341"), :(var"##341" = begin
          #= /home/blegat/.julia/packages/MutableArithmetics/Zy1nV/src/rewrite.jl:321 =#
          let
              #= /home/blegat/.julia/packages/MutableArithmetics/Zy1nV/src/rewrite.jl:322 =#
              begin
                  begin
                      var"##344" = MutableArithmetics.Zero()
                      var"##343" = (MutableArithmetics.operate!!)(MutableArithmetics.add_mul, var"##344", $(Expr(:escape, :x)))
                  end
                  begin
                      var"##342" = (MutableArithmetics.operate!!)(MutableArithmetics.add_mul, var"##343", $(Expr(:escape, 2)), $(Expr(:escape, :x)))
                  end
              end
              #= /home/blegat/.julia/packages/MutableArithmetics/Zy1nV/src/rewrite.jl:323 =#
              var"##342"
          end
      end))

julia> MA.rewrite(:(x + 2x), move_factors_into_sums = false)
(Symbol("##346"), :(var"##346" = begin
          #= /home/blegat/.julia/packages/MutableArithmetics/Zy1nV/src/rewrite.jl:321 =#
          let
              #= /home/blegat/.julia/packages/MutableArithmetics/Zy1nV/src/rewrite.jl:322 =#
              begin
                  #= /home/blegat/.julia/packages/MutableArithmetics/Zy1nV/src/rewrite.jl:317 =#
                  var"##347" = (MutableArithmetics.copy_if_mutable)($(Expr(:escape, :x)))
                  var"##348" = (MutableArithmetics.operate!!)(*, 2, $(Expr(:escape, :x)))
                  var"##349" = (MutableArithmetics.operate!!)(MutableArithmetics.add_mul, var"##347", var"##348")
              end
              #= /home/blegat/.julia/packages/MutableArithmetics/Zy1nV/src/rewrite.jl:323 =#
              var"##349"
          end
      end))

The line var"##348" = (MutableArithmetics.operate!!)(*, 2, $(Expr(:escape, :x))) creates a temporary expression containing the term 2x. This will have quite a bad effect on the performance of JuMP.AffExpr and JuMP.QuadExpr.

@odow
Copy link
Member

odow commented Aug 31, 2023

move_factors_into_sums was perhaps a bad name. It's really: don't mess with the expression graph.

This add_mul issue could be fixed though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants