From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#70036: 30.0.50; Move file-truename to the C level Date: Thu, 28 Mar 2024 13:08:45 +0100 Message-ID: <8734say2wi.fsf@thornhill.no> References: <87le63xzjt.fsf@thornhill.no> <87ttkrqvru.fsf@betli.tmit.bme.hu> <87il17xsea.fsf@thornhill.no> <86wmpmhp0a.fsf@gnu.org> <874jcqygpn.fsf@thornhill.no> <875xx6muzt.fsf@ulti.tmit.bme.hu> Reply-To: Theodor Thornhill Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="36283"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , 70036@debbugs.gnu.org To: Felician Nemeth Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Mar 28 13:09:30 2024 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 1rpoZV-0009CR-Dd for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 28 Mar 2024 13:09:29 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rpoZ7-0007PB-Ks; Thu, 28 Mar 2024 08:09:05 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rpoZ4-0007OR-02 for bug-gnu-emacs@gnu.org; Thu, 28 Mar 2024 08:09:02 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rpoZ3-0000Km-LT for bug-gnu-emacs@gnu.org; Thu, 28 Mar 2024 08:09:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rpoZ4-00028Q-9I for bug-gnu-emacs@gnu.org; Thu, 28 Mar 2024 08:09:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 28 Mar 2024 12:09:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70036 X-GNU-PR-Package: emacs Original-Received: via spool by 70036-submit@debbugs.gnu.org id=B70036.17116277418197 (code B ref 70036); Thu, 28 Mar 2024 12:09:02 +0000 Original-Received: (at 70036) by debbugs.gnu.org; 28 Mar 2024 12:09:01 +0000 Original-Received: from localhost ([127.0.0.1]:39288 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rpoZ2-000288-Hi for submit@debbugs.gnu.org; Thu, 28 Mar 2024 08:09:01 -0400 Original-Received: from out-174.mta0.migadu.com ([2001:41d0:1004:224b::ae]:10778) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rpoYz-000279-HI for 70036@debbugs.gnu.org; Thu, 28 Mar 2024 08:08:58 -0400 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thornhill.no; s=key1; t=1711627729; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=WBpMWIZMnBvAJnOO1U8kMO0eLPAy3YaQQaKpYcyJKFQ=; b=x3IljinKolVusJWjgcVxfvv3C2DtsNuBtCe7aDghIwP32VDPnacUsryQO05eSGGKjEwjQy NxcenBW3VckGuyLgapeX/D+QqiConWJhg3ZJYzC7MCKARl0OpdBn6/cYdxn/ckL7mEP6LR rXYbQRhz4Uas3PXCb4WaDciMLo8EPCJKoQZavLQap5DrvpHbkaF3mYNRiWvCw9BHuOpMwJ VZd5nQHBhrJpTXoffMkbVDQ1mLc3SM5eH2y6eeDVuhAm7zp/oGlpC8aXCfLJlQslWwrNeV /WmjrS2lUYew8qWDFpyfeWF3BNLU1XqhR1uYai5myQWwW3BCtvDSaFspeAmmKQ== In-Reply-To: <875xx6muzt.fsf@ulti.tmit.bme.hu> X-Migadu-Flow: FLOW_OUT 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-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:282221 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Felician Nemeth writes: > Theodor Thornhill writes: > >> This wouldn't help for the usage in find-buffer-visiting, though. But >> this one could more easily be replaced by reworking the diagnostics >> handler. We could store the last received diagnostics in the server >> object, and do a quick lookup from known buffers there. > > eglot-handle-notification:textDocument/publishDiagnostics, > eglot--xref-make-match, and eglot--apply-workspace-edit call > find-buffer-visiting. It seems to me only the first case might be > really time sensitive. Theo, can you email me the relevant messages > that your server sends to Emacs? Does the server send lots of similar > diagnostics messages frequently? > > Thanks, > Felici=C3=A1n I'll try to include such a report a little later today. But yes, it happens a lot. Consider the following patch, though. It eliminates the issues for publishDiagnostics. It can easily be extended to the other places where find-buffer-visiting is used. The publishDiagnostics is sent on change from the server, so that isn't directly initiated by Eglot. What do you think? Theo --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Don-t-use-file-truepath-in-Eglot-bug-70036.patch >From d43fe85eae05baa07dd4c3e873a1b94542d97ea9 Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Thu, 28 Mar 2024 12:56:23 +0100 Subject: [PATCH] Don't use file-truepath in Eglot (bug#70036) `file-truepath' is slow because of recursive calls and being implemented in lisp. It seems to not be needed in eglot, but it is used behind the scenes in `find-buffer-visiting', thus appearing in profiles. Moving the implementation to a hash map will yield similar performance benefits, but wouldn't require us to rewrite `file-truename' in C. * lisp/progmodes/eglot.el (eglot-lsp-server): Convert 'managed-buffers' to a hashmap. (eglot-uri-to-path): Don't use file-truepath, as it is too slow to be included in the hot path. (eglot--on-shutdown): Use buffers from buffer map. (eglot--managed-mode): Add buffer to map, rather than list. Also remove it from the map on deactivation. (eglot-handle-notification): Expose server and get buffer from the buffer map. --- lisp/progmodes/eglot.el | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lisp/progmodes/eglot.el b/lisp/progmodes/eglot.el index 7d2f1a55165..c6dbca6fc6a 100644 --- a/lisp/progmodes/eglot.el +++ b/lisp/progmodes/eglot.el @@ -1053,8 +1053,8 @@ eglot-lsp-server :documentation "Map (DIR -> (WATCH ID1 ID2...)) for `didChangeWatchedFiles'." :initform (make-hash-table :test #'equal) :accessor eglot--file-watches) (managed-buffers - :initform nil - :documentation "List of buffers managed by server." + :documentation "Map (PATH -> BUFFER) for buffers managed by server." + :initform (make-hash-table :test #'equal) :accessor eglot--managed-buffers) (saved-initargs :documentation "Saved initargs for reconnection purposes." @@ -1085,12 +1085,12 @@ eglot-uri-to-path (defun eglot-path-to-uri (path) "Convert PATH, a file name, to LSP URI string and return it." - (let ((truepath (file-truename path))) + (let ((expanded-path (expand-file-name path))) (if (and (url-type (url-generic-parse-url path)) ;; It might be MS Windows path which includes a drive ;; letter that looks like a URL scheme (bug#59338) (not (and (eq system-type 'windows-nt) - (file-name-absolute-p truepath)))) + (file-name-absolute-p expanded-path)))) ;; Path is already a URI, so forward it to the LSP server ;; untouched. The server should be able to handle it, since ;; it provided this URI to clients in the first place. @@ -1098,11 +1098,11 @@ eglot-path-to-uri (concat "file://" ;; Add a leading "/" for local MS Windows-style paths. (if (and (eq system-type 'windows-nt) - (not (file-remote-p truepath))) + (not (file-remote-p expanded-path))) "/") (url-hexify-string ;; Again watch out for trampy paths. - (directory-file-name (file-local-name truepath)) + (directory-file-name (file-local-name expanded-path)) eglot--uri-path-allowed-chars))))) (defun eglot-range-region (range &optional markers) @@ -1187,7 +1187,7 @@ eglot--servers-by-xrefed-file (defun eglot--on-shutdown (server) "Called by jsonrpc.el when SERVER is already dead." ;; Turn off `eglot--managed-mode' where appropriate. - (dolist (buffer (eglot--managed-buffers server)) + (dolist (buffer (map-values (eglot--managed-buffers server))) (let (;; Avoid duplicate shutdowns (github#389) (eglot-autoshutdown nil)) (eglot--when-live-buffer buffer (eglot--managed-mode-off)))) @@ -1992,7 +1992,11 @@ eglot--managed-mode (add-hook 'eldoc-documentation-functions #'eglot-signature-eldoc-function nil t) (eldoc-mode 1)) - (cl-pushnew (current-buffer) (eglot--managed-buffers (eglot-current-server)))) + + (let ((buffer (current-buffer))) + (puthash (expand-file-name (buffer-file-name buffer)) + buffer + (eglot--managed-buffers (eglot-current-server))))) (t (remove-hook 'after-change-functions #'eglot--after-change t) (remove-hook 'before-change-functions #'eglot--before-change t) @@ -2020,10 +2024,10 @@ eglot--managed-mode (let ((server eglot--cached-server)) (setq eglot--cached-server nil) (when server - (setf (eglot--managed-buffers server) - (delq (current-buffer) (eglot--managed-buffers server))) + (remhash (expand-file-name (buffer-file-name (current-buffer))) + (eglot--managed-buffers server)) (when (and eglot-autoshutdown - (null (eglot--managed-buffers server))) + (null (map-values (eglot--managed-buffers server)))) (eglot-shutdown server))))))) (defun eglot--managed-mode-off () @@ -2346,7 +2350,7 @@ eglot-handle-notification (remhash token (eglot--progress-reporters server)))))))))) (cl-defmethod eglot-handle-notification - (_server (_method (eql textDocument/publishDiagnostics)) &key uri diagnostics + (server (_method (eql textDocument/publishDiagnostics)) &key uri diagnostics &allow-other-keys) ; FIXME: doesn't respect `eglot-strict-mode' "Handle notification publishDiagnostics." (cl-flet ((eglot--diag-type (sev) @@ -2357,7 +2361,7 @@ eglot-handle-notification (mess (source code message) (concat source (and code (format " [%s]" code)) ": " message))) (if-let* ((path (expand-file-name (eglot-uri-to-path uri))) - (buffer (find-buffer-visiting path))) + (buffer (gethash path (eglot--managed-buffers server)))) (with-current-buffer buffer (cl-loop initially -- 2.40.1 --=-=-=--