--- /dev/null
+Return-Path: <amthrax@awakening.csail.mit.edu>\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 0B066431FB6\r
+ for <notmuch@notmuchmail.org>; Thu, 9 Dec 2010 13:05:38 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ 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 ZSHQ38EDw+G4 for <notmuch@notmuchmail.org>;\r
+ Thu, 9 Dec 2010 13:05:37 -0800 (PST)\r
+Received: from dmz-mailsec-scanner-4.mit.edu (DMZ-MAILSEC-SCANNER-4.MIT.EDU\r
+ [18.9.25.15])\r
+ by olra.theworths.org (Postfix) with ESMTP id 7B43C431FB5\r
+ for <notmuch@notmuchmail.org>; Thu, 9 Dec 2010 13:05:37 -0800 (PST)\r
+X-AuditID: 1209190f-b7c1dae000000a2b-6d-4d014374b292\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+ by dmz-mailsec-scanner-4.mit.edu (Symantec Brightmail Gateway) with\r
+ SMTP id DB.E2.02603.473410D4; Thu, 9 Dec 2010 16:00:36 -0500 (EST)\r
+Received: from outgoing.mit.edu (OUTGOING-AUTH.MIT.EDU [18.7.22.103])\r
+ by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id oB9L0awt030973; \r
+ Thu, 9 Dec 2010 16:00:36 -0500\r
+Received: from awakening.csail.mit.edu (awakening.csail.mit.edu [18.26.4.91])\r
+ (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.6/8.12.4) with ESMTP id oB9L0YMl010121\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Thu, 9 Dec 2010 16:00:35 -0500 (EST)\r
+Received: from amthrax by awakening.csail.mit.edu with local (Exim 4.72)\r
+ (envelope-from <amthrax@awakening.csail.mit.edu>)\r
+ id 1PQnbO-0007Hy-Pf; Thu, 09 Dec 2010 16:00:34 -0500\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 5/5] Add the tag list to the unified message metadata pass.\r
+Date: Thu, 9 Dec 2010 15:59:56 -0500\r
+Message-Id: <1291928396-27937-6-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.2.3\r
+In-Reply-To: <1291928396-27937-1-git-send-email-amdragon@mit.edu>\r
+References: <1291928396-27937-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker: AAAAARbjX5o=\r
+Cc: Austin Clements <amdragon@mit.edu>\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: Thu, 09 Dec 2010 21:05:38 -0000\r
+\r
+Now each caller of notmuch_message_get_tags only gets a new iterator,\r
+instead of a whole new list. In principle this could cause problems\r
+with iterating while modifying tags, but through the magic of talloc\r
+references, we keep the old tag list alive even after the cache in the\r
+message object is invalidated.\r
+\r
+This reduces my index search from the 3.102 seconds before the unified\r
+metadata pass to 1.811 seconds (1.7X faster). Combined with the\r
+thread search optimization in b3caef1f0659dac8183441357c8fee500a940889,\r
+that makes this query 2.5X faster than when I started.\r
+---\r
+ lib/message.cc | 32 ++++++++++++++++++++++++--------\r
+ 1 files changed, 24 insertions(+), 8 deletions(-)\r
+\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index adb205f..cc75a71 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -32,6 +32,7 @@ struct _notmuch_message {\r
+ char *message_id;\r
+ char *thread_id;\r
+ char *in_reply_to;\r
++ notmuch_string_list_t *tag_list;\r
+ notmuch_string_list_t *filename_term_list;\r
+ notmuch_string_list_t *filename_list;\r
+ char *author;\r
+@@ -102,6 +103,7 @@ _notmuch_message_create_for_document (const void *talloc_owner,\r
+ message->message_id = NULL;\r
+ message->thread_id = NULL;\r
+ message->in_reply_to = NULL;\r
++ message->tag_list = NULL;\r
+ message->filename_term_list = NULL;\r
+ message->filename_list = NULL;\r
+ message->message_file = NULL;\r
+@@ -293,6 +295,7 @@ _notmuch_message_ensure_metadata (notmuch_message_t *message)\r
+ {\r
+ Xapian::TermIterator i, end;\r
+ const char *thread_prefix = _find_prefix ("thread"),\r
++ *tag_prefix = _find_prefix ("tag"),\r
+ *id_prefix = _find_prefix ("id"),\r
+ *filename_prefix = _find_prefix ("file-direntry"),\r
+ *replyto_prefix = _find_prefix ("replyto");\r
+@@ -311,6 +314,13 @@ _notmuch_message_ensure_metadata (notmuch_message_t *message)\r
+ message->thread_id =\r
+ _notmuch_message_get_term (message, i, end, thread_prefix);\r
+ \r
++ /* Get tags */\r
++ if (!message->tag_list) {\r
++ message->tag_list =\r
++ _notmuch_get_terms_with_prefix (message, i, end, tag_prefix);\r
++ _notmuch_string_list_sort (message->tag_list);\r
++ }\r
++\r
+ /* Get id */\r
+ assert (strcmp (thread_prefix, id_prefix) < 0);\r
+ if (!message->message_id)\r
+@@ -345,6 +355,11 @@ _notmuch_message_invalidate_metadata (notmuch_message_t *message,\r
+ message->thread_id = NULL;\r
+ }\r
+ \r
++ if (strcmp ("tag", prefix_name) == 0) {\r
++ talloc_unlink (message, message->tag_list);\r
++ message->tag_list = NULL;\r
++ }\r
++\r
+ if (strcmp ("file-direntry", prefix_name) == 0) {\r
+ talloc_free (message->filename_term_list);\r
+ talloc_free (message->filename_list);\r
+@@ -645,14 +660,14 @@ notmuch_message_get_date (notmuch_message_t *message)\r
+ notmuch_tags_t *\r
+ notmuch_message_get_tags (notmuch_message_t *message)\r
+ {\r
+- Xapian::TermIterator i, end;\r
+- notmuch_string_list_t *tags;\r
+- i = message->doc.termlist_begin();\r
+- end = message->doc.termlist_end();\r
+- tags = _notmuch_get_terms_with_prefix (message, i, end,\r
+- _find_prefix ("tag"));\r
+- _notmuch_string_list_sort (tags);\r
+- return _notmuch_tags_create (message, tags, TRUE);\r
++ if (!message->tag_list)\r
++ _notmuch_message_ensure_metadata (message);\r
++\r
++ /* We tell the iterator to add a talloc reference to the\r
++ * underlying list, rather than stealing it. As a result, it's\r
++ * possible to modify the message tags while iterating because\r
++ * the iterator will keep the current list alive. */\r
++ return _notmuch_tags_create (message, message->tag_list, FALSE);\r
+ }\r
+ \r
+ const char *\r
+@@ -1210,6 +1225,7 @@ notmuch_message_remove_all_tags (notmuch_message_t *message)\r
+ if (! message->frozen)\r
+ _notmuch_message_sync (message);\r
+ \r
++ talloc_free (tags);\r
+ return NOTMUCH_STATUS_SUCCESS;\r
+ }\r
+ \r
+-- \r
+1.7.2.3\r
+\r