new: read db_files and db_subdirs only if mtime changed

The db_files and db_subdirs are unnecessary for unchanged directories.

maildir with 10000 e-mails:

old version:
	$ time ./notmuch new
	No new mail.

	real    0m0.053s
	user    0m0.028s
	sys     0m0.026s

new version:
	$ time ./notmuch new
	No new mail.

	real    0m0.032s
	user    0m0.009s
	sys     0m0.023s

Signed-off-by: Karel Zak <kzak@redhat.com>

Reviewed-by:  Austin Clements <amdragon@mit.edu>

Looks good (faster than, but provably equivalent to the original code!
notmuch_directory_get_child_* are side-effect free,
db_files/db_subdirs aren't used between where they were set in the old
code and where they are set in the new code, and db_files/db_subdirs
are initialized to NULL when declared).

Another timing data point:
Old code: ./notmuch new  0.77s user 0.28s system 99% cpu 1.051 total
New code: ./notmuch new  0.09s user 0.27s system 98% cpu 0.368 total
This commit is contained in:
Karel Zak 2011-02-04 22:44:31 +01:00 committed by Carl Worth
parent 2d6718b837
commit b0006b6ea2

View file

@ -247,15 +247,7 @@ add_files_recursive (notmuch_database_t *notmuch,
directory = notmuch_database_get_directory (notmuch, path); directory = notmuch_database_get_directory (notmuch, path);
db_mtime = notmuch_directory_get_mtime (directory); db_mtime = notmuch_directory_get_mtime (directory);
if (db_mtime == 0) { new_directory = db_mtime ? FALSE : TRUE;
new_directory = TRUE;
db_files = NULL;
db_subdirs = NULL;
} else {
new_directory = FALSE;
db_files = notmuch_directory_get_child_files (directory);
db_subdirs = notmuch_directory_get_child_directories (directory);
}
/* If the database knows about this directory, then we sort based /* If the database knows about this directory, then we sort based
* on strcmp to match the database sorting. Otherwise, we can do * on strcmp to match the database sorting. Otherwise, we can do
@ -328,6 +320,11 @@ add_files_recursive (notmuch_database_t *notmuch,
if (fs_mtime == db_mtime) if (fs_mtime == db_mtime)
goto DONE; goto DONE;
if (!new_directory) {
db_files = notmuch_directory_get_child_files (directory);
db_subdirs = notmuch_directory_get_child_directories (directory);
}
/* Pass 2: Scan for new files, removed files, and removed directories. */ /* Pass 2: Scan for new files, removed files, and removed directories. */
for (i = 0; i < num_fs_entries; i++) for (i = 0; i < num_fs_entries; i++)
{ {