unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Dmitry Gutov <dgutov@yandex.ru>
To: "João Távora" <joaotavora@gmail.com>
Cc: Stefan Monnier <monnier@iro.umontreal.ca>, 48841@debbugs.gnu.org
Subject: bug#48841: fido-mode is slower than ido-mode with similar settings
Date: Fri, 11 Jun 2021 05:19:03 +0300	[thread overview]
Message-ID: <858682b2-b8fd-898b-bef3-97dbe5e4debc@yandex.ru> (raw)
In-Reply-To: <CALDnm51En25oyL8i+g-_oxBtCgmts0skjqwNE8hBn66k_DyV_g@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3655 bytes --]

On 07.06.2021 11:52, João Távora wrote:

>     Maybe moving all of them to parameters and return values (making it a
>     static function and having the caller manage state) would help, I
>     haven't tried that exactly.
> 
> 
> Normally, in those adventures you end up with the same allocations 
> somewhere else, and uglier code. But you can try.

I have it a try, with little success (patch attached, for posterity). 
Since there's no multiple value returns in Lisp, I had to define a 
container for the three values.

The performance is basically the same, which seems to indicate that 
either Elisp has to allocate very little for a compiled lambda code, or 
it's optimized out (which would also make sense: the only thing 
necessary for it is a new container for the current scope).

>      > Might be noise, or you might be thrashing of CPU caches, who
>     knows? If
>      > the string length is on the same cache line as the contents of the
>      > string you're reading, then evicting that to go read the value of a
>      > boxed integer somewhere radically different is slow.
> 
>     But the string value is boxed as well, right? 
> 
> 
> The key is locality. If the string length and data happen to live nearby 
> in memory (in the same box, so to speak), there's a decent chance that 
> reading one brings the other into the cache, and you get a nice hit 
> depending on your subsequent operation.
> 
> Here I'm just speculating, as I said. In managed languages such as 
> Lisps, it's somewhat unpredictable. It's also always hardware dependent.
> Though given C/C++, a known processor and the right application, this 
> will make a world of a difference, and will yield truly "weird" results 
> (which arent weird at all after you understand the logic). Like, for 
> example a vector being much better at sorted insertion than a linked 
> list. (!) Look it up. Bjarne Stroustrup has one of those talks.

When you have to do some work, better memory locality can indeed change 
a lot. But in this case we have an already computed value vs. something 
the code still needs to compute, however fast that is.

Accessing function arguments must be currently much faster than looking 
up the current scope defined with 'let'.

