Emacs: Byte Compile Elisp Files

By Xah Lee. Date: . Last updated: .

What is Byte Compiled File

Emacs lisp files can be byte compiled.

emacs byte compiled file 2023-08-23 4kfT
emacs byte compiled file 2023-08-23 4kfT

Advantage of Byte Compiled Elisp File

emacs byte compile 2020-10-14 pCcgM
warnins and tips from byte compile elisp 2020-10-14

As of today (2023-08-23), for init files and light-weight packages, byte compile don't make any noticeable speed difference.

Disadvantage of Byte Compiled Elisp File

Byte compile creates a extra source of the code. If you edit elisp code and forget to byte compile it, it create a sync problem. Emacs usually check file date when loading a file, and if byte compiled file is older, emacs warns you.

Byte compile may not be compatible across different version of emacs or operating system. Technically it is, but sometimes not. When you are using a incompatible byte compiled file, it creates strange bugs.

How to Byte Compile Emacs Lisp File

There are several ways to byte compile elisp files. The simplest and most useful are:

Alt+x byte-compile-file
Compile a single file. It'll ask for a file name.
Alt+x dired-do-byte-compileB
Compile all marked files in dired.
Alt+x byte-recompile-directory
Batch compile all elisp files in current dir and subdirectory.

Loading Byte Compiled File

When emacs loads emacs lisp files, normally it will load the byte compiled version if exist. Typically by looking for the file name with the .elc suffix.

The most fundamental loading file mechanism in emacs is the function load.

Your init file, if you are using load, do not add the file name extension such as .el or .elc. This way, emacs will automatically load the byte compiled version when exist.

;; load elisp file, use byte compiled version (.elc) if exist
(load "my_emacs_keybinding")
;; no file name extension here

〔see Elisp: load, load-file, autoload

Recompile When Upgrade

you should recompile your elisp files when:

because often, emacs has some incompatible elisp changes, and big packages may fail without recompile.

Reference

2011-07-15 thanks to Adolfo Benedetti https://x.com/adben .

Emacs, Byte Compile