From tom@@@k@||ber@ @end|ng |rom gm@||@com  Fri Mar 10 16:39:39 2023
From: tom@@@k@||ber@ @end|ng |rom gm@||@com (Tomas Kalibera)
Date: Fri, 10 Mar 2023 16:39:39 +0100
Subject: [Rd] On optimizing `R_NewEnv()`
In-Reply-To: <CABzLhzxrrV7AGg5nCwCdEzqB1aRcRcHByoAXrjiXgRMi7Q=4-A@mail.gmail.com>
References: <CABzLhzxrrV7AGg5nCwCdEzqB1aRcRcHByoAXrjiXgRMi7Q=4-A@mail.gmail.com>
Message-ID: <9bb1f967-9a82-deca-e23a-12ebf515f851@gmail.com>


On 2/8/23 21:51, Davis Vaughan via R-devel wrote:
> 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?

I've simplified this code in R-devel as suggested.

However, I think that the original performance motivation is weak: if 
some program creates so many, small and little used hashed environments 
that this overhead matters, probably it should use a different data 
structure, and then it will get much bigger performance benefits.

Best,
Tomas

>
> Thanks,
> Davis
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] On optimizing `R_NewEnv()`
Tomas Kalibera