reddit / emacs
What is the difference between installing a package, "requiring it" and enabling it? Which of these increases startup time?
LowCom · 2022-05-11 05:00
From what I understand. Please tell me which part is wrong.
Installing a package through package-install copies the package files into .emacs.d directory. That's all it does. It just sits there and is not loaded during Emacs startup. Eg. package-install evil
When you require a package, its code is loaded during startup. So it does slow down the Emacs startup time. But the package is not activated, i.e it's key bindinds are not yet available.
Eg. (require evil)
When you activate a package, it's commands and key binds are available to you as soon as you start up Emacs in each and every buffer. So this slows down start up time, but the slowness should be same as when requiring a package as even if you just require it, the code is still run.
Eg: (evil-mode 1)
Horrih · 2022-05-11 05:24
Just having the package installed slows your startup, because emacs reads its auload functions, which are the packages' main commands which, when called, will require the package.
With 50 packages this is 500 ms load time for me. You can't really reduce it easily
Requiring it will actually import the whole elisp file.
I'd say this is the main contribution to your startup time. For me this is the main contribution to startup time, and easy to reduce by lazy loading packages as much as possible. For me about 10x slower than the autoload part
Activating the package is heavily package dependant. Most of the time it's quite fast, but say you have a really big file and enable some mode, it can slow down enormously your emacs
7890yuiop · 2022-05-11 05:55
> You can't really reduce it easily
package-quickstart is a variable defined in `package.el'.
Its value is nil
You can customize this variable.
This variable was introduced, or its default value was changed, in
version 27.1 of Emacs.
Documentation:
Precompute activation actions to speed up startup.
This requires the use of `package-quickstart-refresh' every time the
activations need to be changed, such as when `package-load-list' is modified.
Obviously it still takes time to `read` the autoloaded forms, but this approach means you're only reading a single file (instead of a file per package), so it eliminates most of the related filesystem activity.
Horrih · 2022-05-11 06:00
Thanks I didn't kno about this one !
7890yuiop · 2022-05-11 06:07
> When you require a package ... the package is not activated, i.e it's key bindinds are not yet available.
>
> When you activate a package, it's commands and key binds are available to you as soon as you start up Emacs
There's no standard notion of "activating" a package. Loading a package (with `require` or otherwise) will read and evaluate all of its code, and that code can do anything it wants to. It *might* provide key bindings and various other things (it almost certainly defines keymaps). In general packages are *expected* not to be intrusive until you do something more explicit, so it's true that there commonly is some kind(s) of "activation"; but what that entails specifically is going to be package-dependent (but typically calling some kind of command; perhaps a mode, as in your example).
Note that in your example, installing the `evil` package with the package manager will have generated an autoload for the `evil-mode` function, and when `package-initialize` is called when you start Emacs any such autoloads are read and defined; so when you subsequently call `(evil-mode 1)` which is only defined as an autoload, Emacs firstly loads the associated package and then evaluates the function you called, all in one smooth move.
LowCom · 2022-05-11 06:47
Ok, so when you require for eg. evil mode, all it's code is loaded and evaluated but only when you explicitly type (evil-mode 1), it's keybindings are available to you. So requiring and activating doesn't have any difference in start up time right?
On the other hand, if I just install the package and don't require it, Nothing is loaded into emacs right? Until I explicitly call 'evil-mode' from within an emacs buffer? Or all the installed packages autoloaded during startup?
I ask this because, am I slowing up my emacs just by installing 2048 game and other such "useless" stufff just for curiosity? (even though I am not requiring them)
7890yuiop · 2022-05-11 07:29
> if I just install the package and don't require it, Nothing is loaded into emacs right?
Incorrect. The PACKAGENAME-autoloads.el file is loaded into Emacs (when `package-initialize` runs, which happens automatically). Which is the reason *why* you can call `evil-mode` without having loaded the rest of the package.
> I ask this because, am I slowing up my emacs just by installing 2048 game and other such "useless" stufff just for curiosity? (even though I am not requiring them)
I think the chances are pretty good that you're worrying about a degress of "slowness" that you won't actually notice.
However:
package-load-list is a variable defined in `package.el'.
Its value is (all)
This variable may be risky if used as a file-local variable.
You can customize this variable.
This variable was introduced, or its default value was changed, in
version 24.1 of Emacs.
Probably introduced at or before Emacs version 24.1.
Documentation:
List of packages for `package-initialize' to make available.
Each element in this list should be a list (NAME VERSION), or the
symbol `all'. The symbol `all' says to make available the latest
installed versions of all packages not specified by other
elements.
For an element (NAME VERSION), NAME is a package name (a symbol).
VERSION should be t, a string, or nil.
If VERSION is t, the most recent version is made available.
If VERSION is a string, only that version is ever made available.
Any other version, even if newer, is silently ignored.
Hence, the package is "held" at that version.
If VERSION is nil, the package is not made available (it is "disabled").
I suggest that you do some tests and actually measure the difference these things make, before you worry any more about making things slower -- YMMV, but "slow*er*" doesn't necessarily mean "slow".
Perhaps https://emacs.stackexchange.com/questions/22717 also helps to clarify the autoloading part.
LowCom · 2022-05-11 07:56
so how much of the package code is loaded by packagename.autoloads?
If this contains just a few lines to locate the package when required by the user, it should not be a big deal right.
Like I imagine, when I install 2048 game, the code for the game is copied into .emacs.d folder, then 2048.autoloads is added to startup of Emacs. But 2048.autoloads only contains info about where to locate the game and run its code, if the user types in M-x 2048-mode? Right?
7890yuiop · 2022-05-11 08:04
Look at the file and see for yourself.
And then (please) do some benchmarking / timing. You are the *only* person who can answer the question of whether loading these autoload files is slowing things down in any noticeable way, because no one else can decide that for you -- and you can only find out if you actually measure it in some way, on your system, and with your personal definition of "slow".
samsjj · 2022-05-11 14:54
You can check startup time via M-x emacs-init-time. If you are calling package-initialize in your init.el that will add some time to startup and this is called automatically from emacs 27 onwards.
Checkout use-package
https://github.com/jwiegley/use-package
HM0880 · 2022-05-11 22:58
Here's a useful package for benchmarking actual times:
https://github.com/dholm/benchmark-init-el
Faerryn · 2022-05-14 17:08
when you require a package, then everything works (including keybinds)
when you activate a package, then not everything works, but most of the important binds and functions should. In fact, those binds and functions are autoloaded, which means startup is faster.
thus you should just activate packages in your config for fast startup. If a function you want isn't autoloaded by default, you can autoload it manually by looking at the documentation for `autoload`.
EDIT: make my sentences gooder