[PATCH] new: Don't scan unchanged directories with no sub-directories
authorAustin Clements <amdragon@MIT.EDU>
Thu, 24 Oct 2013 20:33:42 +0000 (16:33 +2000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:57:37 +0000 (09:57 -0800)
6d/b9fb36046010c6fbd2764c51f456d14fb45816 [new file with mode: 0644]

diff --git a/6d/b9fb36046010c6fbd2764c51f456d14fb45816 b/6d/b9fb36046010c6fbd2764c51f456d14fb45816
new file mode 100644 (file)
index 0000000..fbac8b0
--- /dev/null
@@ -0,0 +1,110 @@
+Return-Path: <amdragon@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 EC2F9431FBD\r
+       for <notmuch@notmuchmail.org>; Thu, 24 Oct 2013 13:33:54 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[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 252tNtMlqSzj for <notmuch@notmuchmail.org>;\r
+       Thu, 24 Oct 2013 13:33:48 -0700 (PDT)\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 A28E8431FBC\r
+       for <notmuch@notmuchmail.org>; Thu, 24 Oct 2013 13:33:48 -0700 (PDT)\r
+X-AuditID: 1209190f-b7fa08e0000009c6-5d-5269842c51d6\r
+Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
+       by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP\r
+       id 40.D7.02502.C2489625; Thu, 24 Oct 2013 16:33:48 -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 r9OKXjG9030310; \r
+       Thu, 24 Oct 2013 16:33:46 -0400\r
+Received: from drake.dyndns.org (26-4-172.dynamic.csail.mit.edu [18.26.4.172])\r
+       (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 r9OKXhnO016620\r
+       (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+       Thu, 24 Oct 2013 16:33:45 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+       (envelope-from <amdragon@mit.edu>)\r
+       id 1VZRbD-0006Oz-U8; Thu, 24 Oct 2013 16:33:43 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH] new: Don't scan unchanged directories with no sub-directories\r
+Date: Thu, 24 Oct 2013 16:33:42 -0400\r
+Message-Id: <1382646822-24556-1-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.8.4.rc3\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFlrMIsWRmVeSWpSXmKPExsUixG6nrqvTkhlkMH87p8X1mzOZLTpu72Zz\r
+       YPJ4tuoWs8fHp7dYApiiuGxSUnMyy1KL9O0SuDKafzxjLZjKV9Fy/B9bA+Ndri5GTg4JAROJ\r
+       x23zWCBsMYkL99azgdhCAvsYJbYfcuti5AKyNzJK7Pj/kB0icYRJ4tgzJ4jEXEaJT62LwbrZ\r
+       BDQktu1fzghiiwhIS+y8O5sVxGYWcJTYc2A5WI2wgJ/E/SkrgeIcHCwCqhJX5vCBhHkFHCSu\r
+       tR5kgzhCSWLhqW2sExh5FzAyrGKUTcmt0s1NzMwpTk3WLU5OzMtLLdI10cvNLNFLTSndxAgK\r
+       C05J/h2M3w4qHWIU4GBU4uHV+JAeJMSaWFZcmXuIUZKDSUmUd11TZpAQX1J+SmVGYnFGfFFp\r
+       TmrxIUYJDmYlEd5pekA53pTEyqrUonyYlDQHi5I4700O+yAhgfTEktTs1NSC1CKYrAwHh5IE\r
+       7w2QoYJFqempFWmZOSUIaSYOTpDhPEDDn4HU8BYXJOYWZ6ZD5E8xKkqJ8/4ESQiAJDJK8+B6\r
+       YXH7ilEc6BVh3n8gVTzAmIfrfgU0mAlo8JQlaSCDSxIRUlINjIHzNLe++KZvH3f4ssqzHxt2\r
+       CO0puR7aKJWqaqcruuPxpV4uA40E/tuvGbZbFt2cdnC9y+2FD3ojSoVWTHMr0zw6f3NB1sTO\r
+       RaGMLIs7TvUJGIsen1e6Ivv+v30f1dK4M87pzuY/YsAUzidVsYlvnmyEk37hkZJpExQcu39a\r
+       zuyWqf2xrjLtoxJLcUaioRZzUXEiAB53vU+2AgAA\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, 24 Oct 2013 20:33:55 -0000\r
+\r
+This can substantially reduce the cost of notmuch new in some\r
+situations, such as when the file system cache is cold or when the\r
+Maildir is on NFS.\r
+---\r
+ notmuch-new.c | 20 ++++++++++++++++++++\r
+ 1 file changed, 20 insertions(+)\r
+\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index faa33f1..364c73a 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -323,6 +323,26 @@ add_files (notmuch_database_t *notmuch,\r
+     }\r
+     db_mtime = directory ? notmuch_directory_get_mtime (directory) : 0;\r
\r
++    /* If the directory is unchanged from our last scan and has no\r
++     * sub-directories, then return without scanning it at all.  In\r
++     * some situations, skipping the scan can substantially reduce the\r
++     * cost of notmuch new, especially since the huge numbers of files\r
++     * in Maildirs make scans expensive, but all files live in leaf\r
++     * directories.\r
++     *\r
++     * To check for sub-directories, we borrow a trick from find,\r
++     * kpathsea, and many other UNIX tools: since a directory's link\r
++     * count is the number of sub-directories (specifically, their\r
++     * '..' entries) plus 2 (the link from the parent and the link for\r
++     * '.').  This check is safe even on weird file systems, since\r
++     * file systems that can't compute this will return 0 or 1.  This\r
++     * is safe even on *really* weird file systems like HFS+ that\r
++     * mistakenly return the total number of directory entries, since\r
++     * that only inflates the count beyond 2.\r
++     */\r
++    if (directory && fs_mtime == db_mtime && st.st_nlink == 2)\r
++      goto DONE;\r
++\r
+     /* If the database knows about this directory, then we sort based\r
+      * on strcmp to match the database sorting. Otherwise, we can do\r
+      * inode-based sorting for faster filesystem operation. */\r
+-- \r
+1.8.4.rc3\r
+\r