Skip to content

Commit 7847892

Browse files
Kjetil Barvikgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5719989 commit 7847892

File tree

3 files changed

+67
-24
lines changed

3 files changed

+67
-24
lines changed

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,8 @@ extern int has_symlink_or_noent_leading_path(const char *name, int len);
729729
extern int has_dirs_only_path(const char *name, int len, int prefix_len);
730730
extern void invalidate_lstat_cache(const char *name, int len);
731731
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);
732734

733735
extern struct alternate_object_database {
734736
struct alternate_object_database *next;

symlinks.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,62 @@ int has_dirs_only_path(const char *name, int len, int prefix_len)
245245
FL_DIR|FL_FULLPATH, prefix_len) &
246246
FL_DIR;
247247
}
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

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,36 +52,17 @@ static void add_entry(struct unpack_trees_options *o, struct cache_entry *ce,
5252
add_index_entry(&o->result, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE|ADD_CACHE_SKIP_DFCHECK);
5353
}
5454

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.
5858
*/
5959
static void unlink_entry(struct cache_entry *ce)
6060
{
61-
char *cp, *prev;
62-
char *name = ce->name;
63-
6461
if (has_symlink_or_noent_leading_path(ce->name, ce_namelen(ce)))
6562
return;
66-
if (unlink(name))
63+
if (unlink(ce->name))
6764
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));
8566
}
8667

8768
static struct checkout state;
@@ -117,6 +98,7 @@ static int check_updates(struct unpack_trees_options *o)
11798
continue;
11899
}
119100
}
101+
remove_scheduled_dirs();
120102

121103
for (i = 0; i < index->cache_nr; i++) {
122104
struct cache_entry *ce = index->cache[i];

0 commit comments

Comments
 (0)