--- /dev/null
+Return-Path: <markwalters1009@gmail.com>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id CB7A1431FC2\r
+ for <notmuch@notmuchmail.org>; Fri, 14 Feb 2014 01:15:34 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.201\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.201 tagged_above=-999 required=5\r
+ tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_ENVFROM_END_DIGIT=1, FREEMAIL_FROM=0.001,\r
+ RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id eZH9G3yvFwvE for <notmuch@notmuchmail.org>;\r
+ Fri, 14 Feb 2014 01:15:30 -0800 (PST)\r
+Received: from mail-wg0-f51.google.com (mail-wg0-f51.google.com\r
+ [74.125.82.51]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client\r
+ certificate requested) by olra.theworths.org (Postfix) with ESMTPS id\r
+ 1C6AF431FB6 for <notmuch@notmuchmail.org>; Fri, 14 Feb 2014 01:15:29 -0800\r
+ (PST)\r
+Received: by mail-wg0-f51.google.com with SMTP id n12so183939wgh.18\r
+ for <notmuch@notmuchmail.org>; Fri, 14 Feb 2014 01:15:27 -0800 (PST)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:subject:date:message-id:in-reply-to:references;\r
+ bh=BxZI08p31KmOxc70Ea/BcOAVyJOE+gK2pt1SuAImP6I=;\r
+ b=0tnfCvr3WDn40CEvqz61rma0+bO5K4o2Nm0oidNOdpIY3h7+1jNa2EuFAt4VAHC2Dh\r
+ tMkNaOQjrIZfdCN8CHlH914gs0Ucx2vmiK254JkmogUmLa0ueYI12f/MUjvmFOQINrTr\r
+ I2SlMsCFQ2n/uYX5pEl/OtzDla1dO8RCouiP2xktsY97SSxBFBUfw794ulOEZUEWKXxn\r
+ 11SDGJtfPIGYL7oJst3c4niD0SXa015MHQcop/pPxL+0aUUxnD3BL6pNBD20tMEP4b6C\r
+ rK0tzK+YqYwnpaWBfd4cmZPXAWDzc5Yfyj9CatLHnBe7Ew0PtWQRfGwxY9b5MGtEYvUC\r
+ OpLQ==\r
+X-Received: by 10.194.2.70 with SMTP id 6mr4906069wjs.25.1392369327419;\r
+ Fri, 14 Feb 2014 01:15:27 -0800 (PST)\r
+Received: from localhost (93-97-24-31.zone5.bethere.co.uk. [93.97.24.31])\r
+ by mx.google.com with ESMTPSA id ev4sm2715601wib.1.2014.02.14.01.15.26\r
+ for <multiple recipients>\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Fri, 14 Feb 2014 01:15:26 -0800 (PST)\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: notmuch@notmuchmail.org,\r
+ Austin Clements <amdragon@MIT.EDU>\r
+Subject: [WIP Patch 1/7] Make keys of notmuch-tag-formats regexps and use\r
+ caching\r
+Date: Fri, 14 Feb 2014 09:15:13 +0000\r
+Message-Id: <1392369319-24508-2-git-send-email-markwalters1009@gmail.com>\r
+X-Mailer: git-send-email 1.7.9.1\r
+In-Reply-To: <1392369319-24508-1-git-send-email-markwalters1009@gmail.com>\r
+References: <1392369319-24508-1-git-send-email-markwalters1009@gmail.com>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 14 Feb 2014 09:15:35 -0000\r
+\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+\r
+This was a little hack to test the feasibility of switching\r
+notmuch-tag-formats to use regexps with caching for performance. In\r
+the end it works fine and isn't particularly complex, though there\r
+were a few gotchas:\r
+\r
+1) We have to clear the cache somehow on changes to\r
+notmuch-tag-formats. I opted to use a defcustom :set plus some\r
+documentation telling people what to do if they change it directly\r
+from Elisp. This is less automatic than I would like, but I doubt\r
+people are changing this very often and I concluded that any machinery\r
+to automatically detect changes to notmuch-tag-formats would probably\r
+outweigh the benefits of caching. Alternatively, we could require\r
+search/show/tree buffers to "opt in" to caching when they start\r
+building.\r
+\r
+2) I spent way too long trying to use assoc-default before realizing\r
+this it just wouldn't work, since there's no way to distinguish a\r
+missing key from a present key with a null cdr. assoc* from cl works\r
+fine.\r
+\r
+Performance-wise, the caching of regexp lookup makes this just as fast\r
+as assoc for unformatted tags (it would probably be faster if someone\r
+had a really big `notmuch-tag-formats') and the caching of eval\r
+results makes this much *faster* than the current code for formatted\r
+tags.\r
+\r
+ inbox (usec) unread (usec)\r
+assoc: 0.4 2.8\r
+regexp: 3.2 7.2\r
+regexp+caching: 0.4 0.4\r
+\r
+That said, even at 7.2 usec, tag formatting is still *very* fast\r
+(though regexp matching may get noticeably slower with larger\r
+`notmuch-tag-formats'). Tag formatting is nowhere near our top\r
+bottleneck.\r
+---\r
+ emacs/notmuch-tag.el | 75 +++++++++++++++++++++++++++++++++++--------------\r
+ 1 files changed, 53 insertions(+), 22 deletions(-)\r
+\r
+diff --git a/emacs/notmuch-tag.el b/emacs/notmuch-tag.el\r
+index 908e7ad..cb88fd5 100644\r
+--- a/emacs/notmuch-tag.el\r
++++ b/emacs/notmuch-tag.el\r
+@@ -28,35 +28,56 @@\r
+ (require 'crm)\r
+ (require 'notmuch-lib)\r
+ \r
++;; (notmuch-tag-clear-cache will be called by the defcustom\r
++;; notmuch-tag-formats, so it has to be defined first.)\r
++\r
++(defvar notmuch-tag--format-cache (make-hash-table :test 'equal)\r
++ "Cache of tag format lookup. Internal to `notmuch-tag-format-tag'.")\r
++\r
++(defun notmuch-tag-clear-cache ()\r
++ "Clear the internal cache of tag formats.\r
++\r
++This must be called after changes to `notmuch-tag-formats'."\r
++ (clrhash notmuch-tag--format-cache))\r
++\r
+ (defcustom notmuch-tag-formats\r
+ '(("unread" (propertize tag 'face '(:foreground "red")))\r
+ ("flagged" (propertize tag 'face '(:foreground "blue"))\r
+ (notmuch-tag-format-image-data tag (notmuch-tag-star-icon))))\r
+ "Custom formats for individual tags.\r
+ \r
+-This gives a list that maps from tag names to lists of formatting\r
+-expressions. The car of each element gives a tag name and the\r
+-cdr gives a list of Elisp expressions that modify the tag. If\r
+-the list is empty, the tag will simply be hidden. Otherwise,\r
+-each expression will be evaluated in order: for the first\r
+-expression, the variable `tag' will be bound to the tag name; for\r
+-each later expression, the variable `tag' will be bound to the\r
+-result of the previous expression. In this way, each expression\r
+-can build on the formatting performed by the previous expression.\r
+-The result of the last expression will displayed in place of the\r
+-tag.\r
++This is an association list that maps from tag name regexps to\r
++lists of formatting expressions. The first entry whose car\r
++regexp-matches a tag will be used to format that tag. The regexp\r
++is implicitly anchored, so to match a literal tag name, just use\r
++that tag name (if it contains special regexp characters like\r
++\".\" or \"*\", these have to be escaped). The cdr of the\r
++matching entry gives a list of Elisp expressions that modify the\r
++tag. If the list is empty, the tag will simply be hidden.\r
++Otherwise, each expression will be evaluated in order: for the\r
++first expression, the variable `tag' will be bound to the tag\r
++name; for each later expression, the variable `tag' will be bound\r
++to the result of the previous expression. In this way, each\r
++expression can build on the formatting performed by the previous\r
++expression. The result of the last expression will displayed in\r
++place of the tag.\r
+ \r
+ For example, to replace a tag with another string, simply use\r
+ that string as a formatting expression. To change the foreground\r
+ of a tag to red, use the expression\r
+ (propertize tag 'face '(:foreground \"red\"))\r
+ \r
++After modifying this variable in Elisp, be sure to call\r
++`notmuch-tag-clear-cache'. Modifying this via customize does\r
++this automatically.\r
++\r
+ See also `notmuch-tag-format-image', which can help replace tags\r
+ with images."\r
+ \r
+ :group 'notmuch-search\r
+ :group 'notmuch-show\r
+- :type '(alist :key-type (string :tag "Tag")\r
++ :set (lambda (var val) (set-default var val) (notmuch-tag-clear-cache))\r
++ :type '(alist :key-type (regexp :tag "Tag")\r
+ :extra-offset -3\r
+ :value-type\r
+ (radio :format "%v"\r
+@@ -137,16 +158,26 @@ This can be used with `notmuch-tag-format-image-data'."\r
+ \r
+ (defun notmuch-tag-format-tag (tag)\r
+ "Format TAG by looking into `notmuch-tag-formats'."\r
+- (let ((formats (assoc tag notmuch-tag-formats)))\r
+- (cond\r
+- ((null formats) ;; - Tag not in `notmuch-tag-formats',\r
+- tag) ;; the format is the tag itself.\r
+- ((null (cdr formats)) ;; - Tag was deliberately hidden,\r
+- nil) ;; no format must be returned\r
+- (t ;; - Tag was found and has formats,\r
+- (let ((tag tag)) ;; we must apply all the formats.\r
+- (dolist (format (cdr formats) tag)\r
+- (setq tag (eval format))))))))\r
++ (let ((formatted (gethash tag notmuch-tag--format-cache 'missing)))\r
++ (when (eq formatted 'missing)\r
++ (let* ((formats\r
++ (save-match-data\r
++ (assoc* tag notmuch-tag-formats\r
++ :test (lambda (tag key)\r
++ (and (eq (string-match key tag) 0)\r
++ (= (match-end 0) (length tag))))))))\r
++ (setq formatted\r
++ (cond\r
++ ((null formats) ;; - Tag not in `notmuch-tag-formats',\r
++ tag) ;; the format is the tag itself.\r
++ ((null (cdr formats)) ;; - Tag was deliberately hidden,\r
++ nil) ;; no format must be returned\r
++ (t ;; - Tag was found and has formats,\r
++ (let ((tag tag)) ;; we must apply all the formats.\r
++ (dolist (format (cdr formats) tag)\r
++ (setq tag (eval format)))))))\r
++ (puthash tag formatted notmuch-tag--format-cache)))\r
++ formatted))\r
+ \r
+ (defun notmuch-tag-format-tags (tags &optional face)\r
+ "Return a string representing formatted TAGS."\r
+-- \r
+1.7.9.1\r
+\r