8 ms·
Advanced techniques for reducing Emacs startup time
- drKarl 5y ago
- jb1991 5y agoCompletely unproductive comment teasing an old pointless flame war.
- vicda 5y ago(Despite the comment clearly being flamewar bait...) Most of the other comments are correctly suggesting to just not exit emacs. Something opening slowly once at the beginning of a long workflow is hardly an issue. If you must open something quickly over and over again, being proficient in a secondary lightweight editor like vim wouldn't be a bad suggestion.
- tom_ 5y agoEasiest thing is just not to quit! I stopped autoloading things, as i found it less bothersome to wait 30 seconds for Emacs to load than to get surprised by an autoload delay while working.
- julian37 5y agoCame here to say this. I'm starting Emacs only after a reboot, who cares if it takes 3s or 30s to launch (mine around 5 or so.) Use emacsclient if you want to use it as $EDITOR. https://emacsredux.com/blog/2014/12/23/uptime/ https://emacsredux.com/blog/2014/12/23/uptime/
- tra3 5y agoSadly my emacs freezes about once every 2 weeks. Hand rolled config that restores everything within 30 seconds. But still.
- jonathanstrange 5y agoMy Emacs takes ages to start but for a reason. It takes most of the time to automatically load every file I've ever edited into a buffer. Finding the right buffer is much faster for me than navigating the filesystem, and whenever I use bookmarks they're outdated too quickly or I can't remember them. I guess my habit is the equivalent of piling papers on a desk.
- OskarS 5y agoI just use recentf for that. My buffer list contains everything in the recentf (this works with buffer list replacements from both Helm and Ivy). Makes it trivial to open up any file I've had open before. Opening all buffers you've ever edited sounds nuts to me. What if one of the files you edited was a 10 gb log file or whatever?
- zeendo 5y agoI like this approach and almost considered doing it after reading this - or probably only opening the last few hundred files - but then I realized that the recent file list probably gets me 99% of the way there. Is there anything you get by opening the files that you wouldn't get by opening files from the recent file list instead?
- stjohnswarts 5y agowell I usually have trouble remembering the exact name of the file I was editing 2 months ago? :) . Now if there is a plugin to have rg look through the last few hundred files that I've edited that would be a game changer.
- watrup 5y agoI have something very much like this, a trivial addition to find-file. Typing C-u C-x C-f shows the recently edited filenames (with paths), and vertico + orderless makes it a joy to instantly pick the specific recent file. I get the same UI with regular find-file (C-x C-f), so the two bindings live harmoniously in my brain and fingers. (defun MY-consult-find-file (arg) "If no prefix: find-file, otherwise consult-recentf." (interactive "p") (let* ((filename (if (eq arg 1) (call-interactively 'find-file) (call-interactively 'consult-recent-file))) ))) (global-set-key (kbd "C-x C-f") 'MY-consult-find-file) Before switching to vertico/orderless, I used ivy with essentially the same machinery. That worked great too.
- thom 5y agoWhile I’m very much in the “don’t really care” camp cos I don’t quit Emacs often, I did find optimising my startup time quite educational, and moving to use-package gave me an opportunity to go through every mode in detail, instead of just making the occasional ad-hoc tweak when something annoyed me. I would also point out that early-init exists these days and is the best place for some startup logic (especially bits that prevent UI ever being shown so you don’t get weird flashes of stuff during startup).
- faho 5y agoIdeally use-package would simply be included in emacs, so you could rely on it. Apparently they are getting close: https://github.com/jwiegley/use-package/issues/282 https://github.com/jwiegley/use-package/issues/282 Until then, I have this in my init.el to install it if necessary: ;; Install use-package because that installs everything else (unless (package-installed-p 'use-package) (package-refresh-contents) (package-install 'use-package)) (require 'use-package)
- hprotagonist 5y agoexcept that use-package, by itself, does not install anything. NOTE: use-package is not a package manager! Although use-package does have the useful capability to interface with package managers (see below), its primary purpose is for the configuration and loading of packages. (I use straight, which interfaces nicely into use-package.)
- faho 5y ago> except that use-package, by itself, does not install anything. It can be configured to install things (by using the builtin package.el): https://jwiegley.github.io/use-package/keywords/#ensure-pin https://jwiegley.github.io/use-package/keywords/#ensure-pin > You can use use-package to load packages from ELPA with package.el. This is particularly useful if you share your .emacs among several machines; the relevant packages are downloaded automatically once declared in your .emacs. The :ensure keyword causes the package(s) to be installed automatically if not already present on your system (set (setq use-package-always-ensure t) if you wish this behavior to be global for all packages) So if all you need is package.el (and there's a good chance that's the case), you can just (setq use-package-always-ensure t) and your configuration will now install the packages if you copy it to a new system.
- timlod 5y agoI do what the author mentions as the common retort to their approach: starting an Emacs daemon (server) at boot, and opening Emacsclient when needed. In general, I always have one frame open to do actual work on, but the client is still useful if I ever navigate with a file explorer or open things through the command line (all text extensions open in Emacs). I'm not sure all the extra effort needed really does the trick - I don't think mine would start up 'fast enough' to beat running a daemon even if I deferred loading most of my config (which can lead to config issues at times). That being said, I had a look at their dotfiles, and it seems really well organized. If such an effort makes you write a great config, it may be worth it regardless of whether you end up using the daemon or not!
- faho 5y agoI prefer having a clean instance so I can open up an emacs for the project I'm currently working on, as opposed to lugging all those other buffers around and leaving any changes intact. So I don't use a daemon. And these "advanced techniques" aren't really all that hard - 90% is just using use-package, which has a number of other advantages. It can be set up to install uninstalled packages, so you can do a self-installing init.el. In my case mine will download my other config files, install use-package and then install all packages I use. It also leads to better organization of your config, because you keep the packages self-contained instead of splattering random settings all over init.el.
- unhammer 5y agoI no longer let use-package install things. I use the same init on different computers, so sometimes I would have to wait for use-package to install things on startup and sometimes melpa would be down or some package would be a newer version and require new deps and use-package doesn't handle such situations. It's very little gain for a lot of hassle. Instead I just check all of emacs.d/elpa into the same git repo. That way I can also easily review changes or roll back to last working version.
- morelisp 5y ago
- vegai_ 5y agoHere's mine: https://wiki.archlinux.org/title/emacs#As_a_systemd_unit https://wiki.archlinux.org/title/emacs#As_a_systemd_unit and aliases ec='emacsclient -nc' e='emacsclient -c' I still use neovim because sometimes it's neater that the file opens up in the same terminal where I am in.
- mickeyp 5y agoYou can instruct Emacs (with or without emacsclient) to open in a terminal with: emacsclient -nw That way, you'll connect to your Emacs server but using a terminal Emacs (even if your main instance is GUI Emacs)
- vegai_ 5y agoOh, I wonder how I had missed that option. Thanks!
- R0flcopt3r 5y agoor you use vterm-mode as your terminal and then you simply open a file like you do now, except now it opens in place of the vterm-mode buffer. Close the buffer like you would do, and you're back to vterm.
- jonpalmisc 5y agoI’d like to see something like this but for speeding up Emacs at runtime. I have a 0.25s startup time with packages but my Emacs struggles when using lsp-mode or eglot in large C++ projects. I’ve been using Sublime Text lately but would like to get back to Emacs.
- nine_k 5y agoIs it Emacs, or the language server? It would be interesting to profile.
- loloquwowndueo 5y agoEight Megabytes And Constantly Swapping :)
- lizknope 5y agoI was searching the comments for this! I used my first Unix machine in 1991 and started college in 1993. I knew vi but the professor used emacs and showed us how to run make from within emacs. But wow was it slow! I asked an older student and he said EMACS was just like GNU and was a recursive acronym. Emacs Makes A Computer Slow and then told us Eight Megs And Constantly Swapping. The workstations had 16MB RAM at the time. One big problem was that vi was installed locally on every machine while Emacs was on a remote file server and there were probably 50 students typing "emacs" at the same time at the start of class all accessing the same server. I just used vi and a bunch of terminals although later I used XEmacs in "viper" mode (vi keybindings) because it had color syntax highlighting and the split window support was cool. Vim got those features a long time ago so I just keep using vi/vim
- caycep 5y agoHa! I was just thinking about that acronym. I guess the whole point of all this new hardware compute power on Apple Silicon and TSMC processes is to run emacs faster...
- hsbauauvhabzb 5y agoEmacs has a default buffer gc trigger at 800kb. I increased this to 1gb which mostly improves UX (no gc while scrolling) but I’m fairly sure other apps memory leak all their way to 1gb.
- throw10920 5y agogc-cons-threshold and gc-cons-percentage, for those curious. https://elmord.org/blog/?entry=20190913-emacs-gc https://elmord.org/blog/?entry=20190913-emacs-gc
- ilovecaching 5y agoI gave up on Emacs and went back to Vim because of the slowness and the bugs I was encountering while using the Emacs GUI (granted a lot of them were from lsp-mode and other heavy packages). While digging for answers I was told not to use certain platforms, don't use the GUI, use the GUI, don't turn Emacs off etc. which felt like Stockholm syndrome to me. There's a lot of cargo culted config fragments like the ones you'll find in the article. Why does every user have to constantly reinvent the same config to work around all of the issues in Emacs? I don't know, but I wasn't going to stick around and find out. Vim with all of my plugins, which has the same features as my Emacs config, starts up instantly and I've yet to encounter a single bug. I really wanted to like Emacs, and I've chalked up my bad experience to less people putting dev time towards it rather than actual negligence towards the user experience. In the article the author talks about how he's gone bankrupt three times with his config, maybe that's a sign that the problem isn't the author. If Emacs is that fragile, then its extensibility is really a moot point.
- dilap 5y agoYeah, as soon as you start customizing emacs (which is the whole point of emacs), you run into bugs. Everything is always slightly broken all the time; I don't think it's actually humanly possible to fix this -- too many parts. Fix one thing, another thing breaks. I'm addicted to the flexibility, but I dream of something that offers the same level of flexibility in a more robust ecosystem (e.g. statically typed, statically scoped, what else?). (Is such a thing even possible? I don't know!)
- cheesewhizemacs 5y ago>Everything is always slightly broken all the time; I don't think it's actually humanly possible to fix this -- too many parts. Fix one thing, another thing breaks. Speak for yourself. Just last weekend I had a few hours with little to do, and actually wanted to do some yak shaving on my Emacs config just for the hell of it. but it works too well and I have everything the way I like it! My yak is f*kin' bald, man! Not only have I already worked out all the little bugs that annoy me, but I've added all the features I can think of that I want!
- rawoke083600 5y agoGood Tips ! :) Just thought I'd add a side-note; For anyone wanting to try emacs really give "Doom Emacs" a try ! It's brilliant and comes with vim-bindings (a.k.a evil-mode) ! Doom Emacs has spend a lot of time on making emacs(doom) fast !
- kaushalmodi 5y agoThis post felt like a deja vu, because I had read this earlier back in Nov 2017 :). The HN mods should add the 2017 year to the title. That said, for people who haven't read this post earlier, this post shares some great tips! I like the one about deferring packages using `use-package`. Because this is an old post, references like Spacemacs are now quite dated. Folks prefer Doom Emacs nowadays (though I don't use either of them).
- nine_k 5y agoI honestly don't understand the point. Emacs (much like VSCode or Idea) is a tool that I use for everything, all day. I don't see a point in shutting it down, except for upgrading the version. Its startup time of, say, 15 or even 30 seconds (if you start many language servers) is only paid once, when I log in. So I run it in server mode, and emacsclient start instantly, as a GUI window, or in a terminal, with all the amenities of an IDE already in place.
- giraffe_lady 5y agoJust different use cases then. Emacs isn't my main coding tool these days but I still use it for single-file editing, and I close it when I'm done with that file. The start up time does matter to me somewhat. Though I have a pretty simple config for the same reason and it's not bad enough for me to spend time on. I do keep an eye on it though.
- nine_k 5y agoThere are mini-editors that try to mimic emacs: https://opensource.com/article/20/3/lightweight-emacs https://opensource.com/article/20/3/lightweight-emacs (and more which I forget).
- ta988 5y agoUse the emacs server then. Start once use many.
- stjohnswarts 5y ago"emacs --daemon" and emacs-client are key to solving this scenario
- throw10920 5y agoI hope this doesn't come along as aggressive but: Do you really have a "different use case", or are you just averse to the idea of running it in the background as a daemon? Do you really need to save the 70 MB (without addons) to 200 MB (with addons) that it takes sitting there? Unlike bloated Electron apps, Emacs doesn't actually use CPU while you're not using it - it just sits there, takes up a little memory (and even on my old 4 GB RAM ThinkPad it's not enough to actually inconvenience me), and awaits you opening a new frame with emacsclient.
- dilap 5y agoI just disable emacs garbage collection entirely. Fast startup, no more random pauses, haven't noticed any problems yet. (This was probably like a year ago.) (On the other hand, emacs is never quite problem free, so maybe this is introducing problems and I just haven't noticed.) I have tons of customizations and lots of packages, but be as religious as reasonable about lazy loading. $ time emacs --eval '(save-buffers-kill-emacs)' real 0.21s Not bad, but still very noticable, which is annoying. About half of it is unavoidable. $ time emacs -q --eval '(save-buffers-kill-emacs)' real 0.11s (-q disables reading the config file.) (With default GC settings, startup is about 300ms.)
- matthews2 5y agoInstead of totally killing the garbage collector, you can effectively turn it off during startup by setting gc-cons-threshold to a large number, and then restore it after init. The article touches on this but they don't increase it very much and restore it at the end of init.el (emacs-startup-hook runs later). early-init.el: (setq gc-cons-threshold most-positive-fixnum) (add-hook 'emacs-startup-hook (lambda () (setq gc-cons-threshold (* 2 1024 1024))))
- dilap 5y agoIt was subtle in my message, but my other reason for disabling was to avoid random collection pauses when using emacs. A sibling poster recommended GCing on deactivate, which is an interesting idea! But again, I truly haven't been able to notice any ill effects of not GCing. I tend to probably leave emacs open for maybe a few hours at a time, so I guess in that sense I'm manually GCing. :-)
- matthews2 5y agoNot running the GC at all is certainly interesting though :) Have you tried running 'M-x memory-report' before quitting to see how much memory you ended up using?
- 5y ago
- kkfx 5y agoHonestly I'll rather prefer a bigger multi-threading/MP usage to reduce deadlocks because Emacs is an ecosystem, even reducing it's startup time to the minimum it will never be like Vim, those who do not use EXWM simply can run Emacs daemon at their desktop startup to much with the client Vim startup time, BUT those who use Emacs as a DE like me I bet all prefer far less freezing like during packages upgrades, feeds fetching, Gnus etc vs the not really relevant startup time...
- randomcarbloke 5y agoeven with every module and restoring my buffers Emacs loads faster than any other IDE I use.
- seertaak 5y ago
- b3morales 5y ago> Restarting Emacs helps verify that customization will persist between sessions. On this point I agree, but another (better, in my opinion) option is to hack in one instance and spawn a second to check the config changes you're making. You can kill and recreate the spawned one with less interruption to your work.
- throw10920 5y agoFrom the article, in rebuttal to the "use daemon mode" argument: > Fast things feel nicer. Daemon mode is fast. Mine takes less than a second to open a new frame with a fully-loaded Spacemacs setup, and while my startup time is 20-30 seconds, it runs in the background upon login, and I don't care about that gap. > When customizing Emacs, you sometimes get into weird states that can be hard to recover from without restarting. For example, if you add a slow lambda function to your post-command-hook, it’s tough to remove it. > Restarting Emacs helps verify that customization will persist between sessions. These only matter if your primary use-case for Emacs is...customizing Emacs. Which is a thing that some people do, I guess, but I'm more interested in using Emacs as a tool. Sure, I'm going to spend dozens of hours tweaking it, but I'll spend thousands of hours using it - and rewriting my init.el (especially from its current Spacemacs state) is also going to take dozens of hours, which will definitely wipe out any gains that I would have gotten from having a slightly faster iteration time during the elisp development loop. That said, I still think that the information presented in the article is useful! It's always good to know why software is slow and how to make it faster, and some people just don't want to use daemon mode.
- IshKebab 5y ago20-30 seconds? I thought VSCode was slow and that only takes about 10 (with lots of heavy extensions).
- bfrog 5y agoI’m honestly more excited by helix at this point, though I’m still using emacs with doom and daemon mode setup. The thing about helix is… it starts instantly and let’s say 90% of what I care about
- guenthert 5y agoOne of the problems I never knew I had. Made me curious, how long does it actually take to start. Not entirely trivial to determine, but for the initialization time, Emacs has a function which reports it (of course): (emacs-init-time) "4.7 seconds" Holy cow, that's a lot. But perhaps given that this is a first gen. Banana Pi (1GHz dual-core ARM32) not actually all that much. And given (emacs-uptime) "99 days, 3 hours, 14 minutes, 25 seconds" not significant either.
- sa46 5y agoAuthor here. Things that have changed since I published this. - Doom emacs does most (all) of these with a bunch of cleverer techniques than mine. I’d recommend doom over rolling your own emacs. - I use IntelliJ just about exclusively. - I now do the minimal amount of customization I can get away with.
- rurban 5y ago(2017) really, but still relevant
- liyuankui 5y agoWas expecting some description about dump.
- liyuankui 5y agoAnyone interested in it, please refer to https://www.emacswiki.org/emacs/DumpingEmacs https://www.emacswiki.org/emacs/DumpingEmacs 2~3 seconds startup time with hundreds of packages.