|[ < ]||[ > ]||[ << ]||[ Up ]||[ >> ]||[Top]||[Contents]||[Index]||[ ? ]|
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 can cause CC Mode to perform fairly badly. This section gives some insight in how CC Mode operates, how that interacts with some coding styles, and what you can use to improve performance.
The overall goal is that CC Mode shouldn't be overly slow (i.e. take more than a fraction of a second) in any interactive operation. I.e. it's tuned to limit the maximum response time in single operations, which sometimes is at the expense of batch-like operations like reindenting whole blocks. If you find that CC Mode gradually gets slower and slower in certain situations, perhaps as the file grows in size or as the macro or comment you're editing gets bigger, then chances are that something isn't working right. You should consider reporting it, unless it's something that's mentioned in this section.
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 (it's typically an opening or closing parethesis of some kind). The farther this position is from the current insertion point, the slower it gets.
One of the simplest things you can do to reduce scan time, is make sure
any brace that opens a top-level construct(35) always appears in the
leftmost column. This is actually an Emacs constraint, as embodied in
beginning-of-defun function which CC Mode uses heavily. If
you hang 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: The common
style is to hang the opening braces of functions and classes on the
right side of the line, and that doesn't work well with the Emacs
approach. 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(36). 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 relying on
defun-prompt-regexp will definitely slow
things down because (X)Emacs will be doing regular expression searches a
lot, so you'll probably be taking a hit either way!
CC Mode maintains a cache of the opening parentheses of the blocks surrounding the point, and it adapts that cache as the point is moved around. That means that in bad cases it can take noticeable time to indent a line in a new surrounding, but after that it gets fast as long as the point isn't moved far off. The farther the point is moved, the less useful is the cache. Since editing typically is done in "chunks" rather than on single lines far apart from each other, the cache typically gives good performance even when the code doesn't fit the Emacs approach to finding the defun starts.
XEmacs users can set the variable
c-enable-xemacs-performance-kludge-p to non-
tells CC Mode to use XEmacs-specific built-in functions which, in some
circumstances, can locate the top-most opening brace much more quickly 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
21.3 as of this writing in May 2003).
Text properties are used to speed up skipping over syntactic whitespace, i.e. comments and preprocessor directives. Indenting a line after a huge macro definition can be slow the first time, but after that the text properties are in place and it should be fast (even after you've edited other parts of the file and then moved back).
Font locking can be a CPU hog, especially the font locking done on decoration level 3 which tries to be very accurate. Note that that level is designed to be used with a font lock support mode that only fontifies the text that's actually shown, i.e. Lazy Lock or Just-in-time Lock mode, so make sure you use one of them. Fontification of a whole buffer with some thousand lines can often take over a minute. That is a known weakness; the idea is that it never should happen.
The most effective way to speed up font locking is to reduce the
decoration level to 2 by setting
appropriately. That level is designed to be as pretty as possible
without sacrificing performance. See section 7.1 Font Locking Preliminaries, for
|[ << ]||[ >> ]||[Top]||[Contents]||[Index]||[ ? ]|