www.delorie.com/gnu/docs/emacs/cc-mode_34.html   search  
Buy the book!

CC Mode Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10. Performance Issues

C and its derivative languages are highly complex creatures. Often, ambiguous code situations arise that require CC Mode to scan large portions of the buffer to determine syntactic context. Such pathological code(35) can cause CC Mode to perform fairly badly. This section identifies some of the coding styles to watch out for, and suggests some workarounds that you can use to improve performance.

Because CC Mode has to scan the buffer backwards from the current insertion point, and because C's syntax is fairly difficult to parse in the backwards direction, CC Mode often tries to find the nearest position higher up in the buffer from which to begin a forward scan. The farther this position is from the current insertion point, the slower the mode gets. Some coding styles can even force CC Mode to scan from the beginning of the buffer for every line of code!

One of the simplest things you can do to reduce scan time, is make sure any brace that opens a top-level construct(36) always appears in the leftmost column. This is actually an Emacs constraint, as embodied in the beginning-of-defun function which CC Mode uses heavily. If you insist on hanging top-level open braces on the right side of the line, then you might want to set the variable defun-prompt-regexp to something reasonable, however that "something reasonable" is difficult to define, so CC Mode doesn't do it for you.

A special note about defun-prompt-regexp in Java mode: while much of the early sample Java code seems to encourage a style where the brace that opens a class is hung on the right side of the line, this is not a good style to pursue in Emacs. CC Mode comes with a variable c-Java-defun-prompt-regexp which tries to define a regular expression usable for this style, but there are problems with it. In some cases it can cause beginning-of-defun to hang(37). For this reason, it is not used by default, but if you feel adventurous, you can set defun-prompt-regexp to it in your mode hook. In any event, setting and rely on defun-prompt-regexp will definitely slow things down anyway because you'll be doing regular expression searches for every line you indent, so you're probably screwed either way!

Another alternative for XEmacs users, is to set the variable c-enable-xemacs-performance-kludge-p to non-nil. This tells CC Mode to use XEmacs-specific built-in functions which, in some circumstances, can locate the top-most opening brace much quicker than beginning-of-defun. Preliminary testing has shown that for styles where these braces are hung (e.g. most JDK-derived Java styles), this hack can improve performance of the core syntax parsing routines from 3 to 60 times. However, for styles which do conform to Emacs' recommended style of putting top-level braces in column zero, this hack can degrade performance by about as much. Thus this variable is set to nil by default, since the Emacs-friendly styles should be more common (and encouraged!). Note that this variable has no effect in Emacs since the necessary built-in functions don't exist (in Emacs 20.2 or 20.3 as of this writing 27-Apr-1998).

You will probably notice pathological behavior from CC Mode when working in files containing large amounts of C preprocessor macros. This is because Emacs cannot skip backwards over these lines as quickly as it can comments.

Previous versions of CC Mode had potential performance problems when recognizing K&R style function argument declarations. This was because there are ambiguities in the C syntax when K&R style argument lists are used(38). CC Mode has adopted BOCM's convention for limiting the search: it assumes that argdecls are indented at least one space, and that the function headers are not indented at all. With current versions of CC Mode, user customization of c-recognize-knr-p is deprecated. Just don't put argdecls in column zero!

You might want to investigate the speed-ups contained in the file `cc-lobotomy.el', which comes as part of the CC Mode distribution, but is completely unsupported. As mentioned previous, CC Mode always trades speed for accuracy, however it is recognized that sometimes you need speed and can sacrifice some accuracy in indentation. The file `cc-lobotomy.el' contains hacks that will "dumb down" CC Mode in some specific ways, making that trade-off of accurancy for speed. I won't go into details of its use here; you should read the comments at the top of the file, and look at the variable cc-lobotomy-pith-list for details.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003