R-alpha: Using autoload and smaller 'base' package [was "Re: New Version"].

Thomas Lumley Thomas Lumley <thomas@biostat.washington.edu>
Wed, 29 Oct 1997 10:57:28 -0800 (PST)


On Wed, 29 Oct 1997, Paul Gilbert wrote:

> Perhaps someone could bring those of us not in "core" up to date a bit.
> What are advantages of autoload and the implications, especially for
> memory and speed. 

There is very little difference between explicit loading using library() 
or require() and autoload(). 

If you type 
	autoload("myfun","package") 
then typing, say,
	a<-myfun(42)
is equivalent to 
	library(package)
	a<-myfun(42)
This is accomplished by subtle magic involving the "lazy evaluation"
mechanism.

Using autoload() has a (small) additional overhead in speed and memory (12
cons cells), as the autoload information must be stored somewhere and it
must be parsed when myfun() is called. It is also possible that functions
which do creative things with sys.frame()  may not like being autoloaded,
but I don't have any examples.

The main difference in speed over putting a library() statement in
Rprofile is that the delay happens when the function is first used rather
than when R is started.

>	Should I consider autoloading everything? Will I still need
> to divide the package into pieces? Should I avoid autoloading anything
> that is used really frequently?

As autoload("myfun","package") works by arranging for 
	library("package")  
to be evaluated in the global environment the first time myfun() is called
it should work the same as library().

If you know what functions you use all the time, it may well make sense to
just use library() to load them. 

The point of autoload() is transparency.  If you want survival analysis
routines available for your or your students' occasional use, but don't
want to sacrifice 40000 cons cells and 100k of vector heap, you can put a
set of autoloads in Rprofile for all the common functions in survival4 and
have them available if they are needed. 

It also makes sense for a package containing some rarely used features to
be divided in two, so that you initially load the commonly used features
and autoloads for the other.  In the version of survival4 for R0.60 coming
very soon, the US census mortality tables that come with survival4 for S
are in a separate package with autoloads to load them only if they are
needed.  Since they occupy nearly 200k of the vector heap and I personally
have never used them this is a worthwhile saving.

	-thomas

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=