Function calls pass by constant reference in Stan. Stan prevents assignment to function arguments to simplify reasoning about programs.
Does every value of the parameters meeting the constraints have a positive density? If so, you may still be running into arithmetic issues. Usually reducing the scale of the random inits solves this problem if the model’s reasonably formulated. Fat interval constraints often break things as initialization is in the middle of the interval. So if you have regression coefficients and declare
<lower=0, upper=1e6>, then initialization happens around 5e5.
Stan’s derivatives are pretty efficient, but you can always do better coding them by hand.
Loops aren’t slow in Stan per se, it’s that anything that adds to the autodiff stack is expensive. The way to speed things up is to write C++ functions with analytic derivatives for the components you need. But that’s a lot of work and exactly what we wrote Stan to spare users from having to do. Often we’ll wind up implementing special C++ functions when we see things that are in common use. So if you have some suggestions, we take feature requests!