From a89265a2582d617509011fb2eeffbeb131d2207d Mon Sep 17 00:00:00 2001 From: Aayush Sabharwal Date: Tue, 8 Apr 2025 17:21:38 +0530 Subject: [PATCH 1/2] fix: improve type-stability of `setsym_oop` --- src/state_indexing.jl | 4 ++++ src/value_provider_interface.jl | 31 +++++++++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/state_indexing.jl b/src/state_indexing.jl index fef8f50..dc381ea 100644 --- a/src/state_indexing.jl +++ b/src/state_indexing.jl @@ -464,6 +464,10 @@ for (t1, t2) in [ throw(NotVariableOrParameter("setsym_oop", s)) end end + if sym isa Tuple + vars = Tuple(vars) + pars = Tuple(pars) + end indp = _root_indp(indp) return FullSetter(isempty(vars) ? nothing : OOPSetter(indp, identity.(vars), true), isempty(pars) ? nothing : OOPSetter(indp, identity.(pars), false), diff --git a/src/value_provider_interface.jl b/src/value_provider_interface.jl index e082d54..8a17709 100644 --- a/src/value_provider_interface.jl +++ b/src/value_provider_interface.jl @@ -231,27 +231,34 @@ function (fn::Fix1Multiple)(args...) fn.f(fn.arg, args...) end -struct OOPSetter{I, D} +struct OOPSetter{S, I, D} indp::I idxs::D - is_state::Bool end -function (os::OOPSetter)(valp, val) - buffer = if os.is_state - hasmethod(state_values, Tuple{typeof(valp)}) ? state_values(valp) : valp - else - hasmethod(parameter_values, Tuple{typeof(valp)}) ? parameter_values(valp) : valp - end +OOPSetter(indp, idxs, isstate) = OOPSetter{isstate, typeof(indp), typeof(idxs)}(indp, idxs) + +function (os::OOPSetter{true})(valp, val) + buffer = hasmethod(state_values, Tuple{typeof(valp)}) ? state_values(valp) : valp + return remake_buffer(os.indp, buffer, (os.idxs,), (val,)) +end + +function (os::OOPSetter{false})(valp, val) + buffer = hasmethod(parameter_values, Tuple{typeof(valp)}) ? parameter_values(valp) : valp return remake_buffer(os.indp, buffer, (os.idxs,), (val,)) end -function (os::OOPSetter)(valp, val::Union{Tuple, AbstractArray}) - buffer = if os.is_state - hasmethod(state_values, Tuple{typeof(valp)}) ? state_values(valp) : valp +function (os::OOPSetter{true})(valp, val::Union{Tuple, AbstractArray}) + buffer = hasmethod(state_values, Tuple{typeof(valp)}) ? state_values(valp) : valp + if os.idxs isa Union{Tuple, AbstractArray} + return remake_buffer(os.indp, buffer, os.idxs, val) else - hasmethod(parameter_values, Tuple{typeof(valp)}) ? parameter_values(valp) : valp + return remake_buffer(os.indp, buffer, (os.idxs,), (val,)) end +end + +function (os::OOPSetter{false})(valp, val::Union{Tuple, AbstractArray}) + buffer = hasmethod(parameter_values, Tuple{typeof(valp)}) ? parameter_values(valp) : valp if os.idxs isa Union{Tuple, AbstractArray} return remake_buffer(os.indp, buffer, os.idxs, val) else From 82ff70f9ec6a6f9f2139c3b86585055a318ee135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Miclu=C8=9Ba-C=C3=A2mpeanu?= Date: Wed, 9 Apr 2025 14:22:54 +0300 Subject: [PATCH 2/2] refactor: use tuples instead of vectors for `ParameterIndex`es in `setsym_oop` --- src/batched_interface.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/batched_interface.jl b/src/batched_interface.jl index ad8273c..c518ecc 100644 --- a/src/batched_interface.jl +++ b/src/batched_interface.jl @@ -331,7 +331,7 @@ function setsym_oop(bi::BatchedInterface) push!(curexpr.args, :($paramssym = $parameter_values($prob))) else param_idxssym = Symbol(:param_idxs_, sys_i) - param_idxs = indp_idxs[.!isstate] + param_idxs = (indp_idxs[.!isstate]...,) param_valssym = Symbol(:param_vals, sys_i) vals_idxs = union_idxs[.!isstate] push!(curexpr.args, :($param_idxssym = $param_idxs))