@matheusbsbs / matheus-bagliano / commits / 4c068a9831
Fork 0

tree_entry(): new tree-walking helper function

This adds a "tree_entry()" function that combines the common operation of doing a "tree_entry_extract()" + "update_tree_entry()". It also has a simplified calling convention, designed for simple loops that traverse over a whole tree: the arguments are pointers to the tree descriptor and a name_entry structure to fill in, and it returns a boolean "true" if there was an entry left to be gotten in the tree. This allows tree traversal with struct tree_desc desc; struct name_entry entry; desc.buf = tree->buffer; desc.size = tree->size; while (tree_entry(&desc, &entry) { ... use "entry.{path, sha1, mode, pathlen}" ... } which is not only shorter than writing it out in full, it's hopefully less error prone too. [ It's actually a tad faster too - we don't need to recalculate the entry pathlength in both extract and update, but need to do it only once. Also, some callers can avoid doing a "strlen()" on the result, since it's returned as part of the name_entry structure. However, by now we're talking just 1% speedup on "git-rev-list --objects --all", and we're definitely at the point where tree walking is no longer the issue any more. ] NOTE! Not everybody wants to use this new helper function, since some of the tree walkers very much on purpose do the descriptor update separately from the entry extraction. So the "extract + update" sequence still remains as the core sequence, this is just a simplified interface. We should probably add a silly two-line inline helper function for initializing the descriptor from the "struct tree" too, just to cut down on the noise from that common "desc" initializer. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed May 30, 2006 at 09:45 UTC 4c068a983150b740c3fcf6a33f342ac923abd3f4
11 files changed +112 -141
builtin-grep.c
+10 -16
@@ -578,11 +578,9 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
578 struct tree_desc *tree,
579 const char *tree_name, const char *base)
580 {
581 - unsigned mode;
581 int len;
582 int hit = 0;
584 - const char *path;
585 - const unsigned char *sha1;
583 + struct name_entry entry;
584 char *down;
585 char *path_buf = xmalloc(PATH_MAX + strlen(tree_name) + 100);
586
@@ -597,36 +595,32 @@ static int grep_tree(struct grep_opt *opt, const char **paths,
595 }
596 len = strlen(path_buf);
597
600 - while (tree->size) {
601 - int pathlen;
602 - sha1 = tree_entry_extract(tree, &path, &mode);
603 - pathlen = strlen(path);
604 - strcpy(path_buf + len, path);
598 + while (tree_entry(tree, &entry)) {
599 + strcpy(path_buf + len, entry.path);
600
606 - if (S_ISDIR(mode))
601 + if (S_ISDIR(entry.mode))
602 /* Match "abc/" against pathspec to
603 * decide if we want to descend into "abc"
604 * directory.
605 */
611 - strcpy(path_buf + len + pathlen, "/");
606 + strcpy(path_buf + len + entry.pathlen, "/");
607
608 if (!pathspec_matches(paths, down))
609 ;
615 - else if (S_ISREG(mode))
616 - hit |= grep_sha1(opt, sha1, path_buf);
617 - else if (S_ISDIR(mode)) {
610 + else if (S_ISREG(entry.mode))
611 + hit |= grep_sha1(opt, entry.sha1, path_buf);
612 + else if (S_ISDIR(entry.mode)) {
613 char type[20];
614 struct tree_desc sub;
615 void *data;
621 - data = read_sha1_file(sha1, type, &sub.size);
616 + data = read_sha1_file(entry.sha1, type, &sub.size);
617 if (!data)
618 die("unable to read tree (%s)",
624 - sha1_to_hex(sha1));
619 + sha1_to_hex(entry.sha1));
620 sub.buf = data;
621 hit |= grep_tree(opt, paths, &sub, tree_name, down);
622 free(data);
623 }
629 - update_tree_entry(tree);
624 }
625 return hit;
626 }
builtin-read-tree.c
+13 -23
@@ -53,28 +53,23 @@ typedef int (*merge_fn_t)(struct cache_entry **src);
53 static struct tree_entry_list *create_tree_entry_list(struct tree *tree)
54 {
55 struct tree_desc desc;
56 + struct name_entry one;
57 struct tree_entry_list *ret = NULL;
58 struct tree_entry_list **list_p = &ret;
59
60 desc.buf = tree->buffer;
61 desc.size = tree->size;
62
62 - while (desc.size) {
63 - unsigned mode;
64 - const char *path;
65 - const unsigned char *sha1;
63 + while (tree_entry(&desc, &one)) {
64 struct tree_entry_list *entry;
65
68 - sha1 = tree_entry_extract(&desc, &path, &mode);
69 - update_tree_entry(&desc);
70 -
66 entry = xmalloc(sizeof(struct tree_entry_list));
72 - entry->name = path;
73 - entry->sha1 = sha1;
74 - entry->mode = mode;
75 - entry->directory = S_ISDIR(mode) != 0;
76 - entry->executable = (mode & S_IXUSR) != 0;
77 - entry->symlink = S_ISLNK(mode) != 0;
67 + entry->name = one.path;
68 + entry->sha1 = one.sha1;
69 + entry->mode = one.mode;
70 + entry->directory = S_ISDIR(one.mode) != 0;
71 + entry->executable = (one.mode & S_IXUSR) != 0;
72 + entry->symlink = S_ISLNK(one.mode) != 0;
73 entry->next = NULL;
74
75 *list_p = entry;
@@ -820,27 +815,22 @@ static int read_cache_unmerged(void)
815 static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree)
816 {
817 struct tree_desc desc;
818 + struct name_entry entry;
819 int cnt;
820
821 memcpy(it->sha1, tree->object.sha1, 20);
822 desc.buf = tree->buffer;
823 desc.size = tree->size;
824 cnt = 0;
829 - while (desc.size) {
830 - unsigned mode;
831 - const char *name;
832 - const unsigned char *sha1;
833 -
834 - sha1 = tree_entry_extract(&desc, &name, &mode);
835 - update_tree_entry(&desc);
836 - if (!S_ISDIR(mode))
825 + while (tree_entry(&desc, &entry)) {
826 + if (!S_ISDIR(entry.mode))
827 cnt++;
828 else {
829 struct cache_tree_sub *sub;
840 - struct tree *subtree = lookup_tree(sha1);
830 + struct tree *subtree = lookup_tree(entry.sha1);
831 if (!subtree->object.parsed)
832 parse_tree(subtree);
843 - sub = cache_tree_sub(it, name);
833 + sub = cache_tree_sub(it, entry.path);
834 sub->cache_tree = cache_tree();
835 prime_cache_tree_rec(sub->cache_tree, subtree);
836 cnt += sub->cache_tree->entry_count;
builtin-rev-list.c
+5 -11
@@ -114,6 +114,7 @@ static struct object_list **process_tree(struct tree *tree,
114 {
115 struct object *obj = &tree->object;
116 struct tree_desc desc;
117 + struct name_entry entry;
118 struct name_path me;
119
120 if (!revs.tree_objects)
@@ -132,18 +133,11 @@ static struct object_list **process_tree(struct tree *tree,
133 desc.buf = tree->buffer;
134 desc.size = tree->size;
135
135 - while (desc.size) {
136 - unsigned mode;
137 - const char *name;
138 - const unsigned char *sha1;
139 -
140 - sha1 = tree_entry_extract(&desc, &name, &mode);
141 - update_tree_entry(&desc);
142 -
143 - if (S_ISDIR(mode))
144 - p = process_tree(lookup_tree(sha1), p, &me, name);
136 + while (tree_entry(&desc, &entry)) {
137 + if (S_ISDIR(entry.mode))
138 + p = process_tree(lookup_tree(entry.sha1), p, &me, name);
139 else
146 - p = process_blob(lookup_blob(sha1), p, &me, name);
140 + p = process_blob(lookup_blob(entry.sha1), p, &me, name);
141 }
142 free(tree->buffer);
143 tree->buffer = NULL;
builtin-tar-tree.c
+8 -13
@@ -271,30 +271,25 @@ static void write_global_extended_header(const unsigned char *sha1)
271 static void traverse_tree(struct tree_desc *tree, struct strbuf *path)
272 {
273 int pathlen = path->len;
274 + struct name_entry entry;
275
275 - while (tree->size) {
276 - const char *name;
277 - const unsigned char *sha1;
278 - unsigned mode;
276 + while (tree_entry(tree, &entry)) {
277 void *eltbuf;
278 char elttype[20];
279 unsigned long eltsize;
280
283 - sha1 = tree_entry_extract(tree, &name, &mode);
284 - update_tree_entry(tree);
285 -
286 - eltbuf = read_sha1_file(sha1, elttype, &eltsize);
281 + eltbuf = read_sha1_file(entry.sha1, elttype, &eltsize);
282 if (!eltbuf)
288 - die("cannot read %s", sha1_to_hex(sha1));
283 + die("cannot read %s", sha1_to_hex(entry.sha1));
284
285 path->len = pathlen;
291 - strbuf_append_string(path, name);
292 - if (S_ISDIR(mode))
286 + strbuf_append_string(path, entry.path);
287 + if (S_ISDIR(entry.mode))
288 strbuf_append_string(path, "/");
289
295 - write_entry(sha1, path, mode, eltbuf, eltsize);
290 + write_entry(entry.sha1, path, entry.mode, eltbuf, eltsize);
291
297 - if (S_ISDIR(mode)) {
292 + if (S_ISDIR(entry.mode)) {
293 struct tree_desc subtree;
294 subtree.buf = eltbuf;
295 subtree.size = eltsize;
fetch.c
+5 -11
@@ -38,25 +38,19 @@ static int process(struct object *obj);
38 static int process_tree(struct tree *tree)
39 {
40 struct tree_desc desc;
41 + struct name_entry entry;
42
43 if (parse_tree(tree))
44 return -1;
45
46 desc.buf = tree->buffer;
47 desc.size = tree->size;
47 - while (desc.size) {
48 - unsigned mode;
49 - const char *name;
50 - const unsigned char *sha1;
51 -
52 - sha1 = tree_entry_extract(&desc, &name, &mode);
53 - update_tree_entry(&desc);
54 -
55 - if (S_ISDIR(mode)) {
56 - struct tree *tree = lookup_tree(sha1);
48 + while (tree_entry(&desc, &entry)) {
49 + if (S_ISDIR(entry.mode)) {
50 + struct tree *tree = lookup_tree(entry.sha1);
51 process_tree(tree);
52 } else {
59 - struct blob *blob = lookup_blob(sha1);
53 + struct blob *blob = lookup_blob(entry.sha1);
54 process(&blob->object);
55 }
56 }
http-push.c
+5 -11
@@ -1715,6 +1715,7 @@ static struct object_list **process_tree(struct tree *tree,
1715 {
1716 struct object *obj = &tree->object;
1717 struct tree_desc desc;
1718 + struct name_entry entry;
1719 struct name_path me;
1720
1721 obj->flags |= LOCAL;
@@ -1734,18 +1735,11 @@ static struct object_list **process_tree(struct tree *tree,
1735 desc.buf = tree->buffer;
1736 desc.size = tree->size;
1737
1737 - while (desc.size) {
1738 - unsigned mode;
1739 - const char *name;
1740 - const unsigned char *sha1;
1741 -
1742 - sha1 = tree_entry_extract(&desc, &name, &mode);
1743 - update_tree_entry(&desc);
1744 -
1745 - if (S_ISDIR(mode))
1746 - p = process_tree(lookup_tree(sha1), p, &me, name);
1738 + while (tree_entry(&desc, &entry)) {
1739 + if (S_ISDIR(entry.mode))
1740 + p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1741 else
1748 - p = process_blob(lookup_blob(sha1), p, &me, name);
1742 + p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1743 }
1744 free(tree->buffer);
1745 tree->buffer = NULL;
pack-objects.c
+11 -16
@@ -690,25 +690,20 @@ static void add_pbase_object(struct tree_desc *tree,
690 const char *name,
691 int cmplen)
692 {
693 - while (tree->size) {
694 - const unsigned char *sha1;
695 - const char *entry_name;
696 - int entry_len;
697 - unsigned mode;
693 + struct name_entry entry;
694 +
695 + while (tree_entry(tree,&entry)) {
696 unsigned long size;
697 char type[20];
698
701 - sha1 = tree_entry_extract(tree, &entry_name, &mode);
702 - update_tree_entry(tree);
703 - entry_len = strlen(entry_name);
704 - if (entry_len != cmplen ||
705 - memcmp(entry_name, name, cmplen) ||
706 - !has_sha1_file(sha1) ||
707 - sha1_object_info(sha1, type, &size))
699 + if (entry.pathlen != cmplen ||
700 + memcmp(entry.path, name, cmplen) ||
701 + !has_sha1_file(entry.sha1) ||
702 + sha1_object_info(entry.sha1, type, &size))
703 continue;
704 if (name[cmplen] != '/') {
705 unsigned hash = name_hash(up, name);
711 - add_object_entry(sha1, hash, 1);
706 + add_object_entry(entry.sha1, hash, 1);
707 return;
708 }
709 if (!strcmp(type, tree_type)) {
@@ -718,15 +713,15 @@ static void add_pbase_object(struct tree_desc *tree,
713 const char *down = name+cmplen+1;
714 int downlen = name_cmp_len(down);
715
721 - tree = pbase_tree_get(sha1);
716 + tree = pbase_tree_get(entry.sha1);
717 if (!tree)
718 return;
719 sub.buf = tree->tree_data;
720 sub.size = tree->tree_size;
721
722 me.up = up;
728 - me.elem = entry_name;
729 - me.len = entry_len;
723 + me.elem = entry.path;
724 + me.len = entry.pathlen;
725 add_pbase_object(&sub, &me, down, downlen);
726 pbase_tree_put(tree);
727 }
revision.c
+5 -11
@@ -54,6 +54,7 @@ static void mark_blob_uninteresting(struct blob *blob)
54 void mark_tree_uninteresting(struct tree *tree)
55 {
56 struct tree_desc desc;
57 + struct name_entry entry;
58 struct object *obj = &tree->object;
59
60 if (obj->flags & UNINTERESTING)
@@ -66,18 +67,11 @@ void mark_tree_uninteresting(struct tree *tree)
67
68 desc.buf = tree->buffer;
69 desc.size = tree->size;
69 - while (desc.size) {
70 - unsigned mode;
71 - const char *name;
72 - const unsigned char *sha1;
73 -
74 - sha1 = tree_entry_extract(&desc, &name, &mode);
75 - update_tree_entry(&desc);
76 -
77 - if (S_ISDIR(mode))
78 - mark_tree_uninteresting(lookup_tree(sha1));
70 + while (tree_entry(&desc, &entry)) {
71 + if (S_ISDIR(entry.mode))
72 + mark_tree_uninteresting(lookup_tree(entry.sha1));
73 else
80 - mark_blob_uninteresting(lookup_blob(sha1));
74 + mark_blob_uninteresting(lookup_blob(entry.sha1));
75 }
76
77 /*
tree-walk.c
+31 -2
@@ -37,7 +37,7 @@ static void entry_extract(struct tree_desc *t, struct name_entry *a)
37
38 void update_tree_entry(struct tree_desc *desc)
39 {
40 - void *buf = desc->buf;
40 + const void *buf = desc->buf;
41 unsigned long size = desc->size;
42 int len = strlen(buf) + 1 + 20;
43
@@ -63,7 +63,7 @@ static const char *get_mode(const char *str, unsigned int *modep)
63
64 const unsigned char *tree_entry_extract(struct tree_desc *desc, const char **pathp, unsigned int *modep)
65 {
66 - void *tree = desc->buf;
66 + const void *tree = desc->buf;
67 unsigned long size = desc->size;
68 int len = strlen(tree)+1;
69 const unsigned char *sha1 = tree + len;
@@ -78,6 +78,35 @@ const unsigned char *tree_entry_extract(struct tree_desc *desc, const char **pat
78 return sha1;
79 }
80
81 +int tree_entry(struct tree_desc *desc, struct name_entry *entry)
82 +{
83 + const void *tree = desc->buf, *path;
84 + unsigned long len, size = desc->size;
85 +
86 + if (!size)
87 + return 0;
88 +
89 + path = get_mode(tree, &entry->mode);
90 + if (!path)
91 + die("corrupt tree file");
92 +
93 + entry->path = path;
94 + len = strlen(path);
95 + entry->pathlen = len;
96 +
97 + path += len + 1;
98 + entry->sha1 = path;
99 +
100 + path += 20;
101 + len = path - tree;
102 + if (len > size)
103 + die("corrupt tree file");
104 +
105 + desc->buf = path;
106 + desc->size = size - len;
107 + return 1;
108 +}
109 +
110 void traverse_trees(int n, struct tree_desc *t, const char *base, traverse_callback_t callback)
111 {
112 struct name_entry *entry = xmalloc(n*sizeof(*entry));
tree-walk.h
+4 -1
@@ -2,7 +2,7 @@
2 #define TREE_WALK_H
3
4 struct tree_desc {
5 - void *buf;
5 + const void *buf;
6 unsigned long size;
7 };
8
@@ -16,6 +16,9 @@ struct name_entry {
16 void update_tree_entry(struct tree_desc *);
17 const unsigned char *tree_entry_extract(struct tree_desc *, const char **, unsigned int *);
18
19 +/* Helper function that does both of the above and returns true for success */
20 +int tree_entry(struct tree_desc *, struct name_entry *);
21 +
22 void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1);
23
24 typedef void (*traverse_callback_t)(int n, unsigned long mask, struct name_entry *entry, const char *base);
tree.c
+15 -26
@@ -79,6 +79,7 @@ int read_tree_recursive(struct tree *tree,
79 read_tree_fn_t fn)
80 {
81 struct tree_desc desc;
82 + struct name_entry entry;
83
84 if (parse_tree(tree))
85 return -1;
@@ -86,18 +87,11 @@ int read_tree_recursive(struct tree *tree,
87 desc.buf = tree->buffer;
88 desc.size = tree->size;
89
89 - while (desc.size) {
90 - unsigned mode;
91 - const char *name;
92 - const unsigned char *sha1;
93 -
94 - sha1 = tree_entry_extract(&desc, &name, &mode);
95 - update_tree_entry(&desc);
96 -
97 - if (!match_tree_entry(base, baselen, name, mode, match))
90 + while (tree_entry(&desc, &entry)) {
91 + if (!match_tree_entry(base, baselen, entry.path, entry.mode, match))
92 continue;
93
100 - switch (fn(sha1, base, baselen, name, mode, stage)) {
94 + switch (fn(entry.sha1, base, baselen, entry.path, entry.mode, stage)) {
95 case 0:
96 continue;
97 case READ_TREE_RECURSIVE:
@@ -105,18 +99,17 @@ int read_tree_recursive(struct tree *tree,
99 default:
100 return -1;
101 }
108 - if (S_ISDIR(mode)) {
102 + if (S_ISDIR(entry.mode)) {
103 int retval;
110 - int pathlen = strlen(name);
104 char *newbase;
105
113 - newbase = xmalloc(baselen + 1 + pathlen);
106 + newbase = xmalloc(baselen + 1 + entry.pathlen);
107 memcpy(newbase, base, baselen);
115 - memcpy(newbase + baselen, name, pathlen);
116 - newbase[baselen + pathlen] = '/';
117 - retval = read_tree_recursive(lookup_tree(sha1),
108 + memcpy(newbase + baselen, entry.path, entry.pathlen);
109 + newbase[baselen + entry.pathlen] = '/';
110 + retval = read_tree_recursive(lookup_tree(entry.sha1),
111 newbase,
119 - baselen + pathlen + 1,
112 + baselen + entry.pathlen + 1,
113 stage, match, fn);
114 free(newbase);
115 if (retval)
@@ -156,6 +149,7 @@ static int track_tree_refs(struct tree *item)
149 int n_refs = 0, i;
150 struct object_refs *refs;
151 struct tree_desc desc;
152 + struct name_entry entry;
153
154 /* Count how many entries there are.. */
155 desc.buf = item->buffer;
@@ -170,18 +164,13 @@ static int track_tree_refs(struct tree *item)
164 refs = alloc_object_refs(n_refs);
165 desc.buf = item->buffer;
166 desc.size = item->size;
173 - while (desc.size) {
174 - unsigned mode;
175 - const char *name;
176 - const unsigned char *sha1;
167 + while (tree_entry(&desc, &entry)) {
168 struct object *obj;
169
179 - sha1 = tree_entry_extract(&desc, &name, &mode);
180 - update_tree_entry(&desc);
181 - if (S_ISDIR(mode))
182 - obj = &lookup_tree(sha1)->object;
170 + if (S_ISDIR(entry.mode))
171 + obj = &lookup_tree(entry.sha1)->object;
172 else
184 - obj = &lookup_blob(sha1)->object;
173 + obj = &lookup_blob(entry.sha1)->object;
174 refs->ref[i++] = obj;
175 }
176 set_object_refs(&item->object, refs);