Anyway, looking at what else could be removed, now that the extra 
allocation in 'match-data' is gone, what really speeds it up 2x-11x 
(depending on whether GC kicks in, but it more often doesn't), is 
commenting out the line:

   (setq str (copy-sequence str))

So if it were possible to rearrange completion-pcm--hilit-commonality 
not to have to modify the strings (probably removing the function 
altogether?), that would improve the potential performance of c-a-p-f 
quite a bit, for fido-mode and other frontends (depending on how much 
overhead the other layers add).

Ultimately, the scoring information doesn't have to live in the text 
properties. For sorting, the frontend could allocate a hash table, then 
ask the [backends? styles?] for completion scores on each item and sort 
based on that. Since faces are needed only for the completions that are 
currently displayed, even having to repeat the regexp matching stuff for 
each of them later would be no big deal performance-wise, compared to 
the current approach.

Anyway, these are musing for the much-discussed future iteration of the 
API. With the current version, and tied by backward compatibility, it 
might be possible to wring 10ms of improvement by consolidating text 
property changes somehow, but likely no more than that.

Looking forward for your analysis of fido-vertical-mode's performance 
improvement over the "normal" one.

[-- Attachment #2: completion-pcm-score-struct.diff --]
[-- Type: text/x-patch, Size: 4525 bytes --]

diff --git a/lisp/minibuffer.el b/lisp/minibuffer.el
index d5a0118b7c..0cb247fc19 100644
--- a/lisp/minibuffer.el
+++ b/lisp/minibuffer.el
@@ -3485,6 +3485,7 @@ completion-pcm--hilit-commonality
     (let* ((re (completion-pcm--pattern->regex pattern 'group))
            (point-idx (completion-pcm--pattern-point-idx pattern))
            (case-fold-search completion-ignore-case)
+           (score (make-vector 3 0))
            last-md)
       (mapcar
        (lambda (str)
@@ -3531,37 +3532,17 @@ completion-pcm--hilit-commonality
                 ;;    (SUM_across_i(hole_i_contrib) + 1) * len
                 ;;
                 ;; , where "len" is the string's length.
-                (score-numerator 0)
-                (score-denominator 0)
-                (last-b 0)
-                (update-score-and-face
-                 (lambda (a b)
-                   "Update score and face given match range (A B)."
-                   (add-face-text-property a b
-                                           'completions-common-part
-                                           nil str)
-                   (setq
-                    score-numerator   (+ score-numerator (- b a)))
-                   (unless (or (= a last-b)
-                               (zerop last-b)
-                               (= a (length str)))
-                     (setq
-                      score-denominator (+ score-denominator
-                                           1
-                                           (expt (- a last-b 1)
-                                                 (/ 1.0
-                                                    flex-score-match-tightness)))))
-                   (setq
-                    last-b              b))))
+                )
+           (fillarray score 0)
            (while md
-             (funcall update-score-and-face from (pop md))
+             (completion-pcm--update-score-and-face str from (pop md) score)
              (setq from (pop md)))
            ;; If `pattern' doesn't have an explicit trailing any, the
            ;; regex `re' won't produce match data representing the
            ;; region after the match.  We need to account to account
            ;; for that extra bit of match (bug#42149).
            (unless (= from match-end)
-             (funcall update-score-and-face from match-end))
+             (completion-pcm--update-score-and-face str from match-end score))
            (if (> (length str) pos)
                (add-face-text-property
                 pos (1+ pos)
@@ -3570,10 +3551,35 @@ completion-pcm--hilit-commonality
            (unless (zerop (length str))
              (put-text-property
               0 1 'completion-score
-              (/ score-numerator (* end (1+ score-denominator)) 1.0) str)))
+              (/ (completion-pcm--score-numerator score)
+                 (* end (1+ (completion-pcm--score-denominator score)))
+                 1.0)
+              str)))
          str)
        completions))))
 
+(cl-defstruct (completion-pcm--score (:type vector))
+  (numerator 0) (denominator 0) (last-b 0))
+
+(defun completion-pcm--update-score-and-face (str a b score)
+  "Update score and face in STR given match range (A B)."
+  (add-face-text-property a b
+                          'completions-common-part
+                          nil str)
+  (let ((last-b (completion-pcm--score-last-b score)))
+    (setf (completion-pcm--score-numerator score)
+          (+ (completion-pcm--score-numerator score) (- b a)))
+    (unless (or (= a last-b)
+                (zerop last-b)
+                (= a (length str)))
+      (setf (completion-pcm--score-denominator score)
+            (+ (completion-pcm--score-denominator score)
+               1
+               (expt (- a last-b 1)
+                     (/ 1.0
+                        flex-score-match-tightness)))))
+    (setf (completion-pcm--score-last-b score) b)))
+
 (defun completion-pcm--find-all-completions (string table pred point
                                                     &optional filter)
   "Find all completions for STRING at POINT in TABLE, satisfying PRED.
@@ -3980,7 +3986,7 @@ completion-flex-all-completions
                   string table pred point
                   #'completion-flex--make-flex-pattern)))
       (when all
-        (nconc (completion-pcm--hilit-commonality pattern all)
+        (nconc (benchmark-progn (completion-pcm--hilit-commonality pattern all))
                (length prefix))))))
 
 ;; Initials completion

  reply	other threads:[~2021-06-11  2:19 UTC|newest]

