Skip to content

Document cost of named variables and constraints in Performance Tips #2973

Description

@nickrobinson251

When building a model with millions of variables and constraints, I see model build time roughly half when switching to using anonymous variables and constraints.

An example model has size

Variables: 2574376 
Objective function type: AffExpr
`AffExpr`-in-`EqualTo{Float64}`: 1097232 constraints
`AffExpr`-in-`GreaterThan{Float64}`: 315984 constraints
`AffExpr`-in-`LessThan{Float64}`: 1445558 constraints
`VariableRef`-in-`GreaterThan{Float64}`: 1721368 constraints
`VariableRef`-in-`LessThan{Float64}`: 40032 constraints
`VariableRef`-in-`ZeroOne`: 40032 constraints 

And an example code change would be:

-        @variable(model, foo[z in zones, t in times] >= 0)
+        model[:foo] = foo = @variable(model, [z in zones, t in times], lower_bound=0)

Changing all @variable and @constraint calls similarly, I see differences in model build time like below:

time allocs mem
names 36.214 s 131.82 M 7.19 GiB
no names 16.652 s 67.09 M 4.05 GiB
relative 2.175x 1.965x 1.775x

Depending on the model we're building, the time speed up varies (~1.3 - 2x) but the reduction of allocations is pretty consistently about a ~2x reduction.

The different types of "names" and the behaviour of the macros regarding names is documented

So actually making this change was fairly easy. And this discovery is a very welcome performance improvement!

However, i don't think this significant performance consideration is mentioned anywhere in the docs. And in particular it does't seem to be mentioned in the Performance Tips

I think the overhead of named variables/constraints is known (xref #2817 (comment)), so I think worth documenting given it can be a significant build-time cost and is avoidable with relatively small user code changes.

(p.s. i'm opening this just before some time off so may not be quick to respond)

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions