Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id CC891431FC2 for ; Thu, 24 Oct 2013 14:39:18 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id R3cJTEIAWxAz for ; Thu, 24 Oct 2013 14:39:11 -0700 (PDT) Received: from dmz-mailsec-scanner-2.mit.edu (dmz-mailsec-scanner-2.mit.edu [18.9.25.13]) by olra.theworths.org (Postfix) with ESMTP id 26FD1431FB6 for ; Thu, 24 Oct 2013 14:39:11 -0700 (PDT) X-AuditID: 1209190d-b7f528e0000009b4-08-5269937ead0a Received: from mailhub-auth-1.mit.edu ( [18.9.21.35]) by dmz-mailsec-scanner-2.mit.edu (Symantec Messaging Gateway) with SMTP id 06.26.02484.E7399625; Thu, 24 Oct 2013 17:39:10 -0400 (EDT) Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11]) by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id r9OLd825031538; Thu, 24 Oct 2013 17:39:08 -0400 Received: from drake.dyndns.org (26-4-172.dynamic.csail.mit.edu [18.26.4.172]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id r9OLd6qB010508 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Thu, 24 Oct 2013 17:39:07 -0400 Received: from amthrax by drake.dyndns.org with local (Exim 4.77) (envelope-from ) id 1VZScU-0003Gj-89; Thu, 24 Oct 2013 17:39:06 -0400 From: Austin Clements To: notmuch@notmuchmail.org Subject: [PATCH v2] new: Don't scan unchanged directories with no sub-directories Date: Thu, 24 Oct 2013 17:38:59 -0400 Message-Id: <1382650739-12438-1-git-send-email-amdragon@mit.edu> X-Mailer: git-send-email 1.8.4.rc3 In-Reply-To: <20131024210837.GH20337@mit.edu> References: <20131024210837.GH20337@mit.edu> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrLIsWRmVeSWpSXmKPExsUixCmqrFs3OTPIYPNZdYvrN2cyW3Tc3s3m wOTxbNUtZo+PT2+xBDBFcdmkpOZklqUW6dslcGXM6q0uuCBU8ePMdPYGxvV8XYycHBICJhJb 53WyQ9hiEhfurWfrYuTiEBLYxyjxpfUHE4SzkVHi1OW3jBDOESaJpzunsEM4cxkldr3eyAjS zyagIbFt/3IwW0RAWmLn3dmsIDazgKPEngPLWUBsYYFAidnHb4DVsAioSiy8PwWshlfAQaLn 0lY2iDuUJBae2gYW5xTQlTi07RATiC0koCNxo+kN4wRG/gWMDKsYZVNyq3RzEzNzilOTdYuT E/PyUot0jfRyM0v0UlNKNzGCQ0mSdwfju4NKhxgFOBiVeHg1PqQHCbEmlhVX5h5ilORgUhLl NZ6UGSTEl5SfUpmRWJwRX1Sak1p8iFGCg1lJhHeaHlCONyWxsiq1KB8mJc3BoiTOe5PDPkhI ID2xJDU7NbUgtQgmK8PBoSTBGw8yVLAoNT21Ii0zpwQhzcTBCTKcB2h4FkgNb3FBYm5xZjpE /hSjopQ47waQhABIIqM0D64XFuuvGMWBXhHmrQSp4gGmCbjuV0CDmYAGT1mSBjK4JBEhJdXA KJlbWCsr83PiVstOn8wlwgvv9aa/W/uyUYBb9cuBh0dTtzFudSuwnBcgs2ubzd6F0SYsHGaJ VmsVytnaDJt1nzp/fpFc6M5323NL0UKtogMWs/k8eI63cpxet3ypaXBr9e2TM51fCd46f8/y mf6EMxcSlOdkJxktfKETz9Rh8WVpUo/d5G9LlFiKMxINtZiLihMBqPKXbNACAAA= X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 24 Oct 2013 21:39:18 -0000 This can substantially reduce the cost of notmuch new in some situations, such as when the file system cache is cold or when the Maildir is on NFS. --- This should fix the problem with directories containing symlinks to other directories, but no actual sub-directories. notmuch-new.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/notmuch-new.c b/notmuch-new.c index faa33f1..ba05cb4 100644 --- a/notmuch-new.c +++ b/notmuch-new.c @@ -323,6 +323,35 @@ add_files (notmuch_database_t *notmuch, } db_mtime = directory ? notmuch_directory_get_mtime (directory) : 0; + /* If the directory is unchanged from our last scan and has no + * sub-directories, then return without scanning it at all. In + * some situations, skipping the scan can substantially reduce the + * cost of notmuch new, especially since the huge numbers of files + * in Maildirs make scans expensive, but all files live in leaf + * directories. + * + * To check for sub-directories, we borrow a trick from find, + * kpathsea, and many other UNIX tools: since a directory's link + * count is the number of sub-directories (specifically, their + * '..' entries) plus 2 (the link from the parent and the link for + * '.'). This check is safe even on weird file systems, since + * file systems that can't compute this will return 0 or 1. This + * is safe even on *really* weird file systems like HFS+ that + * mistakenly return the total number of directory entries, since + * that only inflates the count beyond 2. + */ + if (directory && fs_mtime == db_mtime && st.st_nlink == 2) { + /* There's one catch: pass 1 below considers symlinks to + * directories to be directories, but these don't increase the + * file system link count. So, only bail early if the + * database agrees that there are no sub-directories. */ + db_subdirs = notmuch_directory_get_child_directories (directory); + if (!notmuch_filenames_valid (db_subdirs)) + goto DONE; + notmuch_filenames_destroy (db_subdirs); + db_subdirs = NULL; + } + /* If the database knows about this directory, then we sort based * on strcmp to match the database sorting. Otherwise, we can do * inode-based sorting for faster filesystem operation. */ -- 1.8.4.rc3