list-objects: pass full pathname to callbacks

When we find a blob at "a/b/c", we currently pass this to our show_object_fn callbacks as two components: "a/b/" and "c". Callbacks which want the full value then call path_name(), which concatenates the two. But this is an inefficient interface; the path is a strbuf, and we could simply append "c" to it temporarily, then roll back the length, without creating a new copy. So we could improve this by teaching the callsites of path_name() this trick (and there are only 3). But we can also notice that no callback actually cares about the broken-down representation, and simply pass each callback the full path "a/b/c" as a string. The callback code becomes even simpler, then, as we do not have to worry about freeing an allocated buffer, nor rolling back our modification to the strbuf. This is theoretically less efficient, as some callbacks would not bother to format the final path component. But in practice this is not measurable. Since we use the same strbuf over and over, our work to grow it is amortized, and we really only pay to memcpy a few bytes. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 11, 2016 at 17:28 UTC de1e67d0703894cb6ea782e36abb63976ab07e60
9 files changed +26 -58
builtin/pack-objects.c
+2 -13
@@ -2284,21 +2284,11 @@ static void show_commit(struct commit *commit, void *data)
2284 index_commit_for_bitmap(commit);
2285 }
2286
2287 -static void show_object(struct object *obj,
2288 - struct strbuf *path, const char *last,
2289 - void *data)
2287 +static void show_object(struct object *obj, const char *name, void *data)
2288 {
2291 - char *name = path_name(path, last);
2292 -
2289 add_preferred_base_object(name);
2290 add_object_entry(obj->oid.hash, obj->type, name, 0);
2291 obj->flags |= OBJECT_ADDED;
2296 -
2297 - /*
2298 - * We will have generated the hash from the name,
2299 - * but not saved a pointer to it - we can free it
2300 - */
2301 - free((char *)name);
2292 }
2293
2294 static void show_edge(struct commit *commit)
@@ -2480,8 +2470,7 @@ static int get_object_list_from_bitmap(struct rev_info *revs)
2470 }
2471
2472 static void record_recent_object(struct object *obj,
2483 - struct strbuf *path,
2484 - const char *last,
2473 + const char *name,
2474 void *data)
2475 {
2476 sha1_array_append(&recent_objects, obj->oid.hash);
builtin/rev-list.c
+4 -8
@@ -177,9 +177,7 @@ static void finish_commit(struct commit *commit, void *data)
177 free_commit_buffer(commit);
178 }
179
180 -static void finish_object(struct object *obj,
181 - struct strbuf *path, const char *name,
182 - void *cb_data)
180 +static void finish_object(struct object *obj, const char *name, void *cb_data)
181 {
182 struct rev_list_info *info = cb_data;
183 if (obj->type == OBJ_BLOB && !has_object_file(&obj->oid))
@@ -188,15 +186,13 @@ static void finish_object(struct object *obj,
186 parse_object(obj->oid.hash);
187 }
188
191 -static void show_object(struct object *obj,
192 - struct strbuf *path, const char *component,
193 - void *cb_data)
189 +static void show_object(struct object *obj, const char *name, void *cb_data)
190 {
191 struct rev_list_info *info = cb_data;
196 - finish_object(obj, path, component, cb_data);
192 + finish_object(obj, name, cb_data);
193 if (info->flags & REV_LIST_QUIET)
194 return;
199 - show_object_with_name(stdout, obj, path, component);
195 + show_object_with_name(stdout, obj, name);
196 }
197
198 static void show_edge(struct commit *commit)
list-objects.c
+9 -5
@@ -16,6 +16,7 @@ static void process_blob(struct rev_info *revs,
16 void *cb_data)
17 {
18 struct object *obj = &blob->object;
19 + size_t pathlen;
20
21 if (!revs->blob_objects)
22 return;
@@ -24,7 +25,11 @@ static void process_blob(struct rev_info *revs,
25 if (obj->flags & (UNINTERESTING | SEEN))
26 return;
27 obj->flags |= SEEN;
27 - show(obj, path, name, cb_data);
28 +
29 + pathlen = path->len;
30 + strbuf_addstr(path, name);
31 + show(obj, path->buf, cb_data);
32 + strbuf_setlen(path, pathlen);
33 }
34
35 /*
@@ -86,9 +91,8 @@ static void process_tree(struct rev_info *revs,
91 }
92
93 obj->flags |= SEEN;
89 - show(obj, base, name, cb_data);
90 -
94 strbuf_addstr(base, name);
95 + show(obj, base->buf, cb_data);
96 if (base->len)
97 strbuf_addch(base, '/');
98
@@ -207,7 +211,7 @@ void traverse_commit_list(struct rev_info *revs,
211 continue;
212 if (obj->type == OBJ_TAG) {
213 obj->flags |= SEEN;
210 - show_object(obj, NULL, name, data);
214 + show_object(obj, name, data);
215 continue;
216 }
217 if (!path)
@@ -219,7 +223,7 @@ void traverse_commit_list(struct rev_info *revs,
223 }
224 if (obj->type == OBJ_BLOB) {
225 process_blob(revs, (struct blob *)obj, show_object,
222 - NULL, path, data);
226 + &base, path, data);
227 continue;
228 }
229 die("unknown pending object %s (%s)",
list-objects.h
+1 -1
@@ -2,7 +2,7 @@
2 #define LIST_OBJECTS_H
3
4 typedef void (*show_commit_fn)(struct commit *, void *);
5 -typedef void (*show_object_fn)(struct object *, struct strbuf *, const char *, void *);
5 +typedef void (*show_object_fn)(struct object *, const char *, void *);
6 void traverse_commit_list(struct rev_info *, show_commit_fn, show_object_fn, void *);
7
8 typedef void (*show_edge_fn)(struct commit *);
pack-bitmap-write.c
+1 -2
@@ -148,8 +148,7 @@ static uint32_t find_object_pos(const unsigned char *sha1)
148 return entry->in_pack_pos;
149 }
150
151 -static void show_object(struct object *object, struct strbuf *path,
152 - const char *last, void *data)
151 +static void show_object(struct object *object, const char *name, void *data)
152 {
153 struct bitmap *base = data;
154 bitmap_set(base, find_object_pos(object->oid.hash));
pack-bitmap.c
+4 -9
@@ -414,19 +414,15 @@ static int ext_index_add_object(struct object *object, const char *name)
414 return bitmap_pos + bitmap_git.pack->num_objects;
415 }
416
417 -static void show_object(struct object *object, struct strbuf *path,
418 - const char *last, void *data)
417 +static void show_object(struct object *object, const char *name, void *data)
418 {
419 struct bitmap *base = data;
420 int bitmap_pos;
421
422 bitmap_pos = bitmap_position(object->oid.hash);
423
425 - if (bitmap_pos < 0) {
426 - char *name = path_name(path, last);
424 + if (bitmap_pos < 0)
425 bitmap_pos = ext_index_add_object(object, name);
428 - free(name);
429 - }
426
427 bitmap_set(base, bitmap_pos);
428 }
@@ -894,9 +890,8 @@ struct bitmap_test_data {
890 size_t seen;
891 };
892
897 -static void test_show_object(struct object *object,
898 - struct strbuf *path,
899 - const char *last, void *data)
893 +static void test_show_object(struct object *object, const char *name,
894 + void *data)
895 {
896 struct bitmap_test_data *tdata = data;
897 int bitmap_pos;
reachable.c
+2 -3
@@ -43,15 +43,14 @@ static int add_one_ref(const char *path, const struct object_id *oid,
43 * The traversal will have already marked us as SEEN, so we
44 * only need to handle any progress reporting here.
45 */
46 -static void mark_object(struct object *obj, struct strbuf *path,
47 - const char *name, void *data)
46 +static void mark_object(struct object *obj, const char *name, void *data)
47 {
48 update_progress(data);
49 }
50
51 static void mark_commit(struct commit *c, void *data)
52 {
54 - mark_object(&c->object, NULL, NULL, data);
53 + mark_object(&c->object, NULL, data);
54 }
55
56 struct recent_data {
revision.c
+2 -15
@@ -25,27 +25,14 @@ volatile show_early_output_fn_t show_early_output;
25 static const char *term_bad;
26 static const char *term_good;
27
28 -char *path_name(struct strbuf *path, const char *name)
28 +void show_object_with_name(FILE *out, struct object *obj, const char *name)
29 {
30 - struct strbuf ret = STRBUF_INIT;
31 - if (path)
32 - strbuf_addbuf(&ret, path);
33 - strbuf_addstr(&ret, name);
34 - return strbuf_detach(&ret, NULL);
35 -}
36 -
37 -void show_object_with_name(FILE *out, struct object *obj,
38 - struct strbuf *path, const char *component)
39 -{
40 - char *name = path_name(path, component);
41 - char *p;
30 + const char *p;
31
32 fprintf(out, "%s ", oid_to_hex(&obj->oid));
33 for (p = name; *p && *p != '\n'; p++)
34 fputc(*p, out);
35 fputc('\n', out);
47 -
48 - free(name);
36 }
37
38 static void mark_blob_uninteresting(struct blob *blob)
revision.h
+1 -2
@@ -259,8 +259,7 @@ extern void mark_tree_uninteresting(struct tree *tree);
259
260 char *path_name(struct strbuf *path, const char *name);
261
262 -extern void show_object_with_name(FILE *, struct object *,
263 - struct strbuf *, const char *);
262 +extern void show_object_with_name(FILE *, struct object *, const char *);
263
264 extern void add_pending_object(struct rev_info *revs,
265 struct object *obj, const char *name);