unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Kangas <stefankangas@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>, JD Smith <jdtsmith@gmail.com>
Cc: Eli Zaretskii <eliz@gnu.org>, emacs-devel@gnu.org
Subject: Re: Temporarily select-window, without updating mode-line face and cursor fill?
Date: Tue, 4 May 2021 19:49:25 -0500	[thread overview]
Message-ID: <CADwFkmnUtg6mZH5XiAVfGZAnX-fOY+WipKDN8ETANEQ4s3=t9g@mail.gmail.com> (raw)
In-Reply-To: <jwvlf8wwogv.fsf-monnier+emacs@gnu.org>

Stefan Monnier <monnier@iro.umontreal.ca> 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)

Why not to just rename `line-number-at-pos' to
`internal--line-number-at-pos and install something much like the below
as `line-number-at-pos'?

> (defvar nlinum--line-number-cache nil)
> (make-variable-buffer-local 'nlinum--line-number-cache)
>
> ;; We could try and avoid flushing the cache at every change, e.g. with:
> ;;   (defun nlinum--before-change (start _end)
> ;;     (if (and nlinum--line-number-cache
> ;;              (< start (car nlinum--line-number-cache)))
> ;;         (save-excursion (goto-char start) (nlinum--line-number-at-pos))))
> ;; But it's far from clear that it's worth the trouble.  The current simplistic
> ;; approach seems to be good enough in practice.
>
> (defun nlinum--after-change (&rest _args)
>   (setq nlinum--line-number-cache nil))
>
> (defun nlinum--line-number-at-pos ()
>   "Like `line-number-at-pos' but sped up with a cache.
> Only works right if point is at BOL."
>   ;; (cl-assert (bolp))
>   (if nlinum-widen
>       (save-excursion
>         (save-restriction
>           (widen)
>           (forward-line 0)              ;In case (point-min) was not at BOL.
>           (let ((nlinum-widen nil))
>             (nlinum--line-number-at-pos))))
>     (let ((pos
>            (if (and nlinum--line-number-cache
>                     (> (- (point) (point-min))
>                        (abs (- (point) (car nlinum--line-number-cache)))))
>                (funcall (if (> (point) (car nlinum--line-number-cache))
>                             #'+ #'-)
>                         (cdr nlinum--line-number-cache)
>                         (count-lines (point) (car nlinum--line-number-cache)))
>              (line-number-at-pos))))
>       ;;(assert (= pos (line-number-at-pos)))
>       (add-hook 'after-change-functions #'nlinum--after-change nil :local)
>       (setq nlinum--line-number-cache (cons (point) pos))
>       pos)))



  parent reply	other threads:[~2021-05-05  0:49 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 [this message]
2021-05-05 11:54             ` Eli Zaretskii
2021-05-05 19:32               ` Stefan Kangas
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='CADwFkmnUtg6mZH5XiAVfGZAnX-fOY+WipKDN8ETANEQ4s3=t9g@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).