unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Eval Exec <execvy@gmail.com>
To: emacs-devel@gnu.org
Subject: [PATCH] Improve `tab-line-tabs-fixed-window-buffers` sorting performance
Date: Fri, 5 Jul 2024 22:27:10 +0800	[thread overview]
Message-ID: <CAKfPJDpVz0tGRDqcxWuh89sMQh_WWH2yrUXN47f6Sye1Pr=X4Q@mail.gmail.com> (raw)

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

Hello,
After noticing severe lag when `buffer-list` exceeds 1000 buffers,
particularly
with `tab-line-switch-to-prev-tab` and `tab-line-switch-to-next-tab`,
a profiler
revealed that the sorting operation in
`tab-line-tabs-fixed-window-buffers` heavily
relies on the `seq-position` function. This reliance significantly
impacts performance.
To address this, I have proposed a patch to optimize the buffer
sorting mechanism within
`tab-line-tabs-fixed-window-buffers`. The solution involves replacing
the inefficient
`seq-position` calls with a hash table to cache buffer positions,
which markedly
enhances performance when handling large buffer lists.

This is my first attempt at hacking Emacs. Do you have any suggestions
on this idea?

Thank you

[-- Attachment #2: 0001-Improve-tab-line-tabs-fixed-window-buffers-sorting-p.patch --]
[-- Type: text/x-patch, Size: 1772 bytes --]

From fa047a52c0d40aace236bd56f1d8ca70935f438a Mon Sep 17 00:00:00 2001
From: Eval EXEC <execvy@gmail.com>
Date: Fri, 5 Jul 2024 18:53:36 +0800
Subject: [PATCH] Improve `tab-line-tabs-fixed-window-buffers` sorting
 performance

* lsp/tab-line.el (tab-line-tabs-fixed-window-buffers): Enhanced
`tab-line-tabs-fixed-window-buffers` performance by optimizing buffer
sorting mechanism. Replaced inefficient `seq-position` calls with a hash
table to cache buffer positions, significantly improving speed when
handling large buffer lists.
---
 lisp/tab-line.el | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/lisp/tab-line.el b/lisp/tab-line.el
index 1d14fda9825..e4908c61542 100644
--- a/lisp/tab-line.el
+++ b/lisp/tab-line.el
@@ -555,10 +555,14 @@ This means that switching to a buffer previously shown in the same
 window will keep the same order of tabs that was before switching.
 And newly displayed buffers are added to the end of the tab line."
   (let* ((old-buffers (window-parameter nil 'tab-line-buffers))
+         (buffer-positions (let ((index-table (make-hash-table :test 'eq)))
+                             (seq-do-indexed
+                              (lambda (buf idx) (puthash buf idx index-table))
+                              old-buffers)
+                             index-table))
          (new-buffers (sort (tab-line-tabs-window-buffers)
                             :key (lambda (buffer)
-                                   (or (seq-position old-buffers buffer)
-                                       most-positive-fixnum)))))
+                                   (gethash buffer buffer-positions most-positive-fixnum)))))
     (set-window-parameter nil 'tab-line-buffers new-buffers)
     new-buffers))
 
-- 
2.44.1


                 reply	other threads:[~2024-07-05 14:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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='CAKfPJDpVz0tGRDqcxWuh89sMQh_WWH2yrUXN47f6Sye1Pr=X4Q@mail.gmail.com' \
    --to=execvy@gmail.com \
    --cc=emacs-devel@gnu.org \
    /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).