@matheusbsbs / matheus-bagliano / commits / 7847892716
Fork 0

unlink_entry(): introduce schedule_dir_for_removal()

Currently inside unlink_entry() if we get a successful removal of one file with unlink(), we try to remove the leading directories each and every time. So if one directory containing 200 files is moved to an other location we get 199 failed calls to rmdir() and 1 successful call. To fix this and avoid some unnecessary calls to rmdir(), we schedule each directory for removal and wait much longer before we do the real call to rmdir(). Since the unlink_entry() function is called with alphabetically sorted names, this new function end up being very effective to avoid unnecessary calls to rmdir(). In some cases over 95% of all calls to rmdir() is removed with this patch. Signed-off-by: Kjetil Barvik <barvik@broadpark.no> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kjetil Barvik committed Feb 9, 2009 at 21:54 UTC 7847892716a3c9a7b8facc076fc056ac425bcfe6
3 files changed +67 -24
cache.h
+2
@@ -729,6 +729,8 @@ extern int has_symlink_or_noent_leading_path(const char *name, int len);
729 extern int has_dirs_only_path(const char *name, int len, int prefix_len);
730 extern void invalidate_lstat_cache(const char *name, int len);
731 extern void clear_lstat_cache(void);
732 +extern void schedule_dir_for_removal(const char *name, int len);
733 +extern void remove_scheduled_dirs(void);
734
735 extern struct alternate_object_database {
736 struct alternate_object_database *next;
symlinks.c
+59
@@ -245,3 +245,62 @@ int has_dirs_only_path(const char *name, int len, int prefix_len)
245 FL_DIR|FL_FULLPATH, prefix_len) &
246 FL_DIR;
247 }
248 +
249 +static struct removal_def {
250 + char path[PATH_MAX];
251 + int len;
252 +} removal;
253 +
254 +static void do_remove_scheduled_dirs(int new_len)
255 +{
256 + while (removal.len > new_len) {
257 + removal.path[removal.len] = '\0';
258 + if (rmdir(removal.path))
259 + break;
260 + do {
261 + removal.len--;
262 + } while (removal.len > new_len &&
263 + removal.path[removal.len] != '/');
264 + }
265 + removal.len = new_len;
266 + return;
267 +}
268 +
269 +void schedule_dir_for_removal(const char *name, int len)
270 +{
271 + int match_len, last_slash, i, previous_slash;
272 +
273 + match_len = last_slash = i =
274 + longest_path_match(name, len, removal.path, removal.len,
275 + &previous_slash);
276 + /* Find last slash inside 'name' */
277 + while (i < len) {
278 + if (name[i] == '/')
279 + last_slash = i;
280 + i++;
281 + }
282 +
283 + /*
284 + * If we are about to go down the directory tree, we check if
285 + * we must first go upwards the tree, such that we then can
286 + * remove possible empty directories as we go upwards.
287 + */
288 + if (match_len < last_slash && match_len < removal.len)
289 + do_remove_scheduled_dirs(match_len);
290 + /*
291 + * If we go deeper down the directory tree, we only need to
292 + * save the new path components as we go down.
293 + */
294 + if (match_len < last_slash) {
295 + memcpy(&removal.path[match_len], &name[match_len],
296 + last_slash - match_len);
297 + removal.len = last_slash;
298 + }
299 + return;
300 +}
301 +
302 +void remove_scheduled_dirs(void)
303 +{
304 + do_remove_scheduled_dirs(0);
305 + return;
306 +}
unpack-trees.c
+6 -24
@@ -52,36 +52,17 @@ static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
52 add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|ADD_CACHE_SKIP_DFCHECK);
53 }
54
55 -/* Unlink the last component and attempt to remove leading
56 - * directories, in case this unlink is the removal of the
57 - * last entry in the directory -- empty directories are removed.
55 +/*
56 + * Unlink the last component and schedule the leading directories for
57 + * removal, such that empty directories get removed.
58 */
59 static void unlink_entry(struct cache_entry *ce)
60 {
61 - char *cp, *prev;
62 - char *name = ce->name;
63 -
61 if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
62 return;
66 - if (unlink(name))
63 + if (unlink(ce->name))
64 return;
68 - prev = NULL;
69 - while (1) {
70 - int status;
71 - cp = strrchr(name, '/');
72 - if (prev)
73 - *prev = '/';
74 - if (!cp)
75 - break;
76 -
77 - *cp = 0;
78 - status = rmdir(name);
79 - if (status) {
80 - *cp = '/';
81 - break;
82 - }
83 - prev = cp;
84 - }
65 + schedule_dir_for_removal(ce->name, ce_namelen(ce));
66 }
67
68 static struct checkout state;
@@ -117,6 +98,7 @@ static int check_updates(struct unpack_trees_options *o)
98 continue;
99 }
100 }
101 + remove_scheduled_dirs();
102
103 for (i = 0; i < index->cache_nr; i++) {
104 struct cache_entry *ce = index->cache[i];