Skip to content

Commit 33952c8

Browse files
committed
Avoid extra copies of functions in copy_to
1 parent c574485 commit 33952c8

4 files changed

Lines changed: 46 additions & 5 deletions

File tree

src/Utilities/copy.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ function pass_attributes(dest::MOI.ModelLike, src::MOI.ModelLike, index_map)
4040
return
4141
end
4242

43+
function _pass_attribute(
44+
dest::MOI.ModelLike,
45+
src::MOI.ModelLike,
46+
index_map,
47+
::MOI.ConstraintFunction{F},
48+
) where {F}
49+
return _pass_attribute(
50+
dest,
51+
src,
52+
index_map,
53+
UnsafeConstraintFunction{F}(),
54+
)
55+
end
56+
4357
function _pass_attribute(
4458
dest::MOI.ModelLike,
4559
src::MOI.ModelLike,
@@ -175,7 +189,10 @@ function _copy_constraints(
175189
cis_src::Vector{<:MOI.ConstraintIndex},
176190
)
177191
for ci in cis_src
178-
f = MOI.get(src, MOI.ConstraintFunction(), ci)
192+
# MOI does not allows the implementation of `MOI.constraint`
193+
# to modify it or hold a copy of it.
194+
# `map_indices` is also often going to copy it anyway
195+
f = MOI.get(src, UnsafeConstraintFunction(), ci)
179196
s = MOI.get(src, MOI.ConstraintSet(), ci)
180197
index_map_FS[ci] =
181198
MOI.add_constraint(dest, map_indices(index_map, f), s)

src/Utilities/model.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ end
402402

403403
function MOI.get(
404404
model::AbstractModel,
405-
attr::Union{MOI.ConstraintFunction,MOI.ConstraintSet},
405+
attr::Union{UnsafeConstraintFunction,MOI.ConstraintFunction,MOI.ConstraintSet},
406406
ci::MOI.ConstraintIndex,
407407
)
408408
return MOI.get(constraints(model, ci), attr, ci)
@@ -530,7 +530,7 @@ function MOI.supports(model::AbstractModel, attr::MOI.ObjectiveFunction)
530530
return MOI.supports(model.objective, attr)
531531
end
532532

533-
function MOI.get(model::AbstractModel, attr::MOI.ObjectiveFunction)
533+
function MOI.get(model::AbstractModel, attr::Union{UnsafeObjectiveFunction,MOI.ObjectiveFunction})
534534
return MOI.get(model.objective, attr)
535535
end
536536

src/Utilities/objective_container.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ function MOI.supports(
116116
return true
117117
end
118118

119+
struct UnsafeObjectiveFunction{F<:MOI.AbstractFunction} end
120+
121+
function MOI.get(model::MOI.ModelLike, ::UnsafeObjectiveFunction{F}) where {F}
122+
return MOI.get(model, MOI.ObjectiveFunction{F}())
123+
end
124+
119125
function MOI.get(
120126
o::ObjectiveContainer{T},
121127
attr::MOI.ObjectiveFunction{F},
@@ -153,6 +159,10 @@ function MOI.get(
153159
return convert(F, zero(MOI.ScalarAffineFunction{T}))
154160
end
155161

162+
function MOI.get(o::ObjectiveContainer, ::MOI.ObjectiveFunction{F}) where {F}
163+
return copy(MOI.get(o, UnsafeObjectiveFunction{F}()))
164+
end
165+
156166
function _empty_keeping_sense(o::ObjectiveContainer)
157167
sense, is_sense_set = o.sense, o.is_sense_set
158168
MOI.empty!(o)

src/Utilities/vector_of_constraints.jl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,28 @@ function MOI.delete(
9696
return
9797
end
9898

99+
struct UnsafeConstraintFunction end
100+
101+
function MOI.get(model::MOI.ModelLike, ::UnsafeConstraintFunction, ci::MOI.ConstraintIndex)
102+
return MOI.get(model, MOI.ConstraintFunction, ci)
103+
end
104+
99105
function MOI.get(
100106
v::VectorOfConstraints{F,S},
101-
::MOI.ConstraintFunction,
107+
::UnsafeConstraintFunction,
102108
ci::MOI.ConstraintIndex{F,S},
103109
) where {F,S}
104110
MOI.throw_if_not_valid(v, ci)
105111
f, _ = v.constraints[ci]::Tuple{F,S}
106-
return copy(f)
112+
return f
113+
end
114+
115+
function MOI.get(
116+
v::VectorOfConstraints{F,S},
117+
::MOI.ConstraintFunction,
118+
ci::MOI.ConstraintIndex{F,S},
119+
) where {F,S}
120+
return copy(MOI.get(v, MOI.UnsafeConstraintFunction, ci))
107121
end
108122

109123
function MOI.get(

0 commit comments

Comments
 (0)