From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Spencer Baugh Newsgroups: gmane.emacs.bugs Subject: bug#44638: [PATCH 1/2] autorevert: don't reuse existing watch descriptors Date: Sat, 14 Nov 2020 11:54:58 -0500 Message-ID: <20201114165459.9518-1-sbaugh@catern.com> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19521"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Spencer Baugh To: 44638@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Nov 14 17:57:16 2020 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 1kdyrT-0004y2-Ja for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 14 Nov 2020 17:57:15 +0100 Original-Received: from localhost ([::1]:38102 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kdyrS-000507-Ei for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 14 Nov 2020 11:57:14 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48942) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kdyqI-00041e-Um for bug-gnu-emacs@gnu.org; Sat, 14 Nov 2020 11:56:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:40296) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kdyqI-0001t9-Hj for bug-gnu-emacs@gnu.org; Sat, 14 Nov 2020 11:56:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kdyqI-0001pS-HH for bug-gnu-emacs@gnu.org; Sat, 14 Nov 2020 11:56:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Spencer Baugh Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 14 Nov 2020 16:56:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 44638 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.16053729296973 (code B ref -1); Sat, 14 Nov 2020 16:56:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 14 Nov 2020 16:55:29 +0000 Original-Received: from localhost ([127.0.0.1]:51839 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kdypk-0001oP-Tg for submit@debbugs.gnu.org; Sat, 14 Nov 2020 11:55:29 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:52354) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kdypj-0001oI-J2 for submit@debbugs.gnu.org; Sat, 14 Nov 2020 11:55:28 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48812) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kdypj-0003mp-5w for bug-gnu-emacs@gnu.org; Sat, 14 Nov 2020 11:55:27 -0500 Original-Received: from venus.catern.com ([68.183.49.163]:44370) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kdyph-0001Z7-B7 for bug-gnu-emacs@gnu.org; Sat, 14 Nov 2020 11:55:26 -0500 Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=98.7.229.235; helo=localhost; envelope-from=sbaugh@catern.com; receiver= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=catern.com; s=mail; t=1605372918; bh=j1YTF7lOISx1maAVlzOHc7VboheFzi23smuHBasUhzQ=; h=From:To:Cc:Subject:Date; b=g+/sM9nINmvrH2USGsYZZ8FZu5PIcZpKQbDf7tejT/p2oUoUOJawLhYGzLCWVC3ON LIuN5AAr/itYVurqGgV+9E4QHxDelps8iBTYFSQJYI8ZMgMq6obLHm1A0AK47MqFRE wGuhxIPwqaVHD1dbIiBl9VrUbEz+VHLzacBuo1A8= Original-Received: from localhost (cpe-98-7-229-235.nyc.res.rr.com [98.7.229.235]) by venus.catern.com (Postfix) with ESMTPSA id 285362DD0B1; Sat, 14 Nov 2020 16:55:18 +0000 (UTC) X-Mailer: git-send-email 2.28.0 Received-SPF: pass client-ip=68.183.49.163; envelope-from=sbaugh@catern.com; helo=venus.catern.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/11/14 11:55:18 X-ACL-Warn: Detected OS = Linux 3.11 and newer 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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:193309 Archived-At: Previously, when enabling autorevert for a new buffer, we would search the buffers already registered with autorevert to see if any of them had the same filename. This is very slow with a large number of buffers - with 1000, it takes 2 seconds on my system. This 2-second overhead is paid for every new file opened. But this is an unnecesary optimization; registering the same file twice with file-notify has minimal or no overhead, depending on the implementation. In fact, file-notify has some baked-in overhead to support registering the same file twice without problems. For example, inotify on Linux returns the same inotify watch descriptor when the same file is registered twice; file-notify adds an additional uniquifying id so that all watch descriptors are unique in Emacs, even with inotify. We can rely on file-notify's existing support for handling the same file being registered twice. We don't need this slow and complex logic. With this code deleted, enabling autorevert for a new buffer is essentially instant even with 1000 buffers. --- lisp/autorevert.el | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/lisp/autorevert.el b/lisp/autorevert.el index 046ea2b5d6..d5bb75c2f1 100644 --- a/lisp/autorevert.el +++ b/lisp/autorevert.el @@ -650,30 +650,15 @@ will use an up-to-date value of `auto-revert-interval'." (string-match auto-revert-notify-exclude-dir-regexp (expand-file-name default-directory)) (file-symlink-p (or buffer-file-name default-directory))) - ;; Check, whether this has been activated already. (let ((file (if buffer-file-name (expand-file-name buffer-file-name default-directory) (expand-file-name default-directory)))) - (maphash - (lambda (key _value) - (when (and - (file-notify-valid-p key) - (equal (file-notify--watch-absolute-filename - (gethash key file-notify-descriptors)) - (directory-file-name file)) - (equal (file-notify--watch-callback - (gethash key file-notify-descriptors)) - 'auto-revert-notify-handler)) - (setq auto-revert-notify-watch-descriptor key))) - auto-revert--buffers-by-watch-descriptor) - ;; Create a new watch if needed. - (unless auto-revert-notify-watch-descriptor - (setq auto-revert-notify-watch-descriptor - (ignore-errors - (file-notify-add-watch - file - (if buffer-file-name '(change attribute-change) '(change)) - 'auto-revert-notify-handler)))) + (setq auto-revert-notify-watch-descriptor + (ignore-errors + (file-notify-add-watch + file + (if buffer-file-name '(change attribute-change) '(change)) + 'auto-revert-notify-handler)))) (when auto-revert-notify-watch-descriptor (setq auto-revert-notify-modified-p t) (puthash @@ -682,7 +667,7 @@ will use an up-to-date value of `auto-revert-interval'." (gethash auto-revert-notify-watch-descriptor auto-revert--buffers-by-watch-descriptor)) auto-revert--buffers-by-watch-descriptor) - (add-hook 'kill-buffer-hook #'auto-revert-notify-rm-watch nil t))))) + (add-hook 'kill-buffer-hook #'auto-revert-notify-rm-watch nil t)))) ;; If we have file notifications, we want to update the auto-revert buffers ;; immediately when a notification occurs. Since file updates can happen very -- 2.28.0