@matheusbsbs / matheus-bagliano / commits / 304de2d2d6
Fork 0

Avoid unnecessary strlen() calls

This is a micro-optimization that grew out of the mailing list discussion about "strlen()" showing up in profiles. We used to pass regular C strings around to the low-level tree walking routines, and while this worked fine, it meant that we needed to call strlen() on strings that the caller always actually knew the size of anyway. So pass the length of the string down wih the string, and avoid unnecessary calls to strlen(). Also, when extracting a pathname from a tree entry, use "tree_entry_len()" instead of strlen(), since the length of the pathname is directly calculable from the decoded tree entry itself without having to actually do another strlen(). This shaves off another ~5-10% from some loads that are very tree intensive (notably doing commit filtering by a pathspec). Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>" Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Mar 17, 2007 at 20:06 UTC 304de2d2d6afc7500fe9b8f2e22dd0a16a902d8b
3 files changed +36 -29
tree-diff.c
+29 -27
@@ -5,9 +5,8 @@
5 #include "diff.h"
6 #include "tree.h"
7
8 -static char *malloc_base(const char *base, const char *path, int pathlen)
8 +static char *malloc_base(const char *base, int baselen, const char *path, int pathlen)
9 {
10 - int baselen = strlen(base);
10 char *newbase = xmalloc(baselen + pathlen + 2);
11 memcpy(newbase, base, baselen);
12 memcpy(newbase + baselen, path, pathlen);
@@ -16,9 +15,9 @@ static char *malloc_base(const char *base, const char *path, int pathlen)
15 }
16
17 static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
19 - const char *base);
18 + const char *base, int baselen);
19
21 -static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
20 +static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, int baselen, struct diff_options *opt)
21 {
22 unsigned mode1, mode2;
23 const char *path1, *path2;
@@ -28,15 +27,15 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const
27 sha1 = tree_entry_extract(t1, &path1, &mode1);
28 sha2 = tree_entry_extract(t2, &path2, &mode2);
29
31 - pathlen1 = strlen(path1);
32 - pathlen2 = strlen(path2);
30 + pathlen1 = tree_entry_len(path1, sha1);
31 + pathlen2 = tree_entry_len(path2, sha2);
32 cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
33 if (cmp < 0) {
35 - show_entry(opt, "-", t1, base);
34 + show_entry(opt, "-", t1, base, baselen);
35 return -1;
36 }
37 if (cmp > 0) {
39 - show_entry(opt, "+", t2, base);
38 + show_entry(opt, "+", t2, base, baselen);
39 return 1;
40 }
41 if (!opt->find_copies_harder && !hashcmp(sha1, sha2) && mode1 == mode2)
@@ -47,14 +46,14 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const
46 * file, we need to consider it a remove and an add.
47 */
48 if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
50 - show_entry(opt, "-", t1, base);
51 - show_entry(opt, "+", t2, base);
49 + show_entry(opt, "-", t1, base, baselen);
50 + show_entry(opt, "+", t2, base, baselen);
51 return 0;
52 }
53
54 if (opt->recursive && S_ISDIR(mode1)) {
55 int retval;
57 - char *newbase = malloc_base(base, path1, pathlen1);
56 + char *newbase = malloc_base(base, baselen, path1, pathlen1);
57 if (opt->tree_in_recursive)
58 opt->change(opt, mode1, mode2,
59 sha1, sha2, base, path1);
@@ -67,20 +66,20 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const
66 return 0;
67 }
68
70 -static int interesting(struct tree_desc *desc, const char *base, struct diff_options *opt)
69 +static int interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt)
70 {
71 const char *path;
72 + const unsigned char *sha1;
73 unsigned mode;
74 int i;
75 - int baselen, pathlen;
75 + int pathlen;
76
77 if (!opt->nr_paths)
78 return 1;
79
80 - (void)tree_entry_extract(desc, &path, &mode);
80 + sha1 = tree_entry_extract(desc, &path, &mode);
81
82 - pathlen = strlen(path);
83 - baselen = strlen(base);
82 + pathlen = tree_entry_len(path, sha1);
83
84 for (i=0; i < opt->nr_paths; i++) {
85 const char *match = opt->paths[i];
@@ -121,18 +120,18 @@ static int interesting(struct tree_desc *desc, const char *base, struct diff_opt
120 }
121
122 /* A whole sub-tree went away or appeared */
124 -static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base)
123 +static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen)
124 {
125 while (desc->size) {
127 - if (interesting(desc, base, opt))
128 - show_entry(opt, prefix, desc, base);
126 + if (interesting(desc, base, baselen, opt))
127 + show_entry(opt, prefix, desc, base, baselen);
128 update_tree_entry(desc);
129 }
130 }
131
132 /* A file entry went away or appeared */
133 static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
135 - const char *base)
134 + const char *base, int baselen)
135 {
136 unsigned mode;
137 const char *path;
@@ -140,7 +139,8 @@ static void show_entry(struct diff_options *opt, const char *prefix, struct tree
139
140 if (opt->recursive && S_ISDIR(mode)) {
141 enum object_type type;
143 - char *newbase = malloc_base(base, path, strlen(path));
142 + int pathlen = tree_entry_len(path, sha1);
143 + char *newbase = malloc_base(base, baselen, path, pathlen);
144 struct tree_desc inner;
145 void *tree;
146
@@ -149,7 +149,7 @@ static void show_entry(struct diff_options *opt, const char *prefix, struct tree
149 die("corrupt tree sha %s", sha1_to_hex(sha1));
150
151 inner.buf = tree;
152 - show_tree(opt, prefix, &inner, newbase);
152 + show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen);
153
154 free(tree);
155 free(newbase);
@@ -160,26 +160,28 @@ static void show_entry(struct diff_options *opt, const char *prefix, struct tree
160
161 int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
162 {
163 + int baselen = strlen(base);
164 +
165 while (t1->size | t2->size) {
164 - if (opt->nr_paths && t1->size && !interesting(t1, base, opt)) {
166 + if (opt->nr_paths && t1->size && !interesting(t1, base, baselen, opt)) {
167 update_tree_entry(t1);
168 continue;
169 }
168 - if (opt->nr_paths && t2->size && !interesting(t2, base, opt)) {
170 + if (opt->nr_paths && t2->size && !interesting(t2, base, baselen, opt)) {
171 update_tree_entry(t2);
172 continue;
173 }
174 if (!t1->size) {
173 - show_entry(opt, "+", t2, base);
175 + show_entry(opt, "+", t2, base, baselen);
176 update_tree_entry(t2);
177 continue;
178 }
179 if (!t2->size) {
178 - show_entry(opt, "-", t1, base);
180 + show_entry(opt, "-", t1, base, baselen);
181 update_tree_entry(t1);
182 continue;
183 }
182 - switch (compare_tree_entry(t1, t2, base, opt)) {
184 + switch (compare_tree_entry(t1, t2, base, baselen, opt)) {
185 case -1:
186 update_tree_entry(t1);
187 continue;
tree-walk.c
+2 -2
@@ -32,7 +32,7 @@ static void entry_clear(struct name_entry *a)
32 static void entry_extract(struct tree_desc *t, struct name_entry *a)
33 {
34 a->sha1 = tree_entry_extract(t, &a->path, &a->mode);
35 - a->pathlen = strlen(a->path);
35 + a->pathlen = tree_entry_len(a->path, a->sha1);
36 }
37
38 void update_tree_entry(struct tree_desc *desc)
@@ -169,7 +169,7 @@ static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char
169
170 sha1 = tree_entry_extract(t, &entry, mode);
171 update_tree_entry(t);
172 - entrylen = strlen(entry);
172 + entrylen = tree_entry_len(entry, sha1);
173 if (entrylen > namelen)
174 continue;
175 cmp = memcmp(name, entry, entrylen);
tree-walk.h
+5
@@ -13,6 +13,11 @@ struct name_entry {
13 int pathlen;
14 };
15
16 +static inline int tree_entry_len(const char *name, const unsigned char *sha1)
17 +{
18 + return (char *)sha1 - (char *)name - 1;
19 +}
20 +
21 void update_tree_entry(struct tree_desc *);
22 const unsigned char *tree_entry_extract(struct tree_desc *, const char **, unsigned int *);
23