Therneau, Terry M., Ph.D.
2025-Dec-02 12:41 UTC
[Rd] Speed question: passing arguments vs environment
I have a complex likelihood function f() to maximize, with lots of arguments (some of which set up indexes for derivatives, for instance). When using something like optim(), one can pass these arguments through via its ? arg, or could make the likelihood function f() live in the same environment as the main routine so they are found directly. Is there any advantage of one versus the other wrt speed? At the end of the day, f() may get called thousands of times in a Hamiltonian MCMC. Since R does not replicate arguments that are used in a read-only fashion, one might expect little to no penalty for having them on the call chain, unless the bookkeeping for copy-on-write is itself time consuming. Terry T. [[alternative HTML version deleted]]
While not directly answering your question, what about collecting all these arguments into an env object? Then you are just passing one argument in the ... and it is passed by reference, which is fast. On Tue, Dec 2, 2025 at 2:42?PM Therneau, Terry M., Ph.D. via R-devel < r-devel at r-project.org> wrote:> I have a complex likelihood function f() to maximize, with lots of > arguments (some of which set up indexes for derivatives, for instance). > When using something like optim(), one can pass these arguments through > via its ? arg, or could make the likelihood function f() live in the same > environment as the main routine so they are found directly. Is there any > advantage of one versus the other wrt speed? At the end of the day, f() > may get called thousands of times in a Hamiltonian MCMC. > > Since R does not replicate arguments that are used in a read-only fashion, > one might expect little to no penalty for having them on the call chain, > unless the bookkeeping for copy-on-write is itself time consuming. > > Terry T. > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >[[alternative HTML version deleted]]
Duncan Murdoch
2025-Dec-02 13:41 UTC
[Rd] Speed question: passing arguments vs environment
On 2025-12-02 7:41 a.m., Therneau, Terry M., Ph.D. via R-devel wrote:> I have a complex likelihood function f() to maximize, with lots of arguments (some of which set up indexes for derivatives, for instance). > When using something like optim(), one can pass these arguments through via its ? arg, or could make the likelihood function f() live in the same environment as the main routine so they are found directly. Is there any advantage of one versus the other wrt speed? At the end of the day, f() may get called thousands of times in a Hamiltonian MCMC. > > Since R does not replicate arguments that are used in a read-only fashion, one might expect little to no penalty for having them on the call chain, unless the bookkeeping for copy-on-write is itself time consuming. >I think there wouldn't be a big difference in timing, but I'd guess there'd be a slight advantage to having them in the environment of the objective function. I'd recommend testing both ways if you want to be sure. Here are the differences that might matter: - putting them as arguments has the overhead of constructing the argument list each time, and if the args are used, the overhead of resolving promises. - putting them in the environment of the function means lookups will take two steps: the evaluation frame followed by its parent, instead of succeeding in the first lookup. I suspect the overhead of arguments is bigger, but it's probably not very big. Duncan Murdoch
Duncan Murdoch
2025-Dec-02 13:48 UTC
[Rd] Speed question: passing arguments vs environment
On 2025-12-02 7:41 a.m., Therneau, Terry M., Ph.D. via R-devel wrote:> I have a complex likelihood function f() to maximize, with lots of arguments (some of which set up indexes for derivatives, for instance). > When using something like optim(), one can pass these arguments through via its ? arg, or could make the likelihood function f() live in the same environment as the main routine so they are found directly. Is there any advantage of one versus the other wrt speed? At the end of the day, f() may get called thousands of times in a Hamiltonian MCMC. > > Since R does not replicate arguments that are used in a read-only fashion, one might expect little to no penalty for having them on the call chain, unless the bookkeeping for copy-on-write is itself time consuming.By the way, a nice way to put the args in the environment of the objective function is to use local() or a builder, e.g. objective <- local({ arg1 <- 1 arg2 <- 2 arg3 <- 3 function(x) { # objective code here that can see arg1, arg2, arg3 } }) or makeObjective <- function(arg1, arg2, arg3) { force(arg1) # evaluate the promises force(arg2) force(arg3) function(x) { # objective code here that can see arg1, arg2, arg3 } } objective <- makeObjective(1,2,3) Duncan Murdoch
Do you have some publicly available code snippets where you compare different approaches? It can be not exactly your problem but somewhat representative. Best, Serguei. Le 02/12/2025 ? 13:41, Therneau, Terry M., Ph.D. via R-devel a ?crit?:> I have a complex likelihood function f() to maximize, with lots of arguments (some of which set up indexes for derivatives, for instance). > When using something like optim(), one can pass these arguments through via its ? arg, or could make the likelihood function f() live in the same environment as the main routine so they are found directly. Is there any advantage of one versus the other wrt speed? At the end of the day, f() may get called thousands of times in a Hamiltonian MCMC. > > Since R does not replicate arguments that are used in a read-only fashion, one might expect little to no penalty for having them on the call chain, unless the bookkeeping for copy-on-write is itself time consuming. > > Terry T. > > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel