From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Philipp Haselwarter Newsgroups: gmane.emacs.help Subject: Re: Optimizing startup speed Date: Fri, 04 Nov 2011 01:04:04 +0100 Message-ID: <87r51ohhxn.fsf@nzebook.haselwarter.org> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: dough.gmane.org 1320365074 31749 80.91.229.12 (4 Nov 2011 00:04:34 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 4 Nov 2011 00:04:34 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Nov 04 01:04:31 2011 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1RM7Gn-0006Lj-D9 for geh-help-gnu-emacs@m.gmane.org; Fri, 04 Nov 2011 01:04:29 +0100 Original-Received: from localhost ([::1]:45449 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RM7Gm-00032D-MD for geh-help-gnu-emacs@m.gmane.org; Thu, 03 Nov 2011 20:04:28 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:39708) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RM7Gi-00031x-DV for help-gnu-emacs@gnu.org; Thu, 03 Nov 2011 20:04:25 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RM7Gh-0002td-6a for help-gnu-emacs@gnu.org; Thu, 03 Nov 2011 20:04:24 -0400 Original-Received: from lo.gmane.org ([80.91.229.12]:36926) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RM7Gg-0002tV-Ra for help-gnu-emacs@gnu.org; Thu, 03 Nov 2011 20:04:23 -0400 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1RM7Gf-0006Ix-IP for help-gnu-emacs@gnu.org; Fri, 04 Nov 2011 01:04:21 +0100 Original-Received: from 52.88.114.78.rev.sfr.net ([78.114.88.52]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 04 Nov 2011 01:04:21 +0100 Original-Received: from philipp.haselwarter by 52.88.114.78.rev.sfr.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 04 Nov 2011 01:04:21 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 63 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 52.88.114.78.rev.sfr.net X-NSA-Fodder: Glock TWA anarchy Craig Livingstone Bush Wired Crowell X-Meat: Buffalo User-Agent: Gnus/5.110018 (No Gnus v0.18) Emacs/24.0.91 (gnu/linux) Cancel-Lock: sha1:7HA+H48cY1xKLato6TB+dM+H9ps= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 80.91.229.12 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:82750 Archived-At: 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