From d@v|@ @end|ng |rom po@|t@co  Wed Feb  8 21:51:49 2023
From: d@v|@ @end|ng |rom po@|t@co (Davis Vaughan)
Date: Wed, 8 Feb 2023 15:51:49 -0500
Subject: [Rd] On optimizing `R_NewEnv()`
Message-ID: <CABzLhzxrrV7AGg5nCwCdEzqB1aRcRcHByoAXrjiXgRMi7Q=4-A@mail.gmail.com>

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



[Rd] On optimizing `R_NewEnv()`
Davis Vaughan