unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Kangas <stefankangas@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: monnier@iro.umontreal.ca, jdtsmith@gmail.com, emacs-devel@gnu.org
Subject: Re: Temporarily select-window, without updating mode-line face and cursor fill?
Date: Wed, 5 May 2021 14:32:27 -0500	[thread overview]
Message-ID: <CADwFkm=W88GAx226WYyKvub_W9esYVKWL1SBNUXJtSaKPVp_6A@mail.gmail.com> (raw)
In-Reply-To: <83y2ct2y79.fsf@gnu.org>

Eli Zaretskii <eliz@gnu.org> writes:

>> > We really should simply speed up `line-number-at-pos`.
>> > It shouldn't be hard.  See below what I do in nlinum.el.
>>
>> Interesting, it seems much faster at least in this simplistic test:
>>
>>     (benchmark-run 100000 (line-number-at-pos))
>>     => (5.868677411999999 0 0.0)
>>
>>     (benchmark-run 100000 (line-number-at-pos))
>>     => (0.9772498589999999 1 0.5954873589998897)
>
> Doesn't this measure the line number of the same position?  If so,
> that's about as favorable benchmark for caching as possible, no?

Yup.

Here's another one I threw together, tested here in src/keymap.c:

(defvar bench-run-times 10000)

(defun line-number-at-pos-benchmark (lines)
  (let (results)
    (dolist (fun '(line-number-at-pos nlinum--line-number-at-pos))
      (goto-char (/ (point-max) 2))
      (save-excursion
        (push
         (cons fun
               (cons lines
                     (benchmark-run bench-run-times
                       (let ((n (- (random (/ lines 2))
                                   (random (/ lines 2)))))
                         (forward-line n))
                       (funcall fun))))
         results)))
    results))

(defun run-bench (from to inc)
  (with-current-buffer (get-buffer-create "*Bench-Results*")
    (erase-buffer))
  (mapc
   (lambda (res)
     ;; ((nlinum--line-number-at-pos 0 0.93488997 0 0.0)
     ;;  (line-number-at-pos 0 1.0096957629999999 0 0.0))
     (let ((a (car res))
           (b (cadr res)))
      (with-current-buffer (get-buffer-create "*Bench-Results*")
        (insert (format (concat "%3s: "
                                "%s %6.3f %2d %6.3f | "
                                "%s %6.3f %2d %6.3f  [%5.2f %% faster]\n")
                        (cadr b)
                        (car a) (caddr a) (cadddr a) (car (cddddr a))
                        (car b) (caddr b) (cadddr b) (car (cddddr b))
                        (* (- 1.0 (/ (caddr a) (caddr b))) 100))))))
   (mapcar #'line-number-at-pos-benchmark
           (number-sequence from to inc)))
  (pop-to-buffer "*Bench-Results*"))

Now, (run-bench 0 100 5) gives me:

  0: nlinum--line-number-at-pos  0.963  0  0.000 | line-number-at-pos
1.107  0  0.000  [13.04 % faster]
  5: nlinum--line-number-at-pos  0.027  0  0.000 | line-number-at-pos
0.118  0  0.000  [77.13 % faster]
 10: nlinum--line-number-at-pos  0.030  0  0.000 | line-number-at-pos
0.174  0  0.000  [82.56 % faster]
 15: nlinum--line-number-at-pos  0.031  0  0.000 | line-number-at-pos
0.170  0  0.000  [81.80 % faster]
 20: nlinum--line-number-at-pos  0.033  0  0.000 | line-number-at-pos
0.209  0  0.000  [84.26 % faster]
 25: nlinum--line-number-at-pos  0.035  0  0.000 | line-number-at-pos
0.170  0  0.000  [79.45 % faster]
 30: nlinum--line-number-at-pos  0.037  0  0.000 | line-number-at-pos
0.109  0  0.000  [65.96 % faster]
 35: nlinum--line-number-at-pos  0.039  0  0.000 | line-number-at-pos
0.094  0  0.000  [58.57 % faster]
 40: nlinum--line-number-at-pos  0.040  0  0.000 | line-number-at-pos
0.077  0  0.000  [48.01 % faster]
 45: nlinum--line-number-at-pos  0.044  0  0.000 | line-number-at-pos
0.117  0  0.000  [62.09 % faster]
 50: nlinum--line-number-at-pos  0.044  0  0.000 | line-number-at-pos
0.181  0  0.000  [75.77 % faster]
 55: nlinum--line-number-at-pos  0.046  0  0.000 | line-number-at-pos
0.106  0  0.000  [57.01 % faster]
 60: nlinum--line-number-at-pos  0.048  0  0.000 | line-number-at-pos
0.085  0  0.000  [43.54 % faster]
 65: nlinum--line-number-at-pos  0.052  0  0.000 | line-number-at-pos
0.114  0  0.000  [54.87 % faster]
 70: nlinum--line-number-at-pos  0.052  0  0.000 | line-number-at-pos
0.115  0  0.000  [54.48 % faster]
 75: nlinum--line-number-at-pos  0.055  0  0.000 | line-number-at-pos
0.208  0  0.000  [73.71 % faster]
 80: nlinum--line-number-at-pos  0.058  0  0.000 | line-number-at-pos
0.185  0  0.000  [68.89 % faster]
 85: nlinum--line-number-at-pos  0.059  0  0.000 | line-number-at-pos
0.157  0  0.000  [62.57 % faster]
 90: nlinum--line-number-at-pos  0.061  0  0.000 | line-number-at-pos
0.185  0  0.000  [67.22 % faster]
 95: nlinum--line-number-at-pos  0.063  0  0.000 | line-number-at-pos
0.102  0  0.000  [38.34 % faster]
100: nlinum--line-number-at-pos  0.064  0  0.000 | line-number-at-pos
0.729  1  0.597  [91.24 % faster]

However, if I ramp up bench-run-times to 100000 I start seeing GC hits,
and then nlinum--line-number-at-pos can be up to 10 times slower
instead...



  reply	other threads:[~2021-05-05 19:32 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-01 18:46 Temporarily select-window, without updating mode-line face and cursor fill? JD Smith
2021-05-01 19:31 ` Eli Zaretskii
2021-05-01 20:32   ` JD Smith
2021-05-02  6:49     ` Eli Zaretskii
2021-05-03  2:15       ` JD Smith
2021-05-03  7:50         ` martin rudalics
2021-05-03 16:16           ` JD Smith
2021-05-03 17:32             ` martin rudalics
2021-05-02  7:40     ` martin rudalics
2021-05-03  2:23       ` JD Smith
2021-05-01 22:17   ` JD Smith
2021-05-02  6:55     ` Eli Zaretskii
2021-05-03  2:08       ` JD Smith
2021-05-03  2:25         ` Stefan Monnier
2021-05-03  2:49           ` JD Smith
2021-05-04 19:28           ` JD Smith
2021-05-04 19:40             ` Stefan Monnier
2021-05-05  0:49           ` Stefan Kangas
2021-05-05 11:54             ` Eli Zaretskii
2021-05-05 19:32               ` Stefan Kangas [this message]
2021-05-05 19:47                 ` Stefan Monnier
2021-05-06  0:16                 ` JD Smith

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='CADwFkm=W88GAx226WYyKvub_W9esYVKWL1SBNUXJtSaKPVp_6A@mail.gmail.com' \
    --to=stefankangas@gmail.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=jdtsmith@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).