--- /dev/null
+Return-Path: <aclements@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 19362431FD8\r
+ for <notmuch@notmuchmail.org>; Sun, 12 Oct 2014 23:20:26 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_MED=-2.3] 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 mq5YbDnu2QYv for <notmuch@notmuchmail.org>;\r
+ Sun, 12 Oct 2014 23:20:18 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-6.mit.edu (dmz-mailsec-scanner-6.mit.edu\r
+ [18.7.68.35])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 7E3C2431FBD\r
+ for <notmuch@notmuchmail.org>; Sun, 12 Oct 2014 23:20:14 -0700 (PDT)\r
+X-AuditID: 12074423-f799d6d00000337c-0b-543b6f1c3870\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+ (using TLS with cipher AES256-SHA (256/256 bits))\r
+ (Client did not present a certificate)\r
+ by dmz-mailsec-scanner-6.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id BF.74.13180.C1F6B345; Mon, 13 Oct 2014 02:20:12 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id s9D6K8wt019181; \r
+ Mon, 13 Oct 2014 02:20:08 -0400\r
+Received: from drake.dyndns.org ([73.162.189.21]) (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s9D6K5Ve031972\r
+ (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT);\r
+ Mon, 13 Oct 2014 02:20:07 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.84)\r
+ (envelope-from <aclements@csail.mit.edu>)\r
+ id 1XdYzF-0000U8-8L; Mon, 13 Oct 2014 02:20:05 -0400\r
+From: Austin Clements <aclements@csail.mit.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [WIP PATCH 1/4] lib: Only sync modified message documents\r
+Date: Mon, 13 Oct 2014 02:20:00 -0400\r
+Message-Id: <1413181203-1676-2-git-send-email-aclements@csail.mit.edu>\r
+X-Mailer: git-send-email 2.1.0\r
+In-Reply-To: <1413181203-1676-1-git-send-email-aclements@csail.mit.edu>\r
+References: <1413181203-1676-1-git-send-email-aclements@csail.mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFtrIIsWRmVeSWpSXmKPExsUixG6nriuTbx1i0Phc3eL6zZnMDowez1bd\r
+ Yg5gjOKySUnNySxLLdK3S+DKaJ6yjL1gj1jFm5lHmRoYfwp2MXJySAiYSOy7cosNwhaTuHBv\r
+ PZDNxSEkMJtJ4vfZ21DORkaJtXdgnOVMEt9erWeFcJYwSpxq6GUF6WcT0JdYsXYSmC0iIC2x\r
+ 8+5sIJuDg1lATeJPlwqIKSzgJPFvtQ5IBYuAqkTbrxcsIGFeATeJTb8DIY6Qk9iw+z8jiM0p\r
+ 4C5xceJZZhBbCKjkWNs1lgmM/AsYGVYxyqbkVunmJmbmFKcm6xYnJ+blpRbpmunlZpbopaaU\r
+ bmIEh4yL8g7GPweVDjEKcDAq8fBa/rEKEWJNLCuuzD3EKMnBpCTKG5BmHSLEl5SfUpmRWJwR\r
+ X1Sak1p8iFGCg1lJhPetDVCONyWxsiq1KB8mJc3BoiTOu+kHX4iQQHpiSWp2ampBahFMVoaD\r
+ Q0mCVz0PqFGwKDU9tSItM6cEIc3EwQkynAdoeEsOyPDigsTc4sx0iPwpRkUpcd7vuUAJAZBE\r
+ RmkeXC8spl8xigO9IswbB7KCB5gO4LpfAQ1mAhp8tMscZHBJIkJKqoHRKvn1kWvVgTzq29VT\r
+ znlHTdfc9OtsTXB7vKLjogk6urG7nj8zrfAR60+o0FirNtmN0znkvf+dQq9c/a3Pwy+1lCdc\r
+ 3OWcy5R/1NpBofE2j7qBdATLFFblOWvNNqxTdte9wnE9NIL56sStt5eWXrqrWtYmmxy1dUXo\r
+ zSvcx7paObLk9x5bpqPEUpyRaKjFXFScCAAjIfjwxAIAAA==\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: Mon, 13 Oct 2014 06:20:26 -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 a7a13cc..cf2fd7c 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
+@@ -538,6 +541,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
+@@ -791,6 +795,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
+@@ -988,6 +993,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
+@@ -999,8 +1005,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
+@@ -1075,6 +1085,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
+@@ -1143,6 +1154,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.0\r
+\r