@@ -273,16 +273,19 @@ function MOI.eval_constraint_jacobian(evaluator::MOIOptimizationNLPEvaluator, j,
273
273
" automatically generate i with one of the autodiff backends." *
274
274
" If you are using the ModelingToolkit symbolic interface, pass the `cons_j` kwarg set to `true` in `OptimizationProblem`." )
275
275
end
276
- evaluator. f. cons_j (evaluator. J, x)
277
- if evaluator. J isa SparseMatrixCSC
278
- nnz = nonzeros (evaluator. J)
276
+ # Get and cache the Jacobian object here once. `evaluator.J` calls
277
+ # `getproperty`, which is expensive because it calls `fieldnames`.
278
+ J = evaluator. J
279
+ evaluator. f. cons_j (J, x)
280
+ if J isa SparseMatrixCSC
281
+ nnz = nonzeros (J)
279
282
@assert length (j) == length (nnz)
280
283
for (i, Ji) in zip (eachindex (j), nnz)
281
284
j[i] = Ji
282
285
end
283
286
else
284
287
for i in eachindex (j)
285
- j[i] = evaluator . J[i]
288
+ j[i] = J[i]
286
289
end
287
290
end
288
291
return
@@ -336,22 +339,25 @@ function MOI.eval_hessian_lagrangian(evaluator::MOIOptimizationNLPEvaluator{T},
336
339
" automatically generate it with one of the autodiff backends." *
337
340
" If you are using the ModelingToolkit symbolic interface, pass the `hess` kwarg set to `true` in `OptimizationProblem`." )
338
341
end
342
+ # Get and cache the Hessian object here once. `evaluator.H` calls
343
+ # `getproperty`, which is expensive because it calls `fieldnames`.
344
+ H = evaluator. H
339
345
fill! (h, zero (T))
340
346
k = 0
341
- evaluator. f. hess (evaluator . H, x)
342
- sparse_objective = evaluator . H isa SparseMatrixCSC
347
+ evaluator. f. hess (H, x)
348
+ sparse_objective = H isa SparseMatrixCSC
343
349
if sparse_objective
344
- rows, cols, _ = findnz (evaluator . H)
350
+ rows, cols, _ = findnz (H)
345
351
for (i, j) in zip (rows, cols)
346
352
if i <= j
347
353
k += 1
348
- h[k] = σ * evaluator . H[i, j]
354
+ h[k] = σ * H[i, j]
349
355
end
350
356
end
351
357
else
352
- for i in 1 : size (evaluator . H, 1 ), j in 1 : i
358
+ for i in 1 : size (H, 1 ), j in 1 : i
353
359
k += 1
354
- h[k] = σ * evaluator . H[i, j]
360
+ h[k] = σ * H[i, j]
355
361
end
356
362
end
357
363
# A count of the number of non-zeros in the objective Hessian is needed if
0 commit comments