Skip to content

Commit a822321

Browse files
authored
Update julia_interface.jl
1 parent e08bab3 commit a822321

File tree

1 file changed

+1
-9
lines changed

1 file changed

+1
-9
lines changed

src/julia_interface.jl

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ export cons_coord, cons_coord!, consjac
22

33
using NLPModels, SparseArrays
44

5-
# Type conversion helpers for Issue #392: Preallocate more vectors
65
"""
76
prepare_input!(workspace::Vector{T}, x::AbstractVector{S}) where {T, S}
87
@@ -14,7 +13,7 @@ with converted values if type conversion is required.
1413
"""
1514
@inline function prepare_input!(workspace::Vector{T}, x::AbstractVector{S}) where {T, S}
1615
if S === T && typeof(x) <: Vector{T}
17-
return x # No conversion needed
16+
return x
1817
else
1918
resize!(workspace, length(x))
2019
workspace .= x
@@ -77,7 +76,6 @@ end
7776
function NLPModels.grad!(nlp::CUTEstModel{T}, x::AbstractVector, g::AbstractVector) where {T}
7877
@lencheck nlp.meta.nvar x g
7978

80-
# Use type conversion helpers to avoid allocations (Issue #392)
8179
x_prepared = prepare_input!(nlp.input_workspace, x)
8280

8381
if typeof(g) <: Vector{T}
@@ -225,7 +223,6 @@ function cons_coord!(
225223
@lencheck nlp.meta.ncon c
226224
@lencheck nlp.meta.nnzj rows cols vals
227225

228-
# Resize workspace vectors on demand if needed (Issue #392 - double buffering)
229226
nnzj = nlp.meta.nnzj
230227
if length(nlp.jac_coord_rows) < nnzj
231228
resize!(nlp.jac_coord_rows, nnzj)
@@ -236,12 +233,10 @@ function cons_coord!(
236233
resize!(nlp.cons_vals, nlp.meta.ncon)
237234
end
238235

239-
# Use preallocated vectors instead of allocating new ones
240236
cons_coord!(nlp, Vector{T}(x), view(nlp.cons_vals, 1:nlp.meta.ncon),
241237
view(nlp.jac_coord_rows, 1:nnzj), view(nlp.jac_coord_cols, 1:nnzj),
242238
view(nlp.jac_coord_vals, 1:nnzj))
243239

244-
# Copy results to output vectors
245240
rows .= view(nlp.jac_coord_rows, 1:nnzj)
246241
cols .= view(nlp.jac_coord_cols, 1:nnzj)
247242
vals .= view(nlp.jac_coord_vals, 1:nnzj)
@@ -268,7 +263,6 @@ Usage:
268263
function cons_coord(nlp::CUTEstModel{T}, x::StrideOneVector{T}) where {T}
269264
@lencheck nlp.meta.nvar x
270265

271-
# Resize workspace vectors on demand if needed (Issue #392 - double buffering)
272266
nnzj = nlp.meta.nnzj
273267
if length(nlp.jac_coord_rows) < nnzj
274268
resize!(nlp.jac_coord_rows, nnzj)
@@ -279,12 +273,10 @@ function cons_coord(nlp::CUTEstModel{T}, x::StrideOneVector{T}) where {T}
279273
resize!(nlp.cons_vals, nlp.meta.ncon)
280274
end
281275

282-
# Use preallocated vectors to avoid allocations
283276
cons_coord!(nlp, x, view(nlp.cons_vals, 1:nlp.meta.ncon),
284277
view(nlp.jac_coord_rows, 1:nnzj), view(nlp.jac_coord_cols, 1:nnzj),
285278
view(nlp.jac_coord_vals, 1:nnzj))
286279

287-
# Return copies of the results to maintain API compatibility
288280
c = copy(view(nlp.cons_vals, 1:nlp.meta.ncon))
289281
rows = copy(view(nlp.jac_coord_rows, 1:nnzj))
290282
cols = copy(view(nlp.jac_coord_cols, 1:nnzj))

0 commit comments

Comments
 (0)