Re: [PATCH 0/4] Allow specifying alternate names for addresses in other_email
[notmuch-archives.git] / fa / e444ab1998c07b5ed90667851e343366cf9eb4
1 Return-Path: <stewart@flamingspork.com>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id AE743431FBC\r
6         for <notmuch@notmuchmail.org>; Tue, 17 Nov 2009 17:56:50 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 Received: from olra.theworths.org ([127.0.0.1])\r
9         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
10         with ESMTP id 48J5iP+XseiQ for <notmuch@notmuchmail.org>;\r
11         Tue, 17 Nov 2009 17:56:49 -0800 (PST)\r
12 X-Greylist: delayed 3037 seconds by postgrey-1.32 at olra;\r
13         Tue, 17 Nov 2009 17:56:48 PST\r
14 Received: from kaylee.flamingspork.com (kaylee.flamingspork.com\r
15         [74.207.245.61])\r
16         by olra.theworths.org (Postfix) with ESMTP id E59B3431FAE\r
17         for <notmuch@notmuchmail.org>; Tue, 17 Nov 2009 17:56:48 -0800 (PST)\r
18 Received: from willster (localhost [127.0.0.1])\r
19         by kaylee.flamingspork.com (Postfix) with ESMTPS id 348C16333;\r
20         Wed, 18 Nov 2009 01:56:26 +0000 (UTC)\r
21 Received: by willster (Postfix, from userid 1000)\r
22         id 186B710F5D9A; Wed, 18 Nov 2009 12:56:46 +1100 (EST)\r
23 From: Stewart Smith <stewart@flamingspork.com>\r
24 To: notmuch@notmuchmail.org\r
25 Date: Wed, 18 Nov 2009 12:56:40 +1100\r
26 Message-Id: <1258509400-32511-1-git-send-email-stewart@flamingspork.com>\r
27 X-Mailer: git-send-email 1.6.3.3\r
28 Subject: [notmuch] [PATCH 2/2] Read mail directory in inode number order\r
29 X-BeenThere: notmuch@notmuchmail.org\r
30 X-Mailman-Version: 2.1.12\r
31 Precedence: list\r
32 List-Id: "Use and development of the notmuch mail system."\r
33         <notmuch.notmuchmail.org>\r
34 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
35         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
36 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
37 List-Post: <mailto:notmuch@notmuchmail.org>\r
38 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
39 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
40         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
41 X-List-Received-Date: Wed, 18 Nov 2009 01:56:50 -0000\r
42 \r
43 This gives a rather decent reduction in number of seeks required when\r
44 reading a Maildir that isn't in pagecache.\r
45 \r
46 Most filesystems give some locality on disk based on inode numbers.\r
47 In ext[234] this is the inode tables, in XFS groups of sequential inode\r
48 numbers are together on disk and the most significant bits indicate\r
49 allocation group (i.e inode 1,000,000 is always after inode 1,000).\r
50 \r
51 With this patch, we read in the whole directory, sort by inode number\r
52 before stat()ing the contents.\r
53 \r
54 Ideally, directory is sequential and then we make one scan through the\r
55 file system stat()ing.\r
56 \r
57 Since the universe is not ideal, we'll probably seek during reading the\r
58 directory and a fair bit while reading the inodes themselves.\r
59 \r
60 However... with readahead, and stat()ing in inode order, we should be\r
61 in the best place possible to hit the cache.\r
62 \r
63 In a (not very good) benchmark of "how long does it take to find the first\r
64 15,000 messages in my Maildir after 'echo 3 > /proc/sys/vm/drop_caches'",\r
65 this patch consistently cut at least 8 seconds off the scan time.\r
66 \r
67 Without patch: 50 seconds\r
68 With patch: 38-42 seconds.\r
69 \r
70 (I did this in a previous maildir reading project and saw large improvements too)\r
71 ---\r
72  notmuch-new.c |   32 +++++++++++++++-----------------\r
73  1 files changed, 15 insertions(+), 17 deletions(-)\r
74 \r
75 diff --git a/notmuch-new.c b/notmuch-new.c\r
76 index 83a05ba..11fad8c 100644\r
77 --- a/notmuch-new.c\r
78 +++ b/notmuch-new.c\r
79 @@ -73,6 +73,11 @@ add_files_print_progress (add_files_state_t *state)\r
80      fflush (stdout);\r
81  }\r
82  \r
83 +static int ino_cmp(const struct dirent **a, const struct dirent **b)\r
84 +{\r
85 +  return ((*a)->d_ino < (*b)->d_ino)? -1: 1;\r
86 +}\r
87 +\r
88  /* Examine 'path' recursively as follows:\r
89   *\r
90   *   o Ask the filesystem for the mtime of 'path' (path_mtime)\r
91 @@ -100,13 +105,12 @@ add_files_recursive (notmuch_database_t *notmuch,\r
92                      add_files_state_t *state)\r
93  {\r
94      DIR *dir = NULL;\r
95 -    struct dirent *e, *entry = NULL;\r
96 -    int entry_length;\r
97 -    int err;\r
98 +    struct dirent *entry = NULL;\r
99      char *next = NULL;\r
100      time_t path_mtime, path_dbtime;\r
101      notmuch_status_t status, ret = NOTMUCH_STATUS_SUCCESS;\r
102      notmuch_message_t *message = NULL;\r
103 +    struct dirent **namelist = NULL;\r
104  \r
105      /* If we're told to, we bail out on encountering a read-only\r
106       * directory, (with this being a clear clue from the user to\r
107 @@ -122,31 +126,23 @@ add_files_recursive (notmuch_database_t *notmuch,\r
108      path_mtime = st->st_mtime;\r
109  \r
110      path_dbtime = notmuch_database_get_timestamp (notmuch, path);\r
111 +    int n_entries= scandir(path, &namelist, 0, ino_cmp);\r
112  \r
113 -    dir = opendir (path);\r
114 -    if (dir == NULL) {\r
115 +    if (n_entries == -1) {\r
116         fprintf (stderr, "Error opening directory %s: %s\n",\r
117                  path, strerror (errno));\r
118         ret = NOTMUCH_STATUS_FILE_ERROR;\r
119         goto DONE;\r
120      }\r
121  \r
122 -    entry_length = offsetof (struct dirent, d_name) +\r
123 -       pathconf (path, _PC_NAME_MAX) + 1;\r
124 -    entry = malloc (entry_length);\r
125 +    int i=0;\r
126  \r
127      while (!interrupted) {\r
128 -       err = readdir_r (dir, entry, &e);\r
129 -       if (err) {\r
130 -           fprintf (stderr, "Error reading directory: %s\n",\r
131 -                    strerror (errno));\r
132 -           ret = NOTMUCH_STATUS_FILE_ERROR;\r
133 -           goto DONE;\r
134 -       }\r
135 -\r
136 -       if (e == NULL)\r
137 +       if (i == n_entries)\r
138             break;\r
139  \r
140 +        entry= namelist[i++];\r
141 +\r
142         /* If this directory hasn't been modified since the last\r
143          * add_files, then we only need to look further for\r
144          * sub-directories. */\r
145 @@ -243,6 +239,8 @@ add_files_recursive (notmuch_database_t *notmuch,\r
146         free (entry);\r
147      if (dir)\r
148         closedir (dir);\r
149 +    if (namelist)\r
150 +       free (namelist);\r
151  \r
152      return ret;\r
153  }\r
154 -- \r
155 1.6.3.3\r
156 \r