Skip to content

Commit f2eb661

Browse files
authored
Fix various performance issues (#1821)
1 parent 5fa7222 commit f2eb661

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

src/Utilities/DoubleDicts.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ mutable struct IndexDoubleDictInner{F,S} <:
9494
AbstractDoubleDictInner{F,S,MOI.ConstraintIndex{F,S}}
9595
dict::Dict{Int64,Int64}
9696
function IndexDoubleDictInner{F,S}(d::IndexDoubleDict) where {F,S}
97-
return new{F,S}(get!(d.dict, (F, S), Dict{Int64,Int64}()))
97+
if !haskey(d.dict, (F, S))
98+
d.dict[(F, S)] = Dict{Int64,Int64}()
99+
end
100+
return new{F,S}(d.dict[(F, S)])
98101
end
99102
end
100103

src/Utilities/copy.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,17 +242,28 @@ Copy the constraints `cis_src` from the model `src` to the model `dest` and fill
242242
function _copy_constraints(
243243
dest::MOI.ModelLike,
244244
src::MOI.ModelLike,
245-
index_map::IndexMap,
245+
index_map,
246+
index_map_FS,
246247
cis_src::Vector{<:MOI.ConstraintIndex},
247248
)
248249
for ci in cis_src
249250
f = MOI.get(src, MOI.ConstraintFunction(), ci)
250251
s = MOI.get(src, MOI.ConstraintSet(), ci)
251-
index_map[ci] = MOI.add_constraint(dest, map_indices(index_map, f), s)
252+
index_map_FS[ci] =
253+
MOI.add_constraint(dest, map_indices(index_map, f), s)
252254
end
253255
return
254256
end
255257

258+
function _copy_constraints(
259+
dest::MOI.ModelLike,
260+
src::MOI.ModelLike,
261+
index_map,
262+
cis_src::Vector{MOI.ConstraintIndex{F,S}},
263+
) where {F,S}
264+
return _copy_constraints(dest, src, index_map, index_map[F, S], cis_src)
265+
end
266+
256267
function pass_nonvariable_constraints_fallback(
257268
dest::MOI.ModelLike,
258269
src::MOI.ModelLike,

src/Utilities/copy/index_map.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ function Base.getindex(map::IndexMap, key::MOI.ConstraintIndex{F,S}) where {F,S}
6767
return map.con_map[key]::MOI.ConstraintIndex{F,S}
6868
end
6969

70+
function Base.getindex(map::IndexMap, ::Type{F}, ::Type{S}) where {F,S}
71+
return map.con_map[F, S]
72+
end
73+
7074
function Base.setindex!(
7175
map::IndexMap,
7276
value::MOI.VariableIndex,

src/Utilities/functions.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,22 +184,25 @@ end
184184
# Functions
185185

186186
function map_indices(index_map::F, f::MOI.VectorOfVariables) where {F<:Function}
187-
return MOI.VectorOfVariables(index_map.(f.variables))
187+
return MOI.VectorOfVariables([index_map(x) for x in f.variables])
188188
end
189189

190190
function map_indices(
191191
index_map::F,
192192
f::Union{MOI.ScalarAffineFunction,MOI.VectorAffineFunction},
193193
) where {F<:Function}
194-
return typeof(f)(map_indices.(index_map, f.terms), MOI.constant(f))
194+
return typeof(f)(
195+
[map_indices(index_map, t) for t in f.terms],
196+
MOI.constant(f),
197+
)
195198
end
196199

197200
function map_indices(
198201
index_map::F,
199202
f::Union{MOI.ScalarQuadraticFunction,MOI.VectorQuadraticFunction},
200203
) where {F<:Function}
201-
affine_terms = map_indices.(index_map, f.affine_terms)
202-
quadratic_terms = map_indices.(index_map, f.quadratic_terms)
204+
affine_terms = [map_indices(index_map, t) for t in f.affine_terms]
205+
quadratic_terms = [map_indices(index_map, t) for t in f.quadratic_terms]
203206
return typeof(f)(quadratic_terms, affine_terms, MOI.constant(f))
204207
end
205208

0 commit comments

Comments
 (0)