Commit 519e64f9 authored by Alan Mackenzie's avatar Alan Mackenzie

CC Mode: optimize for repeated simple operations.

Do this by recognising that unterminated strings in a buffer are typically
going to be few and close together.  Also optimize code for C++ attributes.

* lisp/progmodes/cc-defs.el (c-previous-single-property-change): New macro.
(c-put-syn-tab, c-clear-syn-tab): Turned from macros into functions, and moved
to cc-mode.el.
(c-clear-syn-tab-properties): Amended to use c-min/max-syn-tab-mkr.
(c-with-extended-string-fences): Removed.

* lisp/progmodes/cc-engine-el (c-enclosing-c++-attribute): Rewritten for
(c-slow-enclosing-c++-attribute): Removed.
(c-semi-pp-to-literal): Remove a superfluous call to

* lisp/progmodes/cc-mode.el (c-min-syn-tab-mkr, c-max-syn-tab-mkr): two new
marker variables which bound the region occupied by positions with
c-fl-syn-tab text properties.
(c-basic-common-init): Initialize these two variables.
(c-fl-syn-tab-region): Removed.
(c-put-syn-tab, c-clear-syn-tab): Functions moved from cc-defs.el.
(c-clear-string-fences): Amended to use the new scheme.
(c-restore-string-fences): Now takes no arguments; amended to use the new
(c-font-lock-fontify-region): Amended to use the new scheme.
parent d0872638
Pipeline #5966 failed with stage
in 13 seconds
......@@ -445,6 +445,15 @@ to it is returned. This function does not modify the point or the mark."
;; Emacs and earlier XEmacs
`(next-single-property-change ,position ,prop ,object ,limit)))
(defmacro c-previous-single-property-change (position prop &optional object limit)
;; See the doc string for either of the defuns expanded to.
(if (and c-use-extents
(fboundp 'previous-single-char-property-change))
;; XEmacs >= 2005-01-25
`(previous-single-char-property-change ,position ,prop ,object ,limit)
;; Emacs and earlier XEmacs
`(previous-single-property-change ,position ,prop ,object ,limit)))
(defmacro c-region-is-active-p ()
;; Return t when the region is active. The determination of region
;; activeness is different in both Emacs and XEmacs.
......@@ -1047,15 +1056,6 @@ MODE is either a mode symbol or a list of mode symbols."
;; properties set on a single character and that never spread to any
;; other characters.
(defmacro c-put-syn-tab (pos value)
;; Set both the syntax-table and the c-fl-syn-tab text properties at POS to
;; VALUE (which should not be nil).
`(let ((-pos- ,pos)
(-value- ,value))
(c-put-char-property -pos- 'syntax-table -value-)
(c-put-char-property -pos- 'c-fl-syn-tab -value-)
(c-truncate-lit-pos-cache -pos-)))
;; Constant used at compile time to decide whether or not to use
;; XEmacs extents. Check all the extent functions we'll use since
......@@ -1183,13 +1183,6 @@ MODE is either a mode symbol or a list of mode symbols."
;; Emacs < 21.
`(c-clear-char-property-fun ,pos ',property))))
(defmacro c-clear-syn-tab (pos)
;; Remove both the 'syntax-table and `c-fl-syn-tab properties at POS.
`(let ((-pos- ,pos))
(c-clear-char-property -pos- 'syntax-table)
(c-clear-char-property -pos- 'c-fl-syn-tab)
(c-truncate-lit-pos-cache -pos-)))
(defmacro c-min-property-position (from to property)
;; Return the first position in the range [FROM to) where the text property
;; PROPERTY is set, or `most-positive-fixnum' if there is no such position.
......@@ -1235,8 +1228,18 @@ MODE is either a mode symbol or a list of mode symbols."
;; Remove all occurrences of the `syntax-table' and `c-fl-syn-tab' text
;; properties between FROM and TO.
`(let ((-from- ,from) (-to- ,to))
(c-clear-char-properties -from- -to- 'syntax-table)
(c-clear-char-properties -from- -to- 'c-fl-syn-tab)))
(when (and
c-min-syn-tab-mkr c-max-syn-tab-mkr
(< -from- c-max-syn-tab-mkr)
(> -to- c-min-syn-tab-mkr))
(let ((pos -from-))
(while (and
(< pos -to-)
(setq pos (c-min-property-position pos -to- 'c-fl-syn-tab))
(< pos -to-))
(c-clear-syn-tab pos)
(setq pos (1+ pos)))))
(c-clear-char-properties -from- -to- 'syntax-table)))
(defmacro c-search-forward-char-property (property value &optional limit)
"Search forward for a text-property PROPERTY having value VALUE.
......@@ -1456,28 +1459,6 @@ with value CHAR in the region [FROM to)."
(c-put-char-property (point) ,property ,value)
(defmacro c-with-extended-string-fences (beg end &rest body)
;; If needed, extend the region with "mirrored" c-fl-syn-tab properties to
;; contain the region (BEG END), then evaluate BODY. If this mirrored
;; region was initially empty, restore it afterwards.
`(let ((-beg- ,beg)
(-end- ,end)
((null c-fl-syn-tab-region)
(c-restore-string-fences -beg- -end-)
((and (>= -beg- (car c-fl-syn-tab-region))
(<= -end- (cdr c-fl-syn-tab-region)))
(t ; Crudely extend the mirrored region.
(setq -beg- (min -beg- (car c-fl-syn-tab-region))
-end- (max -end- (cdr c-fl-syn-tab-region)))
(c-restore-string-fences -beg- -end-)
;; Macros to put overlays (Emacs) or extents (XEmacs) on buffer text.
;; For our purposes, these are characterized by being possible to
......@@ -163,7 +163,9 @@
(defvar c-doc-line-join-re)
(defvar c-doc-bright-comment-start-re)
(defvar c-doc-line-join-end-ch)
(defvar c-fl-syn-tab-region)
(cc-bytecomp-defvar c-min-syn-tab-mkr)
(cc-bytecomp-defvar c-max-syn-tab-mkr)
(cc-bytecomp-defun c-clear-syn-tab)
(cc-bytecomp-defun c-clear-string-fences)
(cc-bytecomp-defun c-restore-string-fences)
......@@ -1910,52 +1912,29 @@ comment at the start of cc-engine.el for more info."
(defun c-enclosing-c++-attribute ()
;; If we're in C++ Mode, and point is within a correctly balanced [[ ... ]]
;; attribute structure, return a cons of its starting and ending positions.
;; Otherwise, return nil. We use the c-{in,is}-sws-face text properties for
;; this determination, this macro being intended only for use in the *-sws-*
;; functions and macros. The match data are NOT preserved over this macro.
(let (attr-end pos-is-sws)
(c-major-mode-is 'c++-mode)
(> (point) (point-min))
(setq pos-is-sws
(if (get-text-property (1- (point)) 'c-is-sws)
(1- (point))
(1- (previous-single-property-change
(point) 'c-is-sws nil (point-min)))))
(goto-char pos-is-sws)
(setq attr-end (c-looking-at-c++-attribute)))
(> attr-end (point))
(cons pos-is-sws attr-end))))
(defun c-slow-enclosing-c++-attribute ()
;; Like `c-enclosing-c++-attribute', but does not depend on the c-i[ns]-sws
;; properties being set.
;; Otherwise, return nil.
(c-major-mode-is 'c++-mode)
(let ((paren-state (c-parse-state))
(let ((lim (max (- (point) 200) (point-min)))
(setq cand
(catch 'found-cand
(while (cdr paren-state)
(when (and (numberp (car paren-state))
(numberp (cadr paren-state))
(eq (car paren-state)
(1+ (cadr paren-state)))
(eq (char-after (car paren-state)) ?\[)
(eq (char-after (cadr paren-state)) ?\[))
(throw 'found-cand (cadr paren-state)))
(setq paren-state (cdr paren-state)))))
(and cand
(and (c-go-list-forward cand)
(eq (char-before) ?\])
(eq (char-before (1- (point))) ?\])))))
(setq paren-state (cdr paren-state)))
(and cand (cons cand (point)))))))
(skip-chars-backward "^[;{}" lim)
(eq (char-before) ?\[))
(not (eq (char-before (1- (point))) ?\[))
(> (point) lim))
(and (eq (char-before) ?\[)
(eq (char-before (1- (point))) ?\[)
(progn (backward-char 2) t)
(setq cand (point))
(c-go-list-forward nil (min (+ (point) 200) (point-max)))
(eq (char-before) ?\])
(eq (char-before (1- (point))) ?\])
(not (c-literal-limits))
(cons cand (point)))))))
(defun c-invalidate-sws-region-before (beg end)
;; Called from c-before-change. BEG and END are the bounds of the change
......@@ -3003,9 +2982,7 @@ comment at the start of cc-engine.el for more info."
(and (nth 4 s) (nth 7 s) ; Line comment
(not (memq (char-before here) '(?\\ ?\n)))))))
pos here
(setq s (parse-partial-sexp pos here nil nil s))))
(setq s (parse-partial-sexp pos here nil nil s)))
(when (not (eq near-pos here))
(c-semi-put-near-cache-entry here s))
This diff is collapsed.
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment