Skip to content

[Utilities] compute start columns once when sorting in default_copy_to - #3161

Open
chriscoey wants to merge 1 commit into
jump-dev:masterfrom
chriscoey:cc/copy-sort-start-columns
Open

chriscoey wants to merge 1 commit into
jump-dev:masterfrom
chriscoey:cc/copy-sort-start-columns

Conversation

@chriscoey

@chriscoey chriscoey commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

_copy_variables_with_set sorts the constrained variables by their start column, and the by function looks up variable_to_column on every comparison. I changed it to compute the start columns once and sort by them. Each variable is in at most one entry of variable_cones, so the start columns are distinct, and the order and the result of copy_to are unchanged.

The variables are collected one set type at a time, so the sort has the most work to do when there is more than one set type:

import MathOptInterface as MOI
src = MOI.Utilities.Model{Float64}()
x = MOI.add_variables(src, 2_000_000)
for (i, xi) in enumerate(x)
    MOI.add_constraint(src, xi, isodd(i) ? MOI.ZeroOne() : MOI.GreaterThan(0.0))
end
MOI.copy_to(MOI.Utilities.Model{Float64}(), src);  # compile
@time MOI.copy_to(MOI.Utilities.Model{Float64}(), src);

On Julia 1.13.4 the timed call takes 4.7 s and allocates 3.26 GiB on master, and 1.1 s and 1.0 GiB with this PR. Master's isolated sort! allocates about 2.3 GiB on Julia 1.12.7 and 1.13.4, and about 15 MiB on 1.11.9.

The sort of constrained variables looked up the start column in a Dict
on every comparison. Compute the start columns once and sort by them
instead.
@blegat

blegat commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Analysis by Opus below, it could reproduce the drop of 2 GB of allocation you get, where does it come from ?

Why each call is slow

Each start_column call is cache.variable_to_column[first(x[1])], which does three things:

  • Hashes a VariableIndex and looks it up in a Dict with 2M entries. The table is far bigger than the CPU cache, and the sort asks for keys in an unpredictable order, so nearly every lookup is a cache miss.
  • Follows the pointer to the element. variable_cones is a Vector{Tuple{Vector{VariableIndex},Any}}. Because of the Any, the tuples are not stored inline: each element is a pointer to a tuple on the heap.
  • Follows a second pointer into the inner Vector to read its first entry.

So each comparison touches up to 6 random memory locations. With starts::Vector{Int}, a comparison reads two Ints that sit next to each other in one array.

Measurements (2M elements)

Variant Time Allocations
sort!(v; by = x -> dict[first(x[1])]) (master) 5.08 s 15 MiB
105M Dict lookups alone, no sort 4.49 s 0
sort!(v; by = x -> x[2].value) (no Dict, still boxed elements) 1.24 s* 22 MiB
precompute starts, then sortperm (PR) 0.31 s 46 MiB

* includes some compilation time.

@chriscoey

Copy link
Copy Markdown
Contributor Author

It depends on the Julia version. On 1.12.7 and 1.13.4, master's sort!(cache.variable_cones; by = start_column) alone allocates 2.3 GiB on this example, and the whole copy_to allocates 3.26 GiB. On 1.11.9 the same sort allocates 15 MiB, as in your table, and copy_to 0.87 GiB. The numbers in the description match 1.12 and 1.13, so I have updated it. The starts and sortperm in this PR allocate about 47 MiB on all three versions.

Master's sort allocation reproduces without MOI. On 1.13.4, sort!(v; by = x -> d[first(x[1])]) over 2M elements, with d::Dict{Int,Int}, allocates 849 MiB when v is a Vector{Tuple{Vector{Int},Any}} and 30.5 MiB when it is a Vector{Tuple{Vector{Int},Int}}. On 1.11.9 the first case allocates 15.3 MiB. Without the Dict lookup in by, the first case allocates 15.3 MiB on 1.13.4 too.

This branch has not been deployed

No deployments
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