From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id CB7A1431FC2 for ; Fri, 14 Feb 2014 01:15:34 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: 0.201 X-Spam-Level: X-Spam-Status: No, score=0.201 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_ENVFROM_END_DIGIT=1, FREEMAIL_FROM=0.001, 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 eZH9G3yvFwvE for ; Fri, 14 Feb 2014 01:15:30 -0800 (PST) Received: from mail-wg0-f51.google.com (mail-wg0-f51.google.com [74.125.82.51]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 1C6AF431FB6 for ; Fri, 14 Feb 2014 01:15:29 -0800 (PST) Received: by mail-wg0-f51.google.com with SMTP id n12so183939wgh.18 for ; Fri, 14 Feb 2014 01:15:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:subject:date:message-id:in-reply-to:references; bh=BxZI08p31KmOxc70Ea/BcOAVyJOE+gK2pt1SuAImP6I=; b=0tnfCvr3WDn40CEvqz61rma0+bO5K4o2Nm0oidNOdpIY3h7+1jNa2EuFAt4VAHC2Dh tMkNaOQjrIZfdCN8CHlH914gs0Ucx2vmiK254JkmogUmLa0ueYI12f/MUjvmFOQINrTr I2SlMsCFQ2n/uYX5pEl/OtzDla1dO8RCouiP2xktsY97SSxBFBUfw794ulOEZUEWKXxn 11SDGJtfPIGYL7oJst3c4niD0SXa015MHQcop/pPxL+0aUUxnD3BL6pNBD20tMEP4b6C rK0tzK+YqYwnpaWBfd4cmZPXAWDzc5Yfyj9CatLHnBe7Ew0PtWQRfGwxY9b5MGtEYvUC OpLQ== X-Received: by 10.194.2.70 with SMTP id 6mr4906069wjs.25.1392369327419; Fri, 14 Feb 2014 01:15:27 -0800 (PST) Received: from localhost (93-97-24-31.zone5.bethere.co.uk. [93.97.24.31]) by mx.google.com with ESMTPSA id ev4sm2715601wib.1.2014.02.14.01.15.26 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Fri, 14 Feb 2014 01:15:26 -0800 (PST) From: Mark Walters To: notmuch@notmuchmail.org, Austin Clements Subject: [WIP Patch 1/7] Make keys of notmuch-tag-formats regexps and use caching Date: Fri, 14 Feb 2014 09:15:13 +0000 Message-Id: <1392369319-24508-2-git-send-email-markwalters1009@gmail.com> X-Mailer: git-send-email 1.7.9.1 In-Reply-To: <1392369319-24508-1-git-send-email-markwalters1009@gmail.com> References: <1392369319-24508-1-git-send-email-markwalters1009@gmail.com> 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: Fri, 14 Feb 2014 09:15:35 -0000 From: Austin Clements 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 files changed, 53 insertions(+), 22 deletions(-) diff --git a/emacs/notmuch-tag.el b/emacs/notmuch-tag.el index 908e7ad..cb88fd5 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 (tag key) + (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 &optional face) "Return a string representing formatted TAGS." -- 1.7.9.1