all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Philipp Haselwarter <philipp.haselwarter@gmx.de>
To: help-gnu-emacs@gnu.org
Subject: Re: Optimizing startup speed
Date: Fri, 04 Nov 2011 01:04:04 +0100	[thread overview]
Message-ID: <87r51ohhxn.fsf@nzebook.haselwarter.org> (raw)
In-Reply-To: C8E3EAEE-CA20-4227-9CB4-8BDE69FC912F@Web.DE

Actually `require' /does/ load the whole file as soon as
(require 'library) is evaluated. Many packages for emacs suggest to just
`require' them in your .emacs, but this is not a very good idea, as it
loads on startup (pulling in dependencies etc).
For example if you just want to read your mail with gnus, you don't need
to lose your time loading libraries for dealing with java code.

Most packages have some kind of entry point (except ofc things like
global minor modes that you really always want to be active).
Ideally the package comes with a 'package-loads.el' or '-autoloads.el'
which does not contain the actual library code but just the signatures
of these entry points and the name of the file where the actual
definition resides.
Emacs then offers you to use these functions, often as hooks or
commands, and loads the actual code just when it needs (ie when you
first call it).
If the package fails to provide autoloads you can easily define them
yourself. For example if the package « brew.el » contains a command
« brew­arabica », you could put the line
--8<---------------cut here---------------start------------->8---
(autoload 'brew­arabica "brew" "Brews a nice cup of arabica" t)
--8<---------------cut here---------------end--------------->8---
in your config file. Now startup won't be slowed down and the brew
package will be loaded just when you need it.

Factoring out configuration of features/packages also has the advantage
of reducing the quantity of code that has to be evaluated.
For example, if our "brew" package contained a « after­brew­hook »,
there's no point in assigning values to that hook until the package is
loaded. This can be achieved using `eval-after-load':
--8<---------------cut here---------------start------------->8---
(eval-after-load 'brew '(add-hook 'after­brew­hook 'brew­add­sugar))
--8<---------------cut here---------------end--------------->8---

For big packages you can easily wind up with a few hundred lines of
configuration. In those cases you factor it out to a my-brew-config.el
file that you require after « brew » is loaded:

this goes into .emacs:
--8<---------------cut here---------------start------------->8---
(eval-after-load 'brew '(require 'my­brew­config))
--8<---------------cut here---------------end--------------->8---

and this into my-brew-config:
--8<---------------cut here---------------start------------->8---
(add­hook 'after­brew­hook 'brew­add­sugar)
(setq brew­sugar­per­cup 7)
--8<---------------cut here---------------end--------------->8---

Another advantage of organizing your stuff this way is that you can more
easily share (and debug) your configuration by keeping all your personal
data in one separate file (user-full-name, erc-nick, etc).

Once you have all this set up and find starting emacs still too slow you
can use 'C-u M-x byte-recompile-directory' or 'M-x byte-compile-file'
(`B' in dired) to compile additional packages and config files. Just be
careful to recompile after you change something in your config, emacs
will rather load my-brew-config.elc than my-brew-config.el.

I hope this makes things a little clearer :)

-- 
Philipp Haselwarter




  reply	other threads:[~2011-11-04  0:04 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-03 16:46 Optimizing startup speed Buchs, Kevin
2011-11-03 20:57 ` Peter Dyballa
2011-11-04  0:04   ` Philipp Haselwarter [this message]
2011-11-07  0:28     ` Peter Dyballa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87r51ohhxn.fsf@nzebook.haselwarter.org \
    --to=philipp.haselwarter@gmx.de \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.