From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Anton Tayanovskyy Newsgroups: gmane.emacs.bugs Subject: bug#47589: [PATCH] 27.1; Freeze on long lines in compilation-shell-minor-mode Date: Sat, 3 Apr 2021 22:52:36 -0400 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="3033"; mail-complaints-to="usenet@ciao.gmane.io" To: 47589@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Apr 04 09:30: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 1lSxD1-0000gO-I2 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 04 Apr 2021 09:30:11 +0200 Original-Received: from localhost ([::1]:55256 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lSxD0-0007BD-KN for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 04 Apr 2021 03:30:10 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:38716) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lSxCt-0007B5-Iq for bug-gnu-emacs@gnu.org; Sun, 04 Apr 2021 03:30:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:51478) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lSxCt-0005KM-Bl for bug-gnu-emacs@gnu.org; Sun, 04 Apr 2021 03:30:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lSxCt-0005pr-58 for bug-gnu-emacs@gnu.org; Sun, 04 Apr 2021 03:30:03 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Anton Tayanovskyy Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 04 Apr 2021 07:30:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 47589 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.161752139222385 (code B ref -1); Sun, 04 Apr 2021 07:30:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 4 Apr 2021 07:29:52 +0000 Original-Received: from localhost ([127.0.0.1]:34791 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lSxCf-0005ov-7M for submit@debbugs.gnu.org; Sun, 04 Apr 2021 03:29:52 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:39718) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lSsse-0007Kb-Aq for submit@debbugs.gnu.org; Sat, 03 Apr 2021 22:52:55 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36138) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lSssd-0002PT-Vx for bug-gnu-emacs@gnu.org; Sat, 03 Apr 2021 22:52:52 -0400 Original-Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]:33559) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lSssc-0002DP-1s for bug-gnu-emacs@gnu.org; Sat, 03 Apr 2021 22:52:51 -0400 Original-Received: by mail-wr1-x429.google.com with SMTP id f12so1974899wro.0 for ; Sat, 03 Apr 2021 19:52:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=I8vpZ8GS2D9tI0l2Od1XNwFk1XugPHQmaW7RYvNvVIo=; b=WUVUoYxdDoRwvbQn98K+Gj07TLNDBpTUeT1MElh7ei0iPwp9JSZad2py7UWwOC8TVM EpMKRO7H6R4eaaENHJotJTgeOmFm+3TFcm2TUNmgZHtXXZRMfBXuhu5d8o3nr9pzW56W cnmsqzuxCB4ueJN5a9G8nGKHmZWDWo5siiFL3ux5EeIRoUFC4kvc9tElfcQVJ+rfD+uD wFesgF/Kd0eARztvpLKspnu0PNqi4iQGQfHyeo4Hdkd2a27H/G4GJnZBLkxM6ocrD3i8 VErfkxdHxBPNSxbdOmQHnOlODyRLQO3+h7TJWxYs6Lqqx2uWEFLZxFZSL+QADw5FLVQW FvQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=I8vpZ8GS2D9tI0l2Od1XNwFk1XugPHQmaW7RYvNvVIo=; b=ug5wBKFW5wn7akLt/KGS6Qa5MG6hUGavNu6+aONfr1JI2ctZ9rUVaHw0YfDCaBeoWJ cRL3lJUjynGVwmwhmdLZNKK3YdMqZsyUPARixpH+EcLUq1XVYNZGLmC5AUt8yxOfzRXh vQ8BitCF/DMoEcMUrjpp4+ejZoS8ZpU/i3h47/bY2JdJ6MPX8Gru5lr+wFJID1pO6KIS W5d+S42yBz9b12Ge/zSw+oUTQvcKe0lGspO9D9Q0nM9APso1QSMTb54020NoM+EqIkot 0lrYs2teZu3hCCodrJ6SqH1h7LO8FZU4iCwMilUQzSwOrXubHeTeSDddPaguaeOQVRDz xFqw== X-Gm-Message-State: AOAM530xfpJXQUDfBwSFIDKcgsDmW2Ybmqtb4u/QYk1T2vc2ovSFViOp T82WSt4tXS9dQudut0zcxhxwTRJpjhbH8mPwHSwhUbtqqd1lSQ== X-Google-Smtp-Source: ABdhPJxG9SnacGBm98egnx2DGAyvx5gFAZ0WmbBptRHIENMhyv6JySrQWB9HUYksP/Gqe5iqeHAKVeBhwD4bAF6UNqI= X-Received: by 2002:a5d:5256:: with SMTP id k22mr22919288wrc.162.1617504766989; Sat, 03 Apr 2021 19:52:46 -0700 (PDT) Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=anton.tayanovskyy@gmail.com; helo=mail-wr1-x429.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Sun, 04 Apr 2021 03:29:47 -0400 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:203519 Archived-At: To reproduce the issue of Emacs freezing on long lines, do the following: ``` M-x shell M-x compilation-shell-minor-mode $ python3 -c 'print("x"*1024*8)' ``` Depending on the machine configuration, make your lines longer. 1024*8 is enough to freeze my MacBook Pro 2019 visibly but I need more characters to slow down an XPS-13 running Ubuntu. In the real world the issue comes up when I'm enjoying my compilation-shell-minor-mode buffer but accidentally cat or print some data with very long lines. Then Emacs freezes and I experience one of my very rare moments of unhappiness with the editor. Using `M-x profiler-start, profiler-stop, profiler-report` the culprit is `compilation-parse-errors`. The function loops over many (about 50) patterns of potential error output and scans the buffer for them repeatedly. The patch edits the function to use a limited version of `re-search-forward`. Instead of finding the pattern everywhere, it only looks for the pattern in the first 1024 chars of every line. It seems plausible that real-world compiler warnings would sit close to the beginning of lines. With the patch the experience (in combination with global-so-long-mode) becomes tolerable; while not lightning fast, Emacs is a lot more responsive in this situation and I as a user retain control to fix the problem, such as comint-clear-buffer etc. FWIW see also the PR https://github.com/emacs-mirror/emacs/pull/24/files --- lisp/progmodes/compile.el | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lisp/progmodes/compile.el b/lisp/progmodes/compile.el index 7a02c3a896..45c69f330c 100644 --- a/lisp/progmodes/compile.el +++ b/lisp/progmodes/compile.el @@ -1535,7 +1535,7 @@ to `compilation-error-regexp-alist' if RULES is nil." (error "HYPERLINK should be an integer: %s" (nth 5 item))) (goto-char start) - (while (re-search-forward pat end t) + (while (compilation--re-search-forward-limited pat end 1024) (when (setq props (compilation-error-properties file line end-line col end-col (or type 2) fmt rule)) @@ -1597,6 +1597,28 @@ to `compilation-error-regexp-alist' if RULES is nil." (match-beginning mn) (match-end mn) 'font-lock-face (cadr props))))))))) +(defun compilation--re-search-forward-limited (regexp bound n) + "Like 're-search-forward limited to the first N chars per line. +This avoids Emacs performance degradation on scanning excessively +long lines of text. It is reasonable when scanning for compiler +warnings to expect to find them early on each line. REGEXP and +BOUND are as in 're-search-forward." + (let ((inhibit-field-text-motion t) + (found nil) + (orig-point (point))) + (while (and (null found) + (< (point) bound) + (not (eobp))) + (setq found (re-search-forward regexp + (max (point) + (min bound (+ (point-at-bol) n))) + t)) + (when (null found) + (forward-line 1))) + (when (null found) + (goto-char orig-point)) + found)) + (defvar-local compilation--parsed -1) (defun compilation--ensure-parse (limit) -- 2.28.0 -------------------------------------------------------------------------------- In GNU Emacs 27.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.21, cairo version 1.16.0) Windowing system distributor 'The X.Org Foundation', version 11.0.12009000 System Description: Ubuntu 20.04.2 LTS Configured using: 'configure --prefix=/nix/store/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-emacs-27.1 --disable-build-details --with-modules --with-x-toolkit=gtk3 --with-xft --with-cairo CFLAGS=-DMAC_OS_X_VERSION_MAX_ALLOWED=101200' Configured features: XPM JPEG TIFF GIF PNG RSVG CAIRO SOUND DBUS GSETTINGS GLIB NOTIFY INOTIFY LIBSELINUX GNUTLS LIBXML2 FREETYPE HARFBUZZ M17N_FLT LIBOTF ZLIB TOOLKIT_SCROLL_BARS GTK3 X11 XDBE XIM MODULES THREADS LIBSYSTEMD JSON PDUMPER GMP