Skip to content

[Utilities] fix performance of printing functions with many terms - #3107

Merged
odow merged 1 commit into
jump-dev:masterfrom
chriscoey:cc/print-linear-concat
Oct 18, 2026
Merged

odow merged 1 commit into
jump-dev:masterfrom
chriscoey:cc/print-linear-concat

Conversation

@chriscoey

Copy link
Copy Markdown
Contributor

_to_string for ScalarAffineFunction and ScalarQuadraticFunction appends each term with s *= ..., so printing a function with n terms is O(n^2). I ran into this printing a model with a large objective:

import MathOptInterface as MOI
model = MOI.Utilities.Model{Float64}()
x = MOI.add_variables(model, 100_000)
f = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(1.0, x), 0.0)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
@time sprint(print, model);

On master this allocates 69 GiB. With this PR it allocates about 0.1 GiB, and the printed output is unchanged.

@odow
odow merged commit d4f09a2 into jump-dev:master Oct 18, 2026
30 checks passed
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.

2 participants