Skip to content

Fix precompilation of MOI.delete(model, x) - #3126

Merged
odow merged 1 commit into
masterfrom
od/precompile-delete
Oct 29, 2026
Merged

odow merged 1 commit into
masterfrom
od/precompile-delete

Conversation

@odow

@odow odow commented Oct 29, 2026

Copy link
Copy Markdown
Member

Closes #3124
Closes #3125

In a fresh session:

julia> import MathOptInterface as MOI

julia> @time begin
           model = MOI.Utilities.Model{Float64}()
           @time MOI.empty!(model)
           x = MOI.add_variable(model)
           @time MOI.delete(model, x)
       end
  0.000012 seconds (49 allocations: 2.250 KiB)
  0.000013 seconds (15 allocations: 736 bytes)
  0.002748 seconds (338 allocations: 17.609 KiB, 89.91% compilation time)

@odow

odow commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

Before

julia> import MathOptInterface as MOI

julia> @time begin
           model = MOI.Utilities.Model{Float64}()
           x = MOI.add_variable(model)
           for i in 1:3
               MOI.add_constraint(model, 1.0 * x, MOI.GreaterThan(sin(i)))
           end
           @time MOI.delete(model, x)
       end
 14.950712 seconds (45.77 M allocations: 2.307 GiB, 2.46% gc time, 100.00% compilation time)
 14.959136 seconds (45.77 M allocations: 2.307 GiB, 2.46% gc time, 100.00% compilation time)

julia> @time begin
           model = MOI.Utilities.Model{Float64}()
           x = MOI.add_variable(model)
           for i in 1:3
               MOI.add_constraint(model, 1.0 * x, MOI.GreaterThan(sin(i)))
           end
           @time MOI.delete(model, x)
       end
  0.000006 seconds (19 allocations: 832 bytes)
  0.000119 seconds (134 allocations: 6.594 KiB)

After

julia> import MathOptInterface as MOI

julia> @time begin
           model = MOI.Utilities.Model{Float64}()
           x = MOI.add_variable(model)
           for i in 1:3
               MOI.add_constraint(model, 1.0 * x, MOI.GreaterThan(sin(i)))
           end
           @time MOI.delete(model, x)
       end
  0.020555 seconds (78.89 k allocations: 4.176 MiB, 99.71% compilation time)
  0.028824 seconds (80.25 k allocations: 4.240 MiB, 98.74% compilation time)

julia> @time begin
           model = MOI.Utilities.Model{Float64}()
           x = MOI.add_variable(model)
           for i in 1:3
               MOI.add_constraint(model, 1.0 * x, MOI.GreaterThan(sin(i)))
           end
           @time MOI.delete(model, x)
       end
  0.000015 seconds (25 allocations: 960 bytes)
  0.000130 seconds (140 allocations: 6.719 KiB)

@odow

odow commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

@blegat's #3125 is

julia> import MathOptInterface as MOI

julia> @time begin
           model = MOI.Utilities.Model{Float64}()
           x = MOI.add_variable(model)
           for i in 1:3
               MOI.add_constraint(model, 1.0 * x, MOI.GreaterThan(sin(i)))
           end
           @time MOI.delete(model, x)
       end
  0.068279 seconds (154.22 k allocations: 7.852 MiB, 99.85% compilation time)
  0.076540 seconds (155.57 k allocations: 7.916 MiB, 99.48% compilation time)

julia> @time begin
           model = MOI.Utilities.Model{Float64}()
           x = MOI.add_variable(model)
           for i in 1:3
               MOI.add_constraint(model, 1.0 * x, MOI.GreaterThan(sin(i)))
           end
           @time MOI.delete(model, x)
       end
  0.000012 seconds (28 allocations: 992 bytes)
  0.000112 seconds (143 allocations: 6.750 KiB)

So this PR is a clear win over that.

@odow
odow merged commit 17de700 into master Oct 29, 2026
31 checks passed
@odow
odow deleted the od/precompile-delete branch October 29, 2026 21:14
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.

Deleting variables takes longer on Julia 1.13

1 participant