Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allocation getting CanonicalConstraintFunction #1353

Closed
blegat opened this issue May 15, 2021 · 2 comments
Closed

Allocation getting CanonicalConstraintFunction #1353

blegat opened this issue May 15, 2021 · 2 comments

Comments

@blegat
Copy link
Member

blegat commented May 15, 2021

using MathOptInterface; const MOI = MathOptInterface
function f()
    model = MOI.Utilities.Model{Float64}()
    x = MOI.add_variable(model)
    c = MOI.add_constraint(model, 1.0 * MOI.SingleVariable(x), MOI.EqualTo(1.0))
    @time MOI.get(model, MOI.CanonicalConstraintFunction(), c)
end
f()

gives

  0.000001 seconds (1 allocation: 16 bytes)

I don't understand where this allocation comes from. It was allocation-free after #1318. This must be due to a recent change.

@odow
Copy link
Member

odow commented Aug 3, 2021

I took a look and couldn't find the reason.

It seems like it might just be a module scoping thing in Julia?

julia> function foo(model, ::MOI.CanonicalConstraintFunction, c)
           func = MOI.get(model, MOI.ConstraintFunction(), c)
           if MOI.Utilities.is_canonical(func)
               return func
           else
               return MOI.Utilities.canonical(func)
           end
       end
foo (generic function with 2 methods)

julia> 

julia> @btime foo(model, MOI.CanonicalConstraintFunction(), c)
  65.235 ns (0 allocations: 0 bytes)
MathOptInterface.ScalarAffineFunction{Float64}(MathOptInterface.ScalarAffineTerm{Float64}[MathOptInterface.ScalarAffineTerm{Float64}(1.0, MathOptInterface.VariableIndex(1))], 0.0)

julia> @btime MOI.get_fallback(model, MOI.CanonicalConstraintFunction(), c)
  81.273 ns (1 allocation: 16 bytes)
MathOptInterface.ScalarAffineFunction{Float64}(MathOptInterface.ScalarAffineTerm{Float64}[MathOptInterface.ScalarAffineTerm{Float64}(1.0, MathOptInterface.VariableIndex(1))], 0.0)

julia> @code_warntype MOI.get_fallback(model, MOI.CanonicalConstraintFunction(), c)
Variables
  #self#::Core.Const(MathOptInterface.get_fallback)
  model::MathOptInterface.Utilities.GenericModel{Float64, MathOptInterface.Utilities.ModelFunctionConstraints{Float64}}
  #unused#::Core.Const(MathOptInterface.CanonicalConstraintFunction())
  ci::MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.EqualTo{Float64}}
  func::MathOptInterface.ScalarAffineFunction{Float64}

Body::MathOptInterface.ScalarAffineFunction{Float64}
1%1 = MathOptInterface.ConstraintFunction()::Core.Const(MathOptInterface.ConstraintFunction())
│        (func = MathOptInterface.get(model, %1, ci))
│   %3 = Base.getproperty(MathOptInterface.Utilities, :is_canonical)::Core.Const(MathOptInterface.Utilities.is_canonical)
│   %4 = (%3)(func)::Bool
└──      goto #3 if not %4
2return func
3%7 = Base.getproperty(MathOptInterface.Utilities, :canonical)::Core.Const(MathOptInterface.Utilities.canonical)
│   %8 = (%7)(func)::MathOptInterface.ScalarAffineFunction{Float64}
└──      return %8

julia> @code_warntype foo(model, MOI.CanonicalConstraintFunction(), c)
Variables
  #self#::Core.Const(foo)
  model::MathOptInterface.Utilities.GenericModel{Float64, MathOptInterface.Utilities.ModelFunctionConstraints{Float64}}
  #unused#::Core.Const(MathOptInterface.CanonicalConstraintFunction())
  c::MathOptInterface.ConstraintIndex{MathOptInterface.ScalarAffineFunction{Float64}, MathOptInterface.EqualTo{Float64}}
  func::MathOptInterface.ScalarAffineFunction{Float64}

Body::MathOptInterface.ScalarAffineFunction{Float64}
1%1  = MathOptInterface.get::Core.Const(MathOptInterface.get)
│   %2  = MathOptInterface.ConstraintFunction::Core.Const(MathOptInterface.ConstraintFunction)
│   %3  = (%2)()::Core.Const(MathOptInterface.ConstraintFunction())
│         (func = (%1)(model, %3, c))
│   %5  = MathOptInterface.Utilities::Core.Const(MathOptInterface.Utilities)
│   %6  = Base.getproperty(%5, :is_canonical)::Core.Const(MathOptInterface.Utilities.is_canonical)
│   %7  = (%6)(func)::Bool
└──       goto #3 if not %7
2return func
3%10 = MathOptInterface.Utilities::Core.Const(MathOptInterface.Utilities)
│   %11 = Base.getproperty(%10, :canonical)::Core.Const(MathOptInterface.Utilities.canonical)
│   %12 = (%11)(func)::MathOptInterface.ScalarAffineFunction{Float64}
└──       return %12

@odow
Copy link
Member

odow commented Aug 9, 2021

julia> import MathOptInterface; const MOI = MathOptInterface
MathOptInterface

julia> using BenchmarkTools

julia> function foo(model, ::MOI.CanonicalConstraintFunction, c)
           func = MOI.get(model, MOI.ConstraintFunction(), c)
           if MOI.Utilities.is_canonical(func)
               return func
           else
               return MOI.Utilities.canonical(func)
           end
       end
foo (generic function with 1 method)

julia> function run_foo()
           model = MOI.Utilities.Model{Float64}()
           x = MOI.add_variable(model)
           c = MOI.add_constraint(model, 1.0 * MOI.SingleVariable(x), MOI.EqualTo(1.0))
           @btime foo($model, MOI.CanonicalConstraintFunction(), $c)
       end
run_foo (generic function with 1 method)

julia> function run_moi()
           model = MOI.Utilities.Model{Float64}()
           x = MOI.add_variable(model)
           c = MOI.add_constraint(model, 1.0 * MOI.SingleVariable(x), MOI.EqualTo(1.0))
           @btime MOI.get_fallback($model, MOI.CanonicalConstraintFunction(), $c)
       end
run_moi (generic function with 1 method)

julia> run_foo()
  48.383 ns (0 allocations: 0 bytes)
MathOptInterface.ScalarAffineFunction{Float64}(MathOptInterface.ScalarAffineTerm{Float64}[MathOptInterface.ScalarAffineTerm{Float64}(1.0, MathOptInterface.VariableIndex(1))], 0.0)

julia> run_moi()
  48.004 ns (0 allocations: 0 bytes)
MathOptInterface.ScalarAffineFunction{Float64}(MathOptInterface.ScalarAffineTerm{Float64}[MathOptInterface.ScalarAffineTerm{Float64}(1.0, MathOptInterface.VariableIndex(1))], 0.0)

I think this was just an artifact of how we were benchmarking it.

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

No branches or pull requests

2 participants