--- /dev/null
+Return-Path: <bremner@tesseract.cs.unb.ca>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 9493B6DE1BAD\r
+ for <notmuch@notmuchmail.org>; Sun, 5 Apr 2015 16:02:56 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.529\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.529 tagged_above=-999 required=5 tests=[AWL=0.519, \r
+ T_HEADER_FROM_DIFFERENT_DOMAINS=0.01] autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id lew0B-DmIy94 for <notmuch@notmuchmail.org>;\r
+ Sun, 5 Apr 2015 16:02:55 -0700 (PDT)\r
+Received: from mx.xen14.node3324.gplhost.com (gitolite.debian.net\r
+ [87.98.215.224])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 02D6A6DE1BA5\r
+ for <notmuch@notmuchmail.org>; Sun, 5 Apr 2015 16:02:51 -0700 (PDT)\r
+Received: from remotemail by mx.xen14.node3324.gplhost.com with local (Exim\r
+ 4.80) (envelope-from <bremner@tesseract.cs.unb.ca>)\r
+ id 1YetXS-0002ou-AV; Sun, 05 Apr 2015 23:01:10 +0000\r
+Received: (nullmailer pid 2237 invoked by uid 1000); Sun, 05 Apr 2015\r
+ 22:59:24 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [WIP2 01/12] lib: Only sync modified message documents\r
+Date: Mon, 6 Apr 2015 07:59:03 +0900\r
+Message-Id: <1428274754-1698-2-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <1428274754-1698-1-git-send-email-david@tethera.net>\r
+References: <1428274754-1698-1-git-send-email-david@tethera.net>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.18\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: Sun, 05 Apr 2015 23:02:56 -0000\r
+\r
+From: Austin Clements <amdragon@mit.edu>\r
+\r
+Previously, we updated the database copy of a message on every call to\r
+_notmuch_message_sync, even if nothing had changed. In particular,\r
+this always happens on a thaw, so a freeze/thaw pair with no\r
+modifications between still caused a database update.\r
+\r
+We only modify message documents in a handful of places, so keep track\r
+of whether the document has been modified and only sync it when\r
+necessary. This will be particularly important when we add message\r
+revision tracking.\r
+---\r
+ lib/message.cc | 12 ++++++++++++\r
+ 1 file changed, 12 insertions(+)\r
+\r
+diff --git a/lib/message.cc b/lib/message.cc\r
+index 5bc7aff..1ddce3c 100644\r
+--- a/lib/message.cc\r
++++ b/lib/message.cc\r
+@@ -43,6 +43,9 @@ struct visible _notmuch_message {\r
+ * if each flag has been initialized. */\r
+ unsigned long lazy_flags;\r
+ \r
++ /* Message document modified since last sync */\r
++ notmuch_bool_t modified;\r
++\r
+ Xapian::Document doc;\r
+ Xapian::termcount termpos;\r
+ };\r
+@@ -539,6 +542,7 @@ _notmuch_message_remove_terms (notmuch_message_t *message, const char *prefix)\r
+ \r
+ try {\r
+ message->doc.remove_term ((*i));\r
++ message->modified = TRUE;\r
+ } catch (const Xapian::InvalidArgumentError) {\r
+ /* Ignore failure to remove non-existent term. */\r
+ }\r
+@@ -793,6 +797,7 @@ void\r
+ _notmuch_message_clear_data (notmuch_message_t *message)\r
+ {\r
+ message->doc.set_data ("");\r
++ message->modified = TRUE;\r
+ }\r
+ \r
+ static void\r
+@@ -990,6 +995,7 @@ _notmuch_message_set_header_values (notmuch_message_t *message,\r
+ Xapian::sortable_serialise (time_value));\r
+ message->doc.add_value (NOTMUCH_VALUE_FROM, from);\r
+ message->doc.add_value (NOTMUCH_VALUE_SUBJECT, subject);\r
++ message->modified = TRUE;\r
+ }\r
+ \r
+ /* Synchronize changes made to message->doc out into the database. */\r
+@@ -1001,8 +1007,12 @@ _notmuch_message_sync (notmuch_message_t *message)\r
+ if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)\r
+ return;\r
+ \r
++ if (! message->modified)\r
++ return;\r
++\r
+ db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);\r
+ db->replace_document (message->doc_id, message->doc);\r
++ message->modified = FALSE;\r
+ }\r
+ \r
+ /* Delete a message document from the database. */\r
+@@ -1077,6 +1087,7 @@ _notmuch_message_add_term (notmuch_message_t *message,\r
+ return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;\r
+ \r
+ message->doc.add_term (term, 0);\r
++ message->modified = TRUE;\r
+ \r
+ talloc_free (term);\r
+ \r
+@@ -1145,6 +1156,7 @@ _notmuch_message_remove_term (notmuch_message_t *message,\r
+ \r
+ try {\r
+ message->doc.remove_term (term);\r
++ message->modified = TRUE;\r
+ } catch (const Xapian::InvalidArgumentError) {\r
+ /* We'll let the philosopher's try to wrestle with the\r
+ * question of whether failing to remove that which was not\r
+-- \r
+2.1.4\r
+\r