From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ihor Radchenko Newsgroups: gmane.emacs.bugs Subject: bug#64735: 29.0.92; find invocations are ~15x slower because of ignores Date: Fri, 21 Jul 2023 13:17:17 +0000 Message-ID: <87v8edl7z6.fsf@localhost> References: <1fd5e3ed-e1c3-5d6e-897f-1d5d55e379fa@gutov.dev> <87wmyupvlw.fsf@localhost> <5c4d9bea-3eb9-b262-138a-4ea0cb203436@gutov.dev> <87tttypp2e.fsf@localhost> <837cqu35lw.fsf@gnu.org> <87o7k6pmk3.fsf@localhost> <834jly351p.fsf@gnu.org> <87lefapkdx.fsf@localhost> <831qh230h5.fsf@gnu.org> <87wmyu8mi0.fsf@localhost> <83wmyu1l1k.fsf@gnu.org> <87fs5hemi1.fsf@gmx.de> <83edl11qzn.fsf@gnu.org> <874jlxebz5.fsf@gmx.de> <87lef9mqio.fsf@localhost> <87edl1scbw.fsf@gmx.de> <87fs5hmp6i.fsf@localhost> <87cz0lmoxy.fsf@localhost> <83v8edzb31.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="7738"; mail-complaints-to="usenet@ciao.gmane.io" Cc: dmitry@gutov.dev, michael.albinus@gmx.de, 64735@debbugs.gnu.org, sbaugh@janestreet.com To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Jul 21 15:18:21 2023 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 1qMq1T-0001mC-Fi for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 21 Jul 2023 15:18:21 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qMq1J-0003qL-6L; Fri, 21 Jul 2023 09:18:09 -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 1qMq1C-0003q2-Lq for bug-gnu-emacs@gnu.org; Fri, 21 Jul 2023 09:18:03 -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 1qMq1C-0003rU-Dp for bug-gnu-emacs@gnu.org; Fri, 21 Jul 2023 09:18:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qMq1C-0001Ai-96 for bug-gnu-emacs@gnu.org; Fri, 21 Jul 2023 09:18:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Ihor Radchenko Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 21 Jul 2023 13:18:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 64735 X-GNU-PR-Package: emacs Original-Received: via spool by 64735-submit@debbugs.gnu.org id=B64735.16899454434451 (code B ref 64735); Fri, 21 Jul 2023 13:18:02 +0000 Original-Received: (at 64735) by debbugs.gnu.org; 21 Jul 2023 13:17:23 +0000 Original-Received: from localhost ([127.0.0.1]:60911 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qMq0Y-00019h-0L for submit@debbugs.gnu.org; Fri, 21 Jul 2023 09:17:22 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]:56085) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qMq0O-00019M-3T for 64735@debbugs.gnu.org; Fri, 21 Jul 2023 09:17:21 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 8E5DE240105 for <64735@debbugs.gnu.org>; Fri, 21 Jul 2023 15:17:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1689945426; bh=YVjgHITeyH1oF6ZfygheH9eD2bdEs4ri7WCKlYXrJ1Y=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:From; b=lIBF0dr9Y7Ag25IOlo1QBA3kDUkL8pMR7krtr2lPleYJ/tiy6Rpbj0Tqa+eB1ILmG gSb8OhOmMgMHdmTDYn39+8Lbwlsbv/G/Zgj4yw/U6XpNGTwopqb1ZBns7z7saOsBGn Bj/LBYBRn8KNP7q4+lDr13dp+apHcw+z5BH19kULb/rHlPKJL7nVDfvkA21bZlTq/J dzRamagX4YbkrCs4PVSTr2UUXdS3w0r4VK8BN+2ZkcYrMxFYMDX0qB+3RxuKT3uEEj 1B0GiY1WoCJs8u/XqEcJ+Id6e1HtlJqsOZPneFj7K/Ct5xZuGEijYIm0cP0sniYJpo WiyEtJwBa/aoA== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4R6qpT5rtQz6txC; Fri, 21 Jul 2023 15:17:05 +0200 (CEST) In-Reply-To: <83v8edzb31.fsf@gnu.org> 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:265704 Archived-At: Eli Zaretskii writes: >> > The question is: what is more costly >> > (a) matching complex regexp && call function or >> > (b) call function (lambda (fn) (when (and foo (match-string- ... fn)) ...)) >> >> (benchmark-run-compiled 10000000 (string-match-p (caar file-name-handler-alist) "/path/to/very/deep/file")) >> ;; => (1.495432981 0 0.0) >> (benchmark-run-compiled 10000000 (funcall (lambda (fn) (and nil (string-match-p (caar file-name-handler-alist) fn))) "/path/to/very/deep/file")) >> ;; => (0.42053276500000003 0 0.0) >> >> Looks like even funcall overheads are not as bad as invoking regexp search. > > But "nil" is not a faithful emulation of the real test which will have > to be put there, is it? It is, at least in some cases. In other cases, it is list lookup, which is also faster: (benchmark-run-compiled 10000000 (funcall (lambda (fn) (and (get 'foo 'jka-compr) (string-match-p (caar file-name-handler-alist) fn))) "/path/to/very/deep/file")) ;; => (0.5831819149999999 0 0.0) Let me go through default handlers one by one: file-name-handler-alist is a variable defined in fileio.c. Value (("..." . jka-compr-handler) (".." . epa-file-handler) ("..." . tramp-archive-file-name-handler) ("..." . tramp-completion-file-name-handler) ("..." . tramp-file-name-handler) ("\\`/:" . file-name-non-special)) ---- 1 ----- (defun jka-compr-handler (operation &rest args) (save-match-data (let ((jka-op (get operation 'jka-compr))) (if (and jka-op (not jka-compr-inhibit)) (apply jka-op args) (jka-compr-run-real-handler operation args))))) skips when `get' fails, and also puts unnecessary `save-match-data' call, which would better be inside if. ---- 2 ----- (defun epa-file-handler (operation &rest args) (save-match-data (let ((op (get operation 'epa-file))) (if (and op (not epa-inhibit)) (apply op args) (epa-file-run-real-handler operation args))))) again checks `get' and also epa-inhitbit. (and again, `save-match-data' only needed for (apply op args)). Side note: These handlers essentially force double handler lookup without skipping already processed handlers when they decide that they need to delegate to defaults. ---- 3 ----- (if (not tramp-archive-enabled) ;; Unregister `tramp-archive-file-name-handler'. (progn (tramp-register-file-name-handlers) (tramp-archive-run-real-handler operation args)) <...> Note how this tries to remove itself from handler list, by testing a boolean variable (nil by default!). However, this "self-removal" will never happen unless we happen to query a file with matching regexp. If no archive file is accessed during Emacs session (as it is the case for me), this branch of code will never be executed and I am doomed to have Emacs checking for regexp in front of this handler forever. ------ 4 ------ (defun tramp-completion-file-name-handler (operation &rest args) "Invoke Tramp file name completion handler for OPERATION and ARGS. Falls back to normal file name handler if no Tramp file name handler exists." (if-let ((fn (and tramp-mode minibuffer-completing-file-name (assoc operation tramp-completion-file-name-handler-alist)))) (save-match-data (apply (cdr fn) args)) (tramp-run-real-handler operation args))) is checking for tramp-mode (t by default) and minibuffer-completing-file-name (often nil). -------- 5 -------- (defun tramp-file-name-handler (operation &rest args) "Invoke Tramp file name handler for OPERATION and ARGS. Fall back to normal file name handler if no Tramp file name handler exists." (let ((filename (apply #'tramp-file-name-for-operation operation args)) <...> (if (tramp-tramp-file-p filename) ;; <<--- always nil when tramp-mode is nil ;; When `tramp-mode' is not enabled, or the file name is quoted, ;; we don't do anything. (tramp-run-real-handler operation args)) this one is more complex, but does nothing when tramp-mode is nil. --------- 6 ------- file-name-non-special is complex. The only thing I noticed is that it binds tramp-mode as (let ((tramp-mode (and tramp-mode (eq method 'local-copy)))) So, other handlers checking for tramp-mode variable early would benefit if they were able to do so. -- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at . Support Org development at , or support my work at