From aa3d92d588fbb8c0459891b952cf8c59dfd3cf63 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Thu, 13 Feb 2014 12:32:31 +1900 Subject: [PATCH] [WIP PATCH] Make keys of notmuch-tag-formats regexps and use caching --- fd/e90025389813ea18d0e20ddcf1f7d8bd203b4d | 228 ++++++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100644 fd/e90025389813ea18d0e20ddcf1f7d8bd203b4d diff --git a/fd/e90025389813ea18d0e20ddcf1f7d8bd203b4d b/fd/e90025389813ea18d0e20ddcf1f7d8bd203b4d new file mode 100644 index 000000000..c9b489d9c --- /dev/null +++ b/fd/e90025389813ea18d0e20ddcf1f7d8bd203b4d @@ -0,0 +1,228 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 3004B431FBD + for ; Wed, 12 Feb 2014 09:32:47 -0800 (PST) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.7 +X-Spam-Level: +X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id UNW9tpZ9IQPp for ; + Wed, 12 Feb 2014 09:32:41 -0800 (PST) +Received: from dmz-mailsec-scanner-7.mit.edu (dmz-mailsec-scanner-7.mit.edu + [18.7.68.36]) + (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id AD4A2431FAE + for ; Wed, 12 Feb 2014 09:32:40 -0800 (PST) +X-AuditID: 12074424-f79e26d000000c70-aa-52fbb03632de +Received: from mailhub-auth-4.mit.edu ( [18.7.62.39]) + (using TLS with cipher AES256-SHA (256/256 bits)) + (Client did not present a certificate) + by dmz-mailsec-scanner-7.mit.edu (Symantec Messaging Gateway) with SMTP + id 92.71.03184.630BBF25; Wed, 12 Feb 2014 12:32:38 -0500 (EST) +Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11]) + by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id s1CHWahp014667; + Wed, 12 Feb 2014 12:32:36 -0500 +Received: from drake.dyndns.org + (216-15-114-40.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com + [216.15.114.40]) (authenticated bits=0) + (User authenticated as amdragon@ATHENA.MIT.EDU) + by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s1CHWX4s032107 + (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); + Wed, 12 Feb 2014 12:32:35 -0500 +Received: from amthrax by drake.dyndns.org with local (Exim 4.77) + (envelope-from ) + id 1WDdfl-0008Bz-O4; Wed, 12 Feb 2014 12:32:33 -0500 +From: Austin Clements +To: notmuch@notmuchmail.org +Subject: [WIP PATCH] Make keys of notmuch-tag-formats regexps and use caching +Date: Wed, 12 Feb 2014 12:32:31 -0500 +Message-Id: <1392226351-31440-1-git-send-email-amdragon@mit.edu> +X-Mailer: git-send-email 1.8.4.rc3 +In-Reply-To: <87r479mf4g.fsf@awakening.csail.mit.edu> +References: <87r479mf4g.fsf@awakening.csail.mit.edu> +X-Brightmail-Tracker: + H4sIAAAAAAAAA+NgFtrNIsWRmVeSWpSXmKPExsUixG6nrmu24XeQwbNNlhar5/JYXL85k9mB + yWPnrLvsHs9W3WIOYIrisklJzcksSy3St0vgyvjweydzwXfjimWXghoYP2t0MXJySAiYSPRd + /sAEYYtJXLi3nq2LkYtDSGA2k0T3z8lMEM5GRonvT/ZCZe4wSUxffIMFwpnLKLFgzw02kH42 + AQ2JbfuXM4LYIgLSEjvvzmYFsZkFHCU+718EVMPBISzgKzHneQFImEVAVaJp4Syw1bwCDhIN + J68yQ5yhJLHw1DawVk4BU4lpax+AjRcCOrXp4zKmCYz8CxgZVjHKpuRW6eYmZuYUpybrFicn + 5uWlFuma6+VmluilppRuYgSHkYvKDsbmQ0qHGAU4GJV4eC9M/R0kxJpYVlyZe4hRkoNJSZT3 + +hqgEF9SfkplRmJxRnxRaU5q8SFGCQ5mJRFeo2agHG9KYmVValE+TEqag0VJnLfW4leQkEB6 + YklqdmpqQWoRTFaGg0NJgtdpPVCjYFFqempFWmZOCUKaiYMTZDgP0PAIkBre4oLE3OLMdIj8 + KUZFKXFeFZCEAEgiozQPrhcW568YxYFeEeZVBqniAaYIuO5XQIOZgAanRoENLklESEk1MCqn + dCsG5clazj6ksv702nl9VyVXRk8pmm3B+Xyx+pa52xQOBC591LvtoaoNY/tWqyV/BL+mb5p5 + 85vjrQw971drjsyVy1Vf9q2gzjrU9/R933sTAhkD+hJkd8wyULw6S8Np9wfWg7E3jvoH3g0V + kPsUc2axtMxelk/Pjh5jO8aY5M+s7dC2olOJpTgj0VCLuag4EQBa3NsNzgIAAA== +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Wed, 12 Feb 2014 17:32:47 -0000 + +This was a little hack to test the feasibility of switching +notmuch-tag-formats to use regexps with caching for performance. In +the end it works fine and isn't particularly complex, though there +were a few gotchas: + +1) We have to clear the cache somehow on changes to +notmuch-tag-formats. I opted to use a defcustom :set plus some +documentation telling people what to do if they change it directly +from Elisp. This is less automatic than I would like, but I doubt +people are changing this very often and I concluded that any machinery +to automatically detect changes to notmuch-tag-formats would probably +outweigh the benefits of caching. Alternatively, we could require +search/show/tree buffers to "opt in" to caching when they start +building. + +2) I spent way too long trying to use assoc-default before realizing +this it just wouldn't work, since there's no way to distinguish a +missing key from a present key with a null cdr. assoc* from cl works +fine. + +Performance-wise, the caching of regexp lookup makes this just as fast +as assoc for unformatted tags (it would probably be faster if someone +had a really big `notmuch-tag-formats') and the caching of eval +results makes this much *faster* than the current code for formatted +tags. + + inbox (usec) unread (usec) +assoc: 0.4 2.8 +regexp: 3.2 7.2 +regexp+caching: 0.4 0.4 + +That said, even at 7.2 usec, tag formatting is still *very* fast +(though regexp matching may get noticeably slower with larger +`notmuch-tag-formats'). Tag formatting is nowhere near our top +bottleneck. +--- + emacs/notmuch-tag.el | 75 +++++++++++++++++++++++++++++++++++++--------------- + 1 file changed, 53 insertions(+), 22 deletions(-) + +diff --git a/emacs/notmuch-tag.el b/emacs/notmuch-tag.el +index b60f46c..07f5772 100644 +--- a/emacs/notmuch-tag.el ++++ b/emacs/notmuch-tag.el +@@ -28,35 +28,56 @@ + (require 'crm) + (require 'notmuch-lib) + ++;; (notmuch-tag-clear-cache will be called by the defcustom ++;; notmuch-tag-formats, so it has to be defined first.) ++ ++(defvar notmuch-tag--format-cache (make-hash-table :test 'equal) ++ "Cache of tag format lookup. Internal to `notmuch-tag-format-tag'.") ++ ++(defun notmuch-tag-clear-cache () ++ "Clear the internal cache of tag formats. ++ ++This must be called after changes to `notmuch-tag-formats'." ++ (clrhash notmuch-tag--format-cache)) ++ + (defcustom notmuch-tag-formats + '(("unread" (propertize tag 'face '(:foreground "red"))) + ("flagged" (propertize tag 'face '(:foreground "blue")) + (notmuch-tag-format-image-data tag (notmuch-tag-star-icon)))) + "Custom formats for individual tags. + +-This gives a list that maps from tag names to lists of formatting +-expressions. The car of each element gives a tag name and the +-cdr gives a list of Elisp expressions that modify the tag. If +-the list is empty, the tag will simply be hidden. Otherwise, +-each expression will be evaluated in order: for the first +-expression, the variable `tag' will be bound to the tag name; for +-each later expression, the variable `tag' will be bound to the +-result of the previous expression. In this way, each expression +-can build on the formatting performed by the previous expression. +-The result of the last expression will displayed in place of the +-tag. ++This is an association list that maps from tag name regexps to ++lists of formatting expressions. The first entry whose car ++regexp-matches a tag will be used to format that tag. The regexp ++is implicitly anchored, so to match a literal tag name, just use ++that tag name (if it contains special regexp characters like ++\".\" or \"*\", these have to be escaped). The cdr of the ++matching entry gives a list of Elisp expressions that modify the ++tag. If the list is empty, the tag will simply be hidden. ++Otherwise, each expression will be evaluated in order: for the ++first expression, the variable `tag' will be bound to the tag ++name; for each later expression, the variable `tag' will be bound ++to the result of the previous expression. In this way, each ++expression can build on the formatting performed by the previous ++expression. The result of the last expression will displayed in ++place of the tag. + + For example, to replace a tag with another string, simply use + that string as a formatting expression. To change the foreground + of a tag to red, use the expression + (propertize tag 'face '(:foreground \"red\")) + ++After modifying this variable in Elisp, be sure to call ++`notmuch-tag-clear-cache'. Modifying this via customize does ++this automatically. ++ + See also `notmuch-tag-format-image', which can help replace tags + with images." + + :group 'notmuch-search + :group 'notmuch-show +- :type '(alist :key-type (string :tag "Tag") ++ :set (lambda (var val) (set-default var val) (notmuch-tag-clear-cache)) ++ :type '(alist :key-type (regexp :tag "Tag") + :extra-offset -3 + :value-type + (radio :format "%v" +@@ -137,16 +158,26 @@ This can be used with `notmuch-tag-format-image-data'." + + (defun notmuch-tag-format-tag (tag) + "Format TAG by looking into `notmuch-tag-formats'." +- (let ((formats (assoc tag notmuch-tag-formats))) +- (cond +- ((null formats) ;; - Tag not in `notmuch-tag-formats', +- tag) ;; the format is the tag itself. +- ((null (cdr formats)) ;; - Tag was deliberately hidden, +- nil) ;; no format must be returned +- (t ;; - Tag was found and has formats, +- (let ((tag tag)) ;; we must apply all the formats. +- (dolist (format (cdr formats) tag) +- (setq tag (eval format)))))))) ++ (let ((formatted (gethash tag notmuch-tag--format-cache 'missing))) ++ (when (eq formatted 'missing) ++ (let* ((formats ++ (save-match-data ++ (assoc* tag notmuch-tag-formats ++ :test (lambda (key tag) ++ (and (eq (string-match key tag) 0) ++ (= (match-end 0) (length tag)))))))) ++ (setq formatted ++ (cond ++ ((null formats) ;; - Tag not in `notmuch-tag-formats', ++ tag) ;; the format is the tag itself. ++ ((null (cdr formats)) ;; - Tag was deliberately hidden, ++ nil) ;; no format must be returned ++ (t ;; - Tag was found and has formats, ++ (let ((tag tag)) ;; we must apply all the formats. ++ (dolist (format (cdr formats) tag) ++ (setq tag (eval format))))))) ++ (puthash tag formatted notmuch-tag--format-cache))) ++ formatted)) + + (defun notmuch-tag-format-tags (tags) + "Return a string representing formatted TAGS." +-- +1.8.4.rc3 + -- 2.26.2