[Rd] Speed-up/Cache loadNamespace()

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Sun Jul 19 18:02:27 CEST 2020


On 19/07/2020 11:50 a.m., Mario Annau wrote:
> Dear all,
> 
> in our current setting we have our packages stored on a (rather slow)
> network drive and need to invoke short R scripts (using RScript) in a
> timely manner. Most of the script's runtime is spent with package loading
> using library() (or loadNamespace to be precise).
> 
> Is there a way to cache the package namespaces as listed in
> loadedNamespaces() and load them into memory before the script is executed?
> 
> My first simplistic attempt was to serialize the environment output
> from loadNamespace() to a file and load it before the script is started.
> However, loading the object automatically also loads all the referenced
> namespaces (from the slow network share) which is undesirable for this use
> case.

I don't think there is, but I doubt if it would help much. 
loadNamespace will be slow if loading the package is slow, and you can't 
avoid doing that once.  (If you call loadNamespace twice on the same 
package, the second one does nothing, and is really quick.)

I think the only savings you might get is the effort of merging various 
tables (e.g. the ones for dispatching S3 and S4 methods), and I wouldn't 
think that would take a really substantial amount of time.

One thing you could do is to create a library on a faster drive, and 
install the minimal set of packages there.  Then if that library comes 
first in .libPaths(), you'll never hit the slow network drive.

Duncan Murdoch



More information about the R-devel mailing list