Install and use Emacs 28 Native comp (gccemacs)

Features

GCC Emacs uses libgccjit to compile and run Emacs Lisp as native code in form of re-loadable elf files. Therefore, it reaches a 3.8 times faster performance.

More details can be found in the paper.

Installation

MacOS

First enable gcc with libgccjit. cd /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula and edit gcc.rb

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
-    languages = %w[c c++ objc obj-c++ fortran]
+    languages = %w[c c++ objc obj-c++ fortran jit]

    args = %W[
      --prefix=#{prefix}
      --libdir=#{lib}/gcc/#{version_suffix}
      --disable-nls
      --enable-checking=release
      --enable-languages=#{languages.join(",")}
      --program-suffix=-#{version_suffix}
      --with-gmp=#{Formula["gmp"].opt_prefix}
      --with-mpfr=#{Formula["mpfr"].opt_prefix}
      --with-mpc=#{Formula["libmpc"].opt_prefix}
      --with-isl=#{Formula["isl"].opt_prefix}
      --with-zstd=#{Formula["zstd"].opt_prefix}
      --with-pkgversion=#{pkgversion}
      --with-bugurl=#{tap.issues_url}
+     --enable-host-shared
    ]

Then run brew install gcc.

After successful installation of libgccjit, there are (at least) four ways to compile Emacs native comp (emacs-plus@28 works for my laptop with Big Sur 11.4).

After compilation, it takes a while to compile your lisp codes. Just wait.

Windows

Enable WSL2 and install ArchWSL

To enable WSL2 in Windows, follow the steps in microsoft doc. You can also download kernel update package if you have already installed it.

Since Arch Linux is not included in the Microsoft Store, we need to install it manually. However, yuk7 has a project to make this possible and easy to do based on a perfect documentation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#set root password
passwd

#setup sudoers
echo "%wheel ALL=(ALL) ALL" > /etc/sudoers.d/wheel

#add user
useradd -m -G wheel -s /bin/bash {username}

#set user password
passwd {username}

#exit and use shell under windows to set default user
.\Arch.exe config --default-user {username}

#login again to Arch
sudo pacman-key --init
sudo pacman-key --populate

#update system
pacman -Syu

#install package as you want
pacman -S <package>

Install yay (an AUR helper)

1
2
3
4
pacman -S --needed git base-devel
git clone https://aur.archlinux.org/yay.git
cd yay
makepkg -si

Add gpg key of libgccjit

This step is needed because libgccjit key is not included in archwsl. Use the command below to add it.

1
2
#replace <key> with the hashed keys according to cmd notification
gpg --keyserver keyserver.ubuntu.com --recv-key <key1> <key2>

Install Emacs 28 (each one should work)

1
2
3
4
5
6
7
8
yay -S emacs-gcc-wayland-devel-bin
yay -S emacs-native-comp-git-enhanced
yay -S emacs-native-comp-git

#from source code
git clone https://aur.archlinux.org/emacs-native-comp-git.git
cd emacs-native-comp-git
makepkg --syncdeps --install

Use XServer to run GUI app

After installation, you can run Emacs but it won’t show xwindow as you expect. You need use a XServer (e.g., MobaXterm, X410, VcXsrv) to do this. Several months ago I’ve written a post that describes my experience of using Emacs on Windows, check it if you like.

It’s also worth noting that in the next generation of Windows, an official GUI support, WSLG, will be provided by Microsoft. You may want to check it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#add this line to your .zshrc/.bashrc
export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0.0

#test if works, expected output: <IP:port>
echo $DISPLAY

#or
nc -v <IP> 6000

#rescale GUI app
export GDK_SCALE=2
#or
export QT_SCALE_FACTOR=2

Linux (Ubuntu)

If you don’t want to build from source or are not able to do this due to lack of full permission, you might need the following two ways.

Emacs-ng

Emacs-ng is based off of the branch of emacs, and regularly merges in the latest changes(this branch includes the native compilation feature from Andrea Corallo).

One can easily download package file (.deb) from release page and use sudo dpkg -i xxx.deb to install it. Note Emacs-ng includes many experimental features powered by webrender and Google V8 engine, which means it’s a fork instead of a Vanilla Emacs.

Snap

Emacs-snap is another way to install latest Emacs in Ubuntu. It’s maintained by Alex Murray and now it includes native-comp.

Emacs configurations

Clone straight.el (optional)

The original boostrap code of straight.el won’t work in WSL2, so we need clone the repository to the .emacs.d directory. See the Debugging section of doc.

1
git clone https://github.com/raxod502/straight.el.git ~/.emacs.d/straight/repos/straight.el

Native comp support

Download your configuration file with codes below to unleash its power.

1
2
3
4
5
6
7
8
(when (and (fboundp 'native-comp-available-p)
           (native-comp-available-p))
  (progn
    (setq native-comp-async-report-warnings-errors nil)
    (setq comp-deferred-compilation t)
    (add-to-list 'native-comp-eln-load-path (expand-file-name "eln-cache/" user-emacs-directory))
    (setq package-native-compile t)
    ))

Conclusion

My experience is that it’s significantly faster on Mac, which is why I spend more time cloning this on Windows. However, on WSL2 I really can’t find the difference because the Emacs 28 without native comp feature on WSL2 is already very fast (init time < 1.0s). Given the blurry problem of running GUI app based on XServer on a high DPI screen, consider twice before your decision.

0%