@matheusbsbs / matheus-bagliano / commits / 61fa30972c
Fork 0

cache-tree: sort the subtree entries.

Not that this makes practical performance difference; the kernel tree for example has 200 or so directories that have subdirectory, and the largest ones have 57 of them (fs and drivers). With a test to apply 600 patches with git-apply and git-write-tree, this did not make more than one per-cent of a difference, but it is a good cleanup. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 25, 2006 at 17:40 UTC 61fa30972c45e73d39c81d27af9eeacbda7531c9
1 file changed +66 -26
cache-tree.c
+66 -26
@@ -19,38 +19,75 @@ void cache_tree_free(struct cache_tree **it_p)
19 if (!it)
20 return;
21 for (i = 0; i < it->subtree_nr; i++)
22 - cache_tree_free(&it->down[i]->cache_tree);
22 + if (it->down[i])
23 + cache_tree_free(&it->down[i]->cache_tree);
24 free(it->down);
25 free(it);
26 *it_p = NULL;
27 }
28
29 +static int subtree_name_cmp(const char *one, int onelen,
30 + const char *two, int twolen)
31 +{
32 + if (onelen < twolen)
33 + return -1;
34 + if (twolen < onelen)
35 + return 1;
36 + return memcmp(one, two, onelen);
37 +}
38 +
39 +static int subtree_pos(struct cache_tree *it, const char *path, int pathlen)
40 +{
41 + struct cache_tree_sub **down = it->down;
42 + int lo, hi;
43 + lo = 0;
44 + hi = it->subtree_nr;
45 + while (lo < hi) {
46 + int mi = (lo + hi) / 2;
47 + struct cache_tree_sub *mdl = down[mi];
48 + int cmp = subtree_name_cmp(path, pathlen,
49 + mdl->name, mdl->namelen);
50 + if (!cmp)
51 + return mi;
52 + if (cmp < 0)
53 + hi = mi;
54 + else
55 + lo = mi + 1;
56 + }
57 + return -lo-1;
58 +}
59 +
60 static struct cache_tree_sub *find_subtree(struct cache_tree *it,
61 const char *path,
62 int pathlen,
63 int create)
64 {
33 - int i;
65 struct cache_tree_sub *down;
35 - for (i = 0; i < it->subtree_nr; i++) {
36 - down = it->down[i];
37 - if (down->namelen == pathlen &&
38 - !memcmp(down->name, path, pathlen))
39 - return down;
40 - }
66 + int pos = subtree_pos(it, path, pathlen);
67 + if (0 <= pos)
68 + return it->down[pos];
69 if (!create)
70 return NULL;
71 +
72 + pos = -pos-1;
73 if (it->subtree_alloc <= it->subtree_nr) {
74 it->subtree_alloc = alloc_nr(it->subtree_alloc);
75 it->down = xrealloc(it->down, it->subtree_alloc *
76 sizeof(*it->down));
77 }
78 + it->subtree_nr++;
79 +
80 down = xmalloc(sizeof(*down) + pathlen + 1);
49 - down->cache_tree = NULL; /* cache_tree(); */
81 + down->cache_tree = NULL;
82 down->namelen = pathlen;
83 memcpy(down->name, path, pathlen);
52 - down->name[pathlen] = 0; /* not strictly needed */
53 - it->down[it->subtree_nr++] = down;
84 + down->name[pathlen] = 0;
85 +
86 + if (pos < it->subtree_nr)
87 + memmove(it->down + pos + 1,
88 + it->down + pos,
89 + sizeof(down) * (it->subtree_nr - pos - 1));
90 + it->down[pos] = down;
91 return down;
92 }
93
@@ -72,25 +109,21 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)
109 slash = strchr(path, '/');
110 it->entry_count = -1;
111 if (!slash) {
75 - int i;
112 + int pos;
113 namelen = strlen(path);
77 - for (i = 0; i < it->subtree_nr; i++) {
78 - if (it->down[i]->namelen == namelen &&
79 - !memcmp(it->down[i]->name, path, namelen))
80 - break;
81 - }
82 - if (i < it->subtree_nr) {
83 - cache_tree_free(&it->down[i]->cache_tree);
84 - free(it->down[i]);
114 + pos = subtree_pos(it, path, namelen);
115 + if (0 <= pos) {
116 + cache_tree_free(&it->down[pos]->cache_tree);
117 + free(it->down[pos]);
118 /* 0 1 2 3 4 5
119 * ^ ^subtree_nr = 6
87 - * i
120 + * pos
121 * move 4 and 5 up one place (2 entries)
89 - * 2 = 6 - 3 - 1 = subtree_nr - i - 1
122 + * 2 = 6 - 3 - 1 = subtree_nr - pos - 1
123 */
91 - memmove(it->down+i, it->down+i+1,
124 + memmove(it->down+pos, it->down+pos+1,
125 sizeof(struct cache_tree_sub *) *
93 - (it->subtree_nr - i - 1));
126 + (it->subtree_nr - pos - 1));
127 it->subtree_nr--;
128 }
129 return;
@@ -364,6 +397,12 @@ static void *write_one(struct cache_tree *it,
397 }
398 for (i = 0; i < it->subtree_nr; i++) {
399 struct cache_tree_sub *down = it->down[i];
400 + if (i) {
401 + struct cache_tree_sub *prev = it->down[i-1];
402 + if (subtree_name_cmp(down->name, down->namelen,
403 + prev->name, prev->namelen) <= 0)
404 + die("fatal - unsorted cache subtree");
405 + }
406 buffer = write_one(down->cache_tree, down->name, down->namelen,
407 buffer, size, offset);
408 }
@@ -435,14 +474,15 @@ static struct cache_tree *read_one(const char **buffer, unsigned long *size_p)
474 for (i = 0; i < subtree_nr; i++) {
475 /* read each subtree */
476 struct cache_tree *sub;
477 + struct cache_tree_sub *subtree;
478 const char *name = buf;
479 int namelen;
480 sub = read_one(&buf, &size);
481 if (!sub)
482 goto free_return;
483 namelen = strlen(name);
444 - it->down[i] = find_subtree(it, name, namelen, 1);
445 - it->down[i]->cache_tree = sub;
484 + subtree = find_subtree(it, name, namelen, 1);
485 + subtree->cache_tree = sub;
486 }
487 if (subtree_nr != it->subtree_nr)
488 die("cache-tree: internal error");