@@ -554,8 +554,9 @@ moi_function(x::Number) = x
554554
555555function moi_function (f:: GenericNonlinearExpr{V} ) where {V}
556556 model = owner_model (f)
557- if haskey (model. subexpressions, f)
558- return model. subexpressions[f]
557+ cache = isnothing (model) ? nothing : model. subexpressions
558+ if ! isnothing (cache) && haskey (cache, f)
559+ return cache[f]
559560 end
560561 ret = MOI. ScalarNonlinearFunction (f. head, similar (f. args))
561562 stack = Tuple{MOI. ScalarNonlinearFunction,Int,GenericNonlinearExpr{V}}[]
@@ -568,8 +569,8 @@ function moi_function(f::GenericNonlinearExpr{V}) where {V}
568569 end
569570 while ! isempty (stack)
570571 parent, i, arg = pop! (stack)
571- if haskey (model . subexpressions , arg)
572- parent. args[i] = model . subexpressions [arg]
572+ if ! isnothing (cache) && haskey (cache , arg)
573+ parent. args[i] = cache [arg]
573574 continue
574575 end
575576 child = MOI. ScalarNonlinearFunction (arg. head, similar (arg. args))
@@ -581,9 +582,13 @@ function moi_function(f::GenericNonlinearExpr{V}) where {V}
581582 child. args[j] = moi_function (arg. args[j])
582583 end
583584 end
584- model. subexpressions[arg] = child
585+ if ! isnothing (cache)
586+ cache[arg] = child
587+ end
588+ end
589+ if ! isnothing (cache)
590+ cache[f] = ret
585591 end
586- model. subexpressions[f] = ret
587592 return ret
588593end
589594
0 commit comments