[Rd] On optimizing `R_NewEnv()`

Davis Vaughan d@v|@ @end|ng |rom po@|t@co
Wed Feb 8 21:51:49 CET 2023


Hi all,

I really like the addition of `R_NewEnv()` back in 4.1.0
https://github.com/wch/r-source/blob/625ab8d45f86f65561e53627e1f0c220bdc5f752/src/main/envir.c#L3619-L3630

I have a use case where I'm likely to call this function a large
number of times to generate many small hashed environments, so I'd
like to optimize it as far as possible.

I noticed that it takes `int size`, converts that to a SEXP for
`R_NewHashedEnv()`, which then simply converts that back to an `int`
here:
https://github.com/wch/r-source/blob/625ab8d45f86f65561e53627e1f0c220bdc5f752/src/main/envir.c#L378

I wonder if we could cut out that intermediate SEXP (along with its
protection) by adjusting `R_NewHashedEnv()` to instead take `int
size`.

I'd be happy to do a patch if that sounds good. I'd update all uses of
`R_NewHashedEnv()` to supply `int`s instead, which actually seems like
it would make every instance of calling that function simpler:
https://github.com/search?q=repo%3Awch%2Fr-source%20R_NewHashedEnv&type=code

So hopefully a win everywhere?

Thanks,
Davis



More information about the R-devel mailing list