From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: David Koppelman Newsgroups: gmane.emacs.bugs Subject: bug#51692: 29.0.50; High CPU in c++ mode. Type finder? Date: Thu, 11 Nov 2021 14:13:48 -0600 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="5536"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) Cc: Yuri D'Elia , Lars Ingebrigtsen , Zhiwei Chen , 51692@debbugs.gnu.org To: Alan Mackenzie Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Nov 11 21:14:12 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mlGSZ-0001Iq-TF for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 11 Nov 2021 21:14:11 +0100 Original-Received: from localhost ([::1]:59410 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mlGSY-0001Rq-NP for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 11 Nov 2021 15:14:10 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:54444) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mlGSR-0001Qh-6i for bug-gnu-emacs@gnu.org; Thu, 11 Nov 2021 15:14:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:59073) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mlGSQ-0004Nb-MJ for bug-gnu-emacs@gnu.org; Thu, 11 Nov 2021 15:14:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mlGSQ-0002pb-I0 for bug-gnu-emacs@gnu.org; Thu, 11 Nov 2021 15:14:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: David Koppelman Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 11 Nov 2021 20:14:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51692 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: moreinfo confirmed Original-Received: via spool by 51692-submit@debbugs.gnu.org id=B51692.163666164010864 (code B ref 51692); Thu, 11 Nov 2021 20:14:02 +0000 Original-Received: (at 51692) by debbugs.gnu.org; 11 Nov 2021 20:14:00 +0000 Original-Received: from localhost ([127.0.0.1]:42386 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mlGSN-0002pA-LE for submit@debbugs.gnu.org; Thu, 11 Nov 2021 15:14:00 -0500 Original-Received: from relay.lsu.edu ([130.39.6.46]:54804) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mlGSM-0002ou-1l for 51692@debbugs.gnu.org; Thu, 11 Nov 2021 15:13:58 -0500 Original-Received: from cyc.ece.lsu.edu (cyc.ece.lsu.edu [96.125.115.182]) by relay.lsu.edu (Postfix) with ESMTPS id E85BE2198B30; Thu, 11 Nov 2021 14:13:48 -0600 (CST) In-Reply-To: (Alan Mackenzie's message of "Thu, 11 Nov 2021 20:00:43 +0000") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:219683 Archived-At: I applied the patch and it fixes the problem! I applied the patch to the checkout on which I reported the problem (I didn't try to update). Also, I tested both on the reduced testcase and the original file, on both there was no suspiciously high CPU usage. Thank you! Alan Mackenzie writes: > Hello, David, Zhiwei, Yuri, and Lars. > > On Mon, Nov 08, 2021 at 13:00:06 -0600, David Koppelman wrote: > >> Load the attached file into a buffer: > >> ./src/emacs --no-init d.cc > >> Emacs will use 100% CPU on a core while the buffer is visible. CPU usage >> goes to normal when switching to another buffer. The attached file is a >> reduced version of a much larger file. (The larger file experiences the >> high CPU usage only while a certain portion of the code is visible.) > > Yes, there is a bug here. CC Mode is expected to use a high amount of > CPU time (but not 100%) for a few seconds after starting C (etc.) Mode, > or for a few minutes after starting Emacs when there's a desktop with a > lot of CC Mode files in it. > > However, with C++ files (and likely Java files, too), a problem with > templates caused this 100% usage. > > I'm hoping the following patch will fix it. Could you (plural) please > apply this patch to the Emacs master branch, byte compile the two > amended files, then load the fixed CC Mode into your Emacs. Then please > try it out with your real files. (If anybody wants any help with the > patching or byte compiling, feel free to send me personal email.) > > Then please report back to the bug list confirming that the bug is > fixed, or telling me what's still not right. > > Thank you! > > > > diff -r 05fd00cb5937 cc-engine.el > --- a/cc-engine.el Sat Oct 30 15:57:26 2021 +0000 > +++ b/cc-engine.el Thu Nov 11 18:47:47 2021 +0000 > @@ -6838,6 +6838,13 @@ > (defvar c-found-types nil) > (make-variable-buffer-local 'c-found-types) > > +;; Dynamically bound variable that instructs `c-forward-type' to > +;; record the ranges of types that only are found. Behaves otherwise > +;; like `c-record-type-identifiers'. Also when this variable is non-nil, > +;; `c-fontify-new-found-type' doesn't get called (yet) for the purported > +;; type. > +(defvar c-record-found-types nil) > + > (defsubst c-clear-found-types () > ;; Clears `c-found-types'. > (setq c-found-types > @@ -6851,7 +6858,10 @@ > (let ((type (c-syntactic-content from to c-recognize-<>-arglists))) > (unless (gethash type c-found-types) > (puthash type t c-found-types) > - (when (and (eq (string-match c-symbol-key type) 0) > + (when (and (not c-record-found-types) ; Only call `c-fontify-new-fount-type' > + ; when we haven't "bound" c-found-types > + ; to itself in c-forward-<>-arglist. > + (eq (string-match c-symbol-key type) 0) > (eq (match-end 0) (length type))) > (c-fontify-new-found-type type))))) > > @@ -8248,11 +8258,6 @@ > (setq c-record-ref-identifiers > (cons range c-record-ref-identifiers)))))) > > -;; Dynamically bound variable that instructs `c-forward-type' to > -;; record the ranges of types that only are found. Behaves otherwise > -;; like `c-record-type-identifiers'. > -(defvar c-record-found-types nil) > - > (defmacro c-forward-keyword-prefixed-id (type) > ;; Used internally in `c-forward-keyword-clause' to move forward > ;; over a type (if TYPE is 'type) or a name (otherwise) which > @@ -8480,6 +8485,11 @@ > (c-forward-<>-arglist-recur all-types))) > (progn > (when (consp c-record-found-types) > + (let ((cur c-record-found-types)) > + (while (consp (car-safe cur)) > + (c-fontify-new-found-type > + (buffer-substring-no-properties (caar cur) (cdar cur))) > + (setq cur (cdr cur)))) > (setq c-record-type-identifiers > ;; `nconc' doesn't mind that the tail of > ;; `c-record-found-types' is t. > @@ -9203,6 +9213,12 @@ > > (when (and (eq res t) > (consp c-record-found-types)) > + ;; Cause the confirmed types to get fontified. > + (let ((cur c-record-found-types)) > + (while (consp (car-safe cur)) > + (c-fontify-new-found-type > + (buffer-substring-no-properties (caar cur) (cdar cur))) > + (setq cur (cdr cur)))) > ;; Merge in the ranges of any types found by the second > ;; `c-forward-type'. > (setq c-record-type-identifiers > diff -r 05fd00cb5937 cc-fonts.el > --- a/cc-fonts.el Sat Oct 30 15:57:26 2021 +0000 > +++ b/cc-fonts.el Thu Nov 11 18:47:47 2021 +0000 > @@ -105,6 +105,7 @@ > (cc-bytecomp-defun c-font-lock-objc-method) > (cc-bytecomp-defun c-font-lock-invalid-string) > (cc-bytecomp-defun c-before-context-fl-expand-region) > +(cc-bytecomp-defun c-font-lock-fontify-region) > > > ;; Note that font-lock in XEmacs doesn't expand face names as > @@ -2431,6 +2432,7 @@ > (defun c-force-redisplay (start end) > ;; Force redisplay immediately. This assumes `font-lock-support-mode' is > ;; 'jit-lock-mode. Set the variable `c-re-redisplay-timer' to nil. > + (save-excursion (c-font-lock-fontify-region start end)) > (jit-lock-force-redisplay (copy-marker start) (copy-marker end)) > (setq c-re-redisplay-timer nil)) > > @@ -2458,7 +2460,6 @@ > (dolist (win-boundary window-boundaries) > (when (and (< (match-beginning 0) (cdr win-boundary)) > (> (match-end 0) (car win-boundary)) > - (c-get-char-property (match-beginning 0) 'fontified) > (not c-re-redisplay-timer)) > (setq c-re-redisplay-timer > (run-with-timer 0 nil #'c-force-redisplay