Skip to content

Fix performance of flatten! for large summations - #4264

Merged
odow merged 1 commit into
masterfrom
od/fix-n2-flatten
Oct 4, 2026
Merged

odow merged 1 commit into
masterfrom
od/fix-n2-flatten

Conversation

@odow

@odow odow commented Oct 2, 2026

Copy link
Copy Markdown
Member

When fixing #4196 I didn't do a good job and introduced a pretty bad performance
regression for problems with large summations.

Script

using JuMP
function main(N)
    model = Model()
    @variable(model, x[i in 1:N])
    @expression(model, sum(100 * (x[i+1] - x[i]^2)^2 + (1 - x[i])^2 for i in 1:N-1))
end

v1.30.0

julia> @time main(10^5);
  0.430789 seconds (10.40 M allocations: 483.358 MiB, 67.91% gc time, 0.01% compilation time)

julia> @time main(10^5);
  0.426143 seconds (10.40 M allocations: 484.103 MiB, 63.67% gc time)

master

julia> @time main(10^5);
 12.954410 seconds (9.60 M allocations: 455.889 MiB)

julia> @time main(10^5);
 12.973724 seconds (9.60 M allocations: 457.638 MiB)

This PR

julia> @time main(10^5);
  0.208661 seconds (9.40 M allocations: 455.111 MiB)

julia> @time main(10^5);
  0.193844 seconds (9.40 M allocations: 458.643 MiB)

@codecov

codecov Bot commented Oct 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.96%. Comparing base (55744bb) to head (d98f847).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4264      +/-   ##
==========================================
+ Coverage   99.95%   99.96%   +0.01%     
==========================================
  Files          43       43              
  Lines        6341     6332       -9     
==========================================
- Hits         6338     6330       -8     
+ Misses          3        2       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@odow
odow merged commit 00f9391 into master Oct 4, 2026
11 of 12 checks passed
@odow
odow deleted the od/fix-n2-flatten branch October 4, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant