From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Andrea Monaco Newsgroups: gmane.emacs.devel Subject: [PATCH] Make rmail-summary-by-thread faster Date: Fri, 09 Dec 2022 21:22:22 +0100 Message-ID: <87a63wnx8h.fsf@autistici.org> References: <83y1s9d6tx.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="35616"; mail-complaints-to="usenet@ciao.gmane.io" Cc: rms@gnu.org, emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Dec 09 21:23:17 2022 Return-path: Envelope-to: ged-emacs-devel@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 1p3jts-000911-Bw for ged-emacs-devel@m.gmane-mx.org; Fri, 09 Dec 2022 21:23:16 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p3jtD-0005bj-Dz; Fri, 09 Dec 2022 15:22:35 -0500 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 1p3jtB-0005bE-Aq for emacs-devel@gnu.org; Fri, 09 Dec 2022 15:22:33 -0500 Original-Received: from latitanza.investici.org ([82.94.249.234]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p3jt8-00041s-CL; Fri, 09 Dec 2022 15:22:33 -0500 Original-Received: from mx3.investici.org (unknown [127.0.0.1]) by latitanza.investici.org (Postfix) with ESMTP id 4NTMrb3HGCzGpCJ; Fri, 9 Dec 2022 20:22:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=autistici.org; s=stigmate; t=1670617343; bh=2AVwu66MmWOVULlOBGm1KDYeyQbI5ZV6admCZAZpDoQ=; h=From:To:Cc:Subject:In-Reply-To:Date:From; b=YjMnWRefrbbAauKBSKUcR3+2qwNYmQhHURNagcgZth5Rt62+p1ENAodoQUDIpWetZ 9tZM2H4T1fNhB/XfMXMEw6SFPrNqZkWovvaPk9Vt/Tm2S4wgDz8GwkqzdCBlnDnNyw dRMPOdfyvo9ETR6KttZ2kB6pRnQqrYnwkOeyNPuI= Original-Received: from [82.94.249.234] (mx3.investici.org [82.94.249.234]) (Authenticated sender: andrea.monaco@autistici.org) by localhost (Postfix) with ESMTPSA id 4NTMrb1bDxzGp77; Fri, 9 Dec 2022 20:22:23 +0000 (UTC) In-Reply-To: <83y1s9d6tx.fsf@gnu.org> (message from Eli Zaretskii on Thu, 17 Nov 2022 15:54:50 +0200) Received-SPF: pass client-ip=82.94.249.234; envelope-from=andrea.monaco@autistici.org; helo=latitanza.investici.org X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:301087 Archived-At: I found the first implementation of rmail-summary-by-thread a bit slow. There's an optimization that escaped me at first: generate another vector called rmail-summary-message-descendants-vector that holds the direct descendants of each message, that is the symmetric relation to rmail-summary-message-parents-vector. Now it is much faster. Andrea Monaco diff --git a/lisp/mail/rmailsum.el b/lisp/mail/rmailsum.el index b30c32aaffd..9d9c6d16988 100644 --- a/lisp/mail/rmailsum.el +++ b/lisp/mail/rmailsum.el @@ -84,6 +84,11 @@ rmail-summary-message-parents-vector References or In-reply-to fields of B, or if A is the first message with the same subject as B. First element is ignored.") +(defvar rmail-summary-message-descendants-vector nil + "Vector that holds the direct descendants of each message. +This is the symmetric relation to `rmail-summary-message-parents-vector'. + First element is ignored.") + (defvar rmail-summary-font-lock-keywords '(("^ *[0-9]+D.*" . font-lock-string-face) ; Deleted. ("^ *[0-9]+-.*" . font-lock-type-face) ; Unread. @@ -331,14 +336,16 @@ rmail-summary--split-header-field (if header (split-string header "[ \f\t\n\r\v,;]+")))) -(defun rmail-summary-fill-message-parents-vector () - "Fill `rmail-summary-message-parents-vector'." +(defun rmail-summary-fill-message-parents-and-descs-vectors () + "Fill `rmail-summary-message-parents-vector' and `rmail-summary-message-descendants-vector'." (with-current-buffer rmail-buffer (rmail-summary-fill-message-ids-hash-table) (setq rmail-summary-subjects-hash-table (make-hash-table :test 'equal :size 1024)) (setq rmail-summary-message-parents-vector (make-vector (1+ rmail-total-messages) nil)) + (setq rmail-summary-message-descendants-vector + (make-vector (1+ rmail-total-messages) nil)) (let ((msgnum 1)) (while (<= msgnum rmail-total-messages) (let* ((parents nil) @@ -346,18 +353,23 @@ rmail-summary-fill-message-parents-vector (subj-cell (gethash subject rmail-summary-subjects-hash-table)) (subj-par (assoc subject subj-cell)) (refs (rmail-summary--split-header-field "References" msgnum)) - (reply-to (rmail-summary--split-header-field "In-reply-to" + (reply-tos (rmail-summary--split-header-field "In-reply-to" msgnum))) (if subj-par - (setq parents (cons (cdr subj-par) parents)) + (progn + (setq parents (cons (cdr subj-par) nil)) + (aset rmail-summary-message-descendants-vector (cdr subj-par) + (cons msgnum (aref rmail-summary-message-descendants-vector (cdr subj-par))))) (puthash subject (cons (cons subject msgnum) subj-cell) rmail-summary-subjects-hash-table)) - (dolist (id (append refs reply-to)) + (dolist (id (append refs reply-tos)) (let ((ent (assoc id (gethash id rmail-summary-message-ids-hash-table)))) - (if ent - (setq parents (cons (cdr ent) parents))))) + (when ent + (setq parents (cons (cdr ent) parents)) + (aset rmail-summary-message-descendants-vector (cdr ent) + (cons msgnum (aref rmail-summary-message-descendants-vector (cdr ent))))))) (aset rmail-summary-message-parents-vector msgnum parents) (setq msgnum (1+ msgnum))))))) @@ -387,20 +399,6 @@ rmail-summary (interactive) (rmail-new-summary "All" '(rmail-summary) nil)) -(defun rmail-summary-direct-descendants (msgnum encountered-msgs) - "Find all direct descendants of MSGNUM, ignoring ENCOUNTERED-MSGS. -Assumes `rmail-summary-message-parents-vector' is filled. Ignores messages -already ticked in ENCOUNTERED-MSGS." - (let (desc - (msg 1)) - (while (<= msg rmail-total-messages) - (when (and - (not (aref encountered-msgs msg)) - (memq msgnum (aref rmail-summary-message-parents-vector msg))) - (setq desc (cons msg desc))) - (setq msg (1+ msg))) - desc)) - (defun rmail-summary--walk-thread-message-recursively (msgnum encountered-msgs) "Add parents and descendants of message MSGNUM to ENCOUNTERED-MSGS, recursively." (unless (aref encountered-msgs msgnum) @@ -412,7 +410,7 @@ rmail-summary--walk-thread-message-recursively (mapc walk-thread-msg (aref rmail-summary-message-parents-vector msgnum)) (mapc walk-thread-msg - (rmail-summary-direct-descendants msgnum encountered-msgs))))) + (aref rmail-summary-message-descendants-vector msgnum))))) ;;;###autoload (defun rmail-summary-by-thread (&optional msgnum) @@ -430,7 +428,7 @@ rmail-summary-by-thread (unless (and rmail-summary-message-parents-vector (= (length rmail-summary-message-parents-vector) (1+ rmail-total-messages))) - (rmail-summary-fill-message-parents-vector)) + (rmail-summary-fill-message-parents-and-descs-vectors)) (let ((enc-msgs (make-bool-vector (1+ rmail-total-messages) nil))) (rmail-summary--walk-thread-message-recursively msgnum enc-msgs) (rmail-new-summary (format "thread containing message %d" msgnum)