Thread overview: 174+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-05  1:39 bug#48841: fido-mode is slower than ido-mode with similar settings Dmitry Gutov
2021-06-05  9:35 ` João Távora
2021-06-05 23:02   ` Dmitry Gutov
2021-06-05 23:20     ` João Távora
2021-06-05 23:42       ` Dmitry Gutov
2021-06-06  0:25       ` Dmitry Gutov
2021-06-06  6:54         ` João Távora
2021-06-06 22:20           ` Dmitry Gutov
2021-06-06 23:49             ` João Távora
2021-06-07  0:11               ` Dmitry Gutov
2021-06-07  8:52                 ` João Távora
2021-06-11  2:19                   ` Dmitry Gutov [this message]
2021-06-11 17:09                     ` João Távora
2021-06-11 22:34                       ` Dmitry Gutov
2021-06-11 22:41                         ` Dmitry Gutov
2021-06-13 14:55                         ` João Távora
2021-06-17  2:36                           ` Dmitry Gutov
2021-06-17 21:21                             ` João Távora
2021-07-04  1:53                               ` Dmitry Gutov
2021-07-07  8:56                                 ` bug#47711: " Daniel Mendler
2021-06-11 23:24                     ` João Távora
2021-06-12  0:43                       ` Dmitry Gutov
2021-06-13 14:29                         ` João Távora
2021-06-14  0:08                           ` Dmitry Gutov
2021-06-14  0:16                             ` João Távora
2021-06-17  2:23                               ` Dmitry Gutov
2021-06-17 21:29                                 ` João Távora
2021-07-04  1:42                                   ` Dmitry Gutov
2021-06-06  2:34       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-06-06  6:59         ` João Távora
2021-06-06 16:54           ` Dmitry Gutov
2021-06-06 18:37             ` João Távora
2021-06-06 22:21               ` Dmitry Gutov
2021-06-06 23:27                 ` João Távora
2021-06-06 17:55           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-06-06 21:33             ` João Távora
2021-08-11 14:16 ` bug#48841: [PATCH] Add new `completion-filter-completions` API and deferred highlighting Daniel Mendler
2021-08-11 16:11   ` Daniel Mendler
2021-08-11 16:17     ` bug#47711: " João Távora
2021-08-12  9:24     ` Daniel Mendler
2021-08-13 10:38       ` bug#48841: [PATCH VERSION 2] " Daniel Mendler
2021-08-13 10:56         ` João Távora
2021-08-13 11:21           ` bug#48841: bug#47711: " Daniel Mendler
2021-08-13 12:05             ` João Távora
2021-08-13 12:22               ` Daniel Mendler
2021-08-13 12:37                 ` bug#48841: " João Távora
2021-08-13 12:56                   ` Daniel Mendler
2021-08-13 13:36                     ` bug#48841: " João Távora
2021-08-13 14:03                       ` Daniel Mendler
2021-08-13 14:11                         ` bug#48841: " João Távora
2021-08-13 14:37                           ` bug#47711: " Daniel Mendler
2021-08-14  2:47                       ` Dmitry Gutov
2021-08-14  7:12                         ` bug#47711: " Eli Zaretskii
2021-08-14 11:22                           ` Dmitry Gutov
2021-08-16  8:48                           ` Daniel Mendler
2021-08-16 11:57                             ` bug#47711: " Eli Zaretskii
2021-08-16 12:02                               ` João Távora
2021-08-16 12:19                                 ` Eli Zaretskii
2021-08-16 12:08                               ` Daniel Mendler
2021-08-14 10:36                         ` João Távora
2021-08-14 11:29                           ` Eli Zaretskii
2021-08-14 12:12                             ` bug#47711: " Lars Ingebrigtsen
2021-08-14 12:39                               ` Eli Zaretskii
2021-08-14 13:29                                 ` Lars Ingebrigtsen
2021-08-16  3:21                               ` Dmitry Gutov
2021-08-16  3:27                                 ` bug#47711: " João Távora
2021-08-16  3:31                                   ` Dmitry Gutov
2021-08-16  3:53                                     ` João Távora
2021-08-16  3:59                                       ` Dmitry Gutov
2021-08-16  4:25                                         ` bug#47711: " João Távora
2021-08-16  9:08                                           ` Daniel Mendler
2021-08-16 10:15                                             ` João Távora
2021-08-16 10:52                                               ` Daniel Mendler
2021-08-16 11:37                                                 ` bug#48841: " João Távora
2021-08-16 12:05                                                   ` Daniel Mendler
2021-08-16 12:17                                                     ` João Távora
2021-08-16 12:43                                                     ` Eli Zaretskii
2021-08-16 14:26                                                   ` bug#48841: " Dmitry Gutov
2021-08-16 14:29                                                     ` João Távora
2021-08-16 12:39                                                 ` Eli Zaretskii
2021-08-16 12:49                                                   ` bug#48841: " Daniel Mendler
2021-08-16 13:21                                                     ` Eli Zaretskii
2021-08-16 14:00                                                       ` Dmitry Gutov
2021-08-16 14:20                                                         ` João Távora
2021-08-16 14:33                                                           ` bug#48841: " Dmitry Gutov
2021-08-16 14:36                                                             ` João Távora
2021-08-16 14:47                                                               ` bug#47711: bug#48841: " Dmitry Gutov
2021-08-16 16:59                                                                 ` João Távora
2021-08-16 18:25                                                             ` João Távora
2021-08-17  2:08                                                               ` Dmitry Gutov
2021-08-17  8:59                                                                 ` João Távora
2021-08-17 11:48                                                                   ` bug#48841: " Eli Zaretskii
2021-08-17 11:52                                                                     ` bug#47711: " João Távora
2021-08-16  3:17                             ` Dmitry Gutov
2021-08-16 11:46                               ` Eli Zaretskii
2021-08-16 13:38                                 ` Dmitry Gutov
2021-08-16 13:41                                   ` João Távora
2021-08-16 14:14                                     ` bug#47711: " Dmitry Gutov
2021-08-15 18:32                           ` bug#48841: [PATCH] Make fido-mode about as fast as ido-mode even with many completions João Távora
2021-08-25 15:42                             ` João Távora
2021-08-14  7:01                     ` bug#48841: bug#47711: [PATCH VERSION 2] Add new `completion-filter-completions` API and deferred highlighting Eli Zaretskii
2021-08-14  9:48                       ` João Távora
2021-08-15  0:03                         ` João Távora
2021-08-16  3:26                       ` Dmitry Gutov
2021-08-16 11:48                         ` bug#48841: " Eli Zaretskii
2021-08-16  8:47                       ` Daniel Mendler
2021-08-14  2:55               ` bug#47711: bug#48841: " Dmitry Gutov
2021-08-14  7:16                 ` bug#48841: " Eli Zaretskii
2023-10-24 22:25                   ` bug#47711: " Dmitry Gutov
2023-10-25 17:52                     ` João Távora
2023-10-25 20:50                       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-25 21:02                         ` João Távora
2023-10-25 22:12                           ` João Távora
2023-10-26 21:49                             ` João Távora
2023-10-26 23:10                               ` Dmitry Gutov
2023-10-26 23:27                                 ` João Távora
2023-10-26 23:35                                   ` Dmitry Gutov
2023-10-26 23:52                                     ` João Távora
2023-10-26 23:25                       ` Dmitry Gutov
2023-10-26 23:44                         ` João Távora
2023-10-27  0:11                           ` Dmitry Gutov
2023-10-27  0:26                             ` João Távora
2023-10-27 13:29                               ` Dmitry Gutov
2023-10-27 13:46                                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-27 15:41                                   ` Dmitry Gutov
2023-10-27 16:19                                     ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-27 17:06                                       ` Dmitry Gutov
2023-10-27 18:12                                         ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-29  2:07                                           ` Dmitry Gutov
2023-10-29  4:41                                             ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-11-03  0:16                                               ` Dmitry Gutov
2023-11-03  3:05                                                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-10-27 17:16                                 ` João Távora
2023-10-28 22:24                                   ` Dmitry Gutov
2023-10-29 23:12                                     ` João Távora
2023-10-31  3:20                                       ` Dmitry Gutov
2023-10-31 10:55                                         ` João Távora
2023-10-31 20:52                                           ` Dmitry Gutov
2023-11-01 18:47                                             ` João Távora
2023-11-01 22:45                                               ` Dmitry Gutov
2023-11-02  9:48                                                 ` João Távora
2023-11-02 10:10                                                   ` Eli Zaretskii
2023-11-02 10:39                                                     ` João Távora
2023-11-02 10:58                                                       ` Eli Zaretskii
2023-11-02 11:12                                                         ` João Távora
2023-11-02 14:40                                                   ` Dmitry Gutov
2023-11-02 15:24                                                     ` João Távora
2023-11-02 15:36                                                       ` Dmitry Gutov
2023-11-02 15:58                                                         ` João Távora
2023-11-02 16:03                                                           ` Dmitry Gutov
2023-11-02 16:09                                                             ` João Távora
2023-11-02 16:15                                                               ` Dmitry Gutov
2021-04-11 20:51                                                                 ` bug#47711: 27.1; Deferred highlighting support in `completion-all-completions', `vertico--all-completions` Daniel Mendler
     [not found]                                                                   ` <handler.47711.B.16181742862702.ack@debbugs.gnu.org>
2021-04-18 21:26                                                                     ` bug#47711: Acknowledgement (27.1; Deferred highlighting support in `completion-all-completions', `vertico--all-completions`) Daniel Mendler
2023-11-04 18:46                                                                   ` bug#47711: bug#48841: bug#47711: bug#48841: bug#47711: [PATCH VERSION 2] Add new `completion-filter-completions` API and deferred highlighting Howard Melman
2024-04-08 17:19                                                                   ` bug#47711: 27.1; Deferred highlighting support in `completion-all-completions', `vertico--all-completions` Dmitry Gutov
2023-11-06 16:20                                                 ` bug#47711: bug#48841: bug#47711: bug#48841: bug#47711: [PATCH VERSION 2] Add new `completion-filter-completions` API and deferred highlighting João Távora
2023-11-06 19:38                                                   ` Dmitry Gutov
2023-11-07 12:13                                                     ` João Távora
2023-11-08  1:06                                                       ` Dmitry Gutov
2023-11-08  1:24                                                         ` João Távora
2023-11-08  1:47                                                           ` Dmitry Gutov
2023-10-27  0:14                           ` João Távora
2021-08-14  8:23                 ` João Távora
2021-08-16  3:48                   ` Dmitry Gutov
2021-08-16  4:20                     ` bug#48841: " João Távora
2021-08-16  8:53                       ` Daniel Mendler
2021-08-14  6:45         ` Eli Zaretskii
2021-08-14  3:11     ` bug#47711: bug#48841: [PATCH] " Dmitry Gutov
2021-08-12  8:00   ` Eli Zaretskii
2021-08-12  8:47     ` Daniel Mendler
2021-08-14  6:27       ` Eli Zaretskii
2021-08-16  9:42         ` Daniel Mendler
2021-08-16 12:58           ` bug#47711: " Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=858682b2-b8fd-898b-bef3-97dbe5e4debc@yandex.ru \
    --to=dgutov@yandex.ru \
    --cc=48841@debbugs.gnu.org \
    --cc=joaotavora@gmail.com \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).