Skip to content

Commit

Permalink
Improve performance of haskey lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Oct 31, 2024
1 parent 1a4f551 commit d63fe6f
Showing 1 changed file with 49 additions and 39 deletions.
88 changes: 49 additions & 39 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -731,10 +731,11 @@ function MOI.get(model::Optimizer, ::MOI.ListOfVariableIndices)
end

function _info(model::Optimizer, key::MOI.VariableIndex)
if haskey(model.variable_info, key)
return model.variable_info[key]
info = get(model.variable_info, key, nothing)
if info === nothing
return throw(MOI.InvalidIndex(key))
end
return throw(MOI.InvalidIndex(key))
return info
end

"""
Expand Down Expand Up @@ -795,14 +796,13 @@ function MOI.get(model::Optimizer, ::Type{MOI.VariableIndex}, name::String)
if model.name_to_variable === nothing
_rebuild_name_to_variable(model)
end
if haskey(model.name_to_variable, name)
variable = model.name_to_variable[name]
if variable === nothing
error("Duplicate name detected: $(name)")
end
return variable
variable = get(model.name_to_variable, name, missing)
if variable === nothing
error("Duplicate name detected: $(name)")
elseif variable === missing
return nothing # Variable with this name does not exist
end
return nothing
return variable::MOI.VariableIndex
end

function MOI.get(model::Optimizer, ::MOI.VariableName, v::MOI.VariableIndex)
Expand Down Expand Up @@ -1172,10 +1172,11 @@ function _info(
c::MOI.ConstraintIndex{MOI.VariableIndex,<:Any},
)
var_index = MOI.VariableIndex(c.value)
if haskey(model.variable_info, var_index)
return _info(model, var_index)
info = get(model.variable_info, var_index, nothing)
if info === nothing
return throw(MOI.InvalidIndex(c))
end
return throw(MOI.InvalidIndex(c))
return info
end

"""
Expand All @@ -1197,40 +1198,46 @@ function MOI.is_valid(
model::Optimizer,
c::MOI.ConstraintIndex{MOI.VariableIndex,MOI.LessThan{Float64}},
)
if haskey(model.variable_info, MOI.VariableIndex(c.value))
info = _info(model, c)
return info.bound == _BOUND_LESS_THAN ||
info.bound == _BOUND_LESS_AND_GREATER_THAN
info = get(model.variable_info, MOI.VariableIndex(c.value), nothing)
if info === nothing
return false
end
return false
return info.bound == _BOUND_LESS_THAN ||
info.bound == _BOUND_LESS_AND_GREATER_THAN
end

function MOI.is_valid(
model::Optimizer,
c::MOI.ConstraintIndex{MOI.VariableIndex,MOI.GreaterThan{Float64}},
)
if haskey(model.variable_info, MOI.VariableIndex(c.value))
info = _info(model, c)
return info.bound == _BOUND_GREATER_THAN ||
info.bound == _BOUND_LESS_AND_GREATER_THAN
info = get(model.variable_info, MOI.VariableIndex(c.value), nothing)
if info === nothing
return false
end
return false
return info.bound == _BOUND_GREATER_THAN ||
info.bound == _BOUND_LESS_AND_GREATER_THAN
end

function MOI.is_valid(
model::Optimizer,
c::MOI.ConstraintIndex{MOI.VariableIndex,MOI.Interval{Float64}},
)
return haskey(model.variable_info, MOI.VariableIndex(c.value)) &&
_info(model, c).bound == _BOUND_INTERVAL
info = get(model.variable_info, MOI.VariableIndex(c.value), nothing)
if info === nothing
return false

Check warning on line 1227 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L1227

Added line #L1227 was not covered by tests
end
return info.bound == _BOUND_INTERVAL
end

function MOI.is_valid(
model::Optimizer,
c::MOI.ConstraintIndex{MOI.VariableIndex,MOI.EqualTo{Float64}},
)
return haskey(model.variable_info, MOI.VariableIndex(c.value)) &&
_info(model, c).bound == _BOUND_EQUAL_TO
info = get(model.variable_info, MOI.VariableIndex(c.value), nothing)
if info === nothing
return false

Check warning on line 1238 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L1238

Added line #L1238 was not covered by tests
end
return info.bound == _BOUND_EQUAL_TO
end

function MOI.get(
Expand Down Expand Up @@ -1495,10 +1502,11 @@ function _info(
c::MOI.ConstraintIndex{MOI.ScalarAffineFunction{Float64},<:_SCALAR_SETS},
)
key = _ConstraintKey(c.value)
if haskey(model.affine_constraint_info, key)
return model.affine_constraint_info[key]
info = get(model.affine_constraint_info, key, nothing)
if info === nothing
return throw(MOI.InvalidIndex(c))
end
return throw(MOI.InvalidIndex(c))
return info
end

function row(
Expand Down Expand Up @@ -1742,14 +1750,13 @@ function MOI.get(model::Optimizer, ::Type{MOI.ConstraintIndex}, name::String)
if model.name_to_constraint_index === nothing
_rebuild_name_to_constraint_index(model)
end
if haskey(model.name_to_constraint_index, name)
constr = model.name_to_constraint_index[name]
if constr === nothing
error("Duplicate constraint name detected: $(name)")
end
return constr
constraint = get(model.name_to_constraint_index, name, missing)
if constraint === nothing
error("Duplicate constraint name detected: $(name)")
elseif constraint === missing
return nothing # No constraint exists with this name
end
return nothing
return constraint
end

function MOI.get(
Expand Down Expand Up @@ -2545,8 +2552,11 @@ function MOI.is_valid(
model::Optimizer,
c::MOI.ConstraintIndex{MOI.VariableIndex,S},
) where {S<:Union{MOI.Semicontinuous{Float64},MOI.Semiinteger{Float64}}}
return haskey(model.variable_info, MOI.VariableIndex(c.value)) &&
_info(model, c).bound == _bound_type(S)
info = get(model.variable_info, MOI.VariableIndex(c.value), nothing)
if info === nothing
return false

Check warning on line 2557 in src/MOI_wrapper.jl

View check run for this annotation

Codecov / codecov/patch

src/MOI_wrapper.jl#L2557

Added line #L2557 was not covered by tests
end
return info.bound == _bound_type(S)
end

function MOI.add_constraint(
Expand Down

0 comments on commit d63fe6f

Please sign in to comment.