--- /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 2526B431FD9\r
+ for <notmuch@notmuchmail.org>; Sat, 22 Mar 2014 04:51:36 -0700 (PDT)\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 YJUJPBYsmaGm for <notmuch@notmuchmail.org>;\r
+ Sat, 22 Mar 2014 04:51:30 -0700 (PDT)\r
+Received: from mail-we0-f170.google.com (mail-we0-f170.google.com\r
+ [74.125.82.170]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 019AD431FC3\r
+ for <notmuch@notmuchmail.org>; Sat, 22 Mar 2014 04:51:26 -0700 (PDT)\r
+Received: by mail-we0-f170.google.com with SMTP id w61so2284627wes.29\r
+ for <notmuch@notmuchmail.org>; Sat, 22 Mar 2014 04:51:25 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:in-reply-to:references;\r
+ bh=EFSKQjZN1z/OWg512QtUAk02B958coxE0/mG1L2Apw0=;\r
+ b=Ax+oYVXRxNcxsihfgZqQysA8rydj0GlwauOCJewB5KPjUOikATF6QWFSCWh3EG8Nk0\r
+ 79/EWkBSIET3pf+6f7E9kbUNSngqHq+sahcWsZC+jZTXdRm+2wFxrvLhTE16ZeyorwXm\r
+ g+9aOucUmfM0v35ZTIj3RZ152Db98fxn0B3sVRs86N2ovi8C+XPosHglJLlg3dR8WHh1\r
+ SkNkm5yMdLQrpyBJt0b5KQRrvx8cfkPma4VChaSa5Y2kFtvERm2A03prthCDPUOxBT4U\r
+ U6OKGRciqfZmCkUUAJpKeYDLm8/FagptnOxj8WSfVPTiWnxuK9UxIPRT5h3THFz03q2W\r
+ Oj2A==\r
+X-Received: by 10.180.77.49 with SMTP id p17mr3485871wiw.4.1395489085798;\r
+ Sat, 22 Mar 2014 04:51:25 -0700 (PDT)\r
+Received: from localhost (93-97-24-31.zone5.bethere.co.uk. [93.97.24.31])\r
+ by mx.google.com with ESMTPSA id uq2sm16881230wjc.5.2014.03.22.04.51.24\r
+ for <multiple recipients>\r
+ (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+ Sat, 22 Mar 2014 04:51:25 -0700 (PDT)\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: notmuch@notmuchmail.org,\r
+ amdragon@mit.edu\r
+Subject: [Patch v4 2/8] Make keys of notmuch-tag-formats regexps and use\r
+ caching\r
+Date: Sat, 22 Mar 2014 11:51:06 +0000\r
+Message-Id: <1395489072-19001-3-git-send-email-markwalters1009@gmail.com>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1395489072-19001-1-git-send-email-markwalters1009@gmail.com>\r
+References: <1395489072-19001-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: Sat, 22 Mar 2014 11:51:36 -0000\r
+\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+\r
+This modifies `notmuch-tag-format-tag' to treat the keys of\r
+`notmuch-tag-formats' as (anchored) regexps, rather than literal\r
+strings. This is clearly more flexible, as it allows for prefix\r
+matching, defining a fallback format, etc. This may cause compatibility\r
+problems if people have customized `notmuch-tag-formats' to match tags\r
+that contain regexp specials, but this seems unlikely.\r
+\r
+Regular expression matching has quite a performance hit over string\r
+lookup, so this also introduces a simple cache from exact tags to\r
+formatted strings. The number of unique tags is likely to be quite\r
+small, so this cache should have a high hit rate. In addition to\r
+eliminating the regexp lookup in the common case, this cache stores\r
+fully formatted tags, eliminating the repeated evaluation of potentially\r
+expensive, user-specified formatting code. This makes regexp lookup at\r
+least as fast as assoc for unformatted tags (e.g., inbox) and *faster*\r
+than the current code for formatted tags (e.g., unread):\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
+(Though even at 7.2 usec, tag formatting is not our top bottleneck.)\r
+\r
+This cache must be explicitly cleared to keep it coherent, so this adds\r
+the appropriate clearing calls.\r
+---\r
+ emacs/notmuch-show.el | 1 +\r
+ emacs/notmuch-tag.el | 76 ++++++++++++++++++++++++++++++++++---------------\r
+ emacs/notmuch-tree.el | 1 +\r
+ emacs/notmuch.el | 1 +\r
+ 4 files changed, 56 insertions(+), 23 deletions(-)\r
+\r
+diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el\r
+index b8782dd..019f51d 100644\r
+--- a/emacs/notmuch-show.el\r
++++ b/emacs/notmuch-show.el\r
+@@ -1145,6 +1145,7 @@ (defun notmuch-show-build-buffer ()\r
+ ;; Don't track undo information for this buffer\r
+ (set 'buffer-undo-list t)\r
+ \r
++ (notmuch-tag-clear-cache)\r
+ (erase-buffer)\r
+ (goto-char (point-min))\r
+ (save-excursion\r
+diff --git a/emacs/notmuch-tag.el b/emacs/notmuch-tag.el\r
+index 41b1687..42c425e 100644\r
+--- a/emacs/notmuch-tag.el\r
++++ b/emacs/notmuch-tag.el\r
+@@ -34,17 +34,21 @@ (defcustom notmuch-tag-formats\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
+@@ -56,7 +60,7 @@ (defcustom notmuch-tag-formats\r
+ \r
+ :group 'notmuch-search\r
+ :group 'notmuch-show\r
+- :type '(alist :key-type (string :tag "Tag")\r
++ :type '(alist :key-type (regexp :tag "Tag")\r
+ :extra-offset -3\r
+ :value-type\r
+ (radio :format "%v"\r
+@@ -135,18 +139,44 @@ (defun notmuch-tag-tag-icon ()\r
+ </g>\r
+ </svg>")\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
++ (clrhash notmuch-tag--format-cache))\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
++ "Format TAG by according to `notmuch-tag-formats'.\r
++\r
++Callers must ensure that the tag format cache has been recently cleared\r
++via `notmuch-tag-clear-cache' before using this function. For example,\r
++it would be appropriate to clear the cache just prior to filling a\r
++buffer that uses formatted tags."\r
++\r
++ (let ((formatted (gethash tag notmuch-tag--format-cache 'missing)))\r
++ (when (eq formatted 'missing)\r
++ (let* ((formats\r
++ (save-match-data\r
++ ;; Don't use assoc-default since there's no way to\r
++ ;; distinguish a missing key from a present key with a\r
++ ;; null cdr:.\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
+diff --git a/emacs/notmuch-tree.el b/emacs/notmuch-tree.el\r
+index e3aa2cd..c78d9de 100644\r
+--- a/emacs/notmuch-tree.el\r
++++ b/emacs/notmuch-tree.el\r
+@@ -881,6 +881,7 @@ (defun notmuch-tree-worker (basic-query &optional query-context target open-targ\r
+ (message-arg "--entire-thread"))\r
+ (if (equal (car (process-lines notmuch-command "count" search-args)) "0")\r
+ (setq search-args basic-query))\r
++ (notmuch-tag-clear-cache)\r
+ (let ((proc (notmuch-start-notmuch\r
+ "notmuch-tree" (current-buffer) #'notmuch-tree-process-sentinel\r
+ "show" "--body=false" "--format=sexp"\r
+diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
+index 5cddaac..93a6d8b 100644\r
+--- a/emacs/notmuch.el\r
++++ b/emacs/notmuch.el\r
+@@ -888,6 +888,7 @@ (defun notmuch-search (&optional query oldest-first target-thread target-line)\r
+ (set 'notmuch-search-oldest-first oldest-first)\r
+ (set 'notmuch-search-target-thread target-thread)\r
+ (set 'notmuch-search-target-line target-line)\r
++ (notmuch-tag-clear-cache)\r
+ (let ((proc (get-buffer-process (current-buffer)))\r
+ (inhibit-read-only t))\r
+ (if proc\r
+-- \r
+1.7.10.4\r
+\r