23

I was browing through compile.el and I noticed this construction:

(defcustom compile-command (purecopy "make -k ")) 

The purecopy docs state:

purecopy is a built-in function in `C source code'.

(purecopy OBJ)

Make a copy of object OBJ in pure storage. Recursively copies contents of vectors and cons cells. Does not copy symbols. Copies strings without text properties.

So I think I understand why it might be necessary if it was expected that compile-command contained a complex alist that might require modification at a later date. However its just a simple string, so why is purecopy necessary? More generally, when is purecopy needed?

31

purecopy never needed in user configuration or 3rd party libraries. In fact, it is a no-op when called from a normal Emacs instance. From Pure Storage:

This function is a no-op except while Emacs is being built and dumped; it is usually called only in preloaded Lisp files.

Some background information follows.

Pure Storage

Emacs has a two-stage build process. It first builds a plain binary, and then runs this binary, loads a selected set of built-in libraries, and then dumps a memory image of the process, and creates the real emacs binary from this dump.

The reasons for this convoluted process are better performance and less memory usage: Pre-loading essential Emacs Lisp libraries allows for faster startup, since no further IO is required to load built-in libraries, and decreases memory usage, since the pre-loaded libraries can be shared among all Emacs instances.

This particular memory area which contains the pre-loaded libraries is the “pure storage”. It's readonly, hence “pure”, to allow for the aforementioned memory sharing. Hence, purecopy is a no-op in normal Emacs instances: It couldn't write into pure storage anyway. Consequently, pure storage is also exempt from garbage collection.

However, while creating and dumping the memory image for pre-loaded libraries, these libraries can use purecopy to “mark” specific objects for allocation in pure storage, typically frequently used objects that are needed for the entire lifetime of Emacs anyway.

Strings in pure storage are essentially global static constant strings. Since "make -k" is going to be used frequently in running Emacs sessions (it's likely the most frequently used compilation command, even today), it makes sense to keep this string constant and static, to avoid unnecessary re-allocation and garbage collection.

This concept of pure storage is also the reason why modifying the .el files of some built-in libraries (notably startup.el, subr.el, and others) has no effect: Emacs never actually loads the source files of these libraries. Instead, it has their byte code built into its binary, and loads them from a special memory area.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.