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 2824e1841b99393d2469c495253d547c643bd8f1
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->sha1, 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->sha1);
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_sha1_file(obj->sha1))
@@ -188,15 +186,13 @@ static void finish_object(struct object *obj,
186 parse_object(obj->sha1);
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->sha1));
pack-bitmap.c
+4 -9
@@ -422,19 +422,15 @@ static int ext_index_add_object(struct object *object, const char *name)
422 return bitmap_pos + bitmap_git.pack->num_objects;
423 }
424
425 -static void show_object(struct object *object, struct strbuf *path,
426 - const char *last, void *data)
425 +static void show_object(struct object *object, const char *name, void *data)
426 {
427 struct bitmap *base = data;
428 int bitmap_pos;
429
430 bitmap_pos = bitmap_position(object->sha1);
431
433 - if (bitmap_pos < 0) {
434 - char *name = path_name(path, last);
432 + if (bitmap_pos < 0)
433 bitmap_pos = ext_index_add_object(object, name);
436 - free(name);
437 - }
434
435 bitmap_set(base, bitmap_pos);
436 }
@@ -902,9 +898,8 @@ struct bitmap_test_data {
898 size_t seen;
899 };
900
905 -static void test_show_object(struct object *object,
906 - struct strbuf *path,
907 - const char *last, void *data)
901 +static void test_show_object(struct object *object, const char *name,
902 + void *data)
903 {
904 struct bitmap_test_data *tdata = data;
905 int bitmap_pos;
reachable.c
+2 -3
@@ -36,15 +36,14 @@ static int add_one_ref(const char *path, const unsigned char *sha1, int flag, vo
36 * The traversal will have already marked us as SEEN, so we
37 * only need to handle any progress reporting here.
38 */
39 -static void mark_object(struct object *obj, struct strbuf *path,
40 - const char *name, void *data)
39 +static void mark_object(struct object *obj, const char *name, void *data)
40 {
41 update_progress(data);
42 }
43
44 static void mark_commit(struct commit *c, void *data)
45 {
47 - mark_object(&c->object, NULL, NULL, data);
46 + mark_object(&c->object, NULL, data);
47 }
48
49 struct recent_data {
revision.c
+2 -15
@@ -21,27 +21,14 @@
21
22 volatile show_early_output_fn_t show_early_output;
23
24 -char *path_name(struct strbuf *path, const char *name)
24 +void show_object_with_name(FILE *out, struct object *obj, const char *name)
25 {
26 - struct strbuf ret = STRBUF_INIT;
27 - if (path)
28 - strbuf_addbuf(&ret, path);
29 - strbuf_addstr(&ret, name);
30 - return strbuf_detach(&ret, NULL);
31 -}
32 -
33 -void show_object_with_name(FILE *out, struct object *obj,
34 - struct strbuf *path, const char *component)
35 -{
36 - char *name = path_name(path, component);
37 - char *p;
26 + const char *p;
27
28 fprintf(out, "%s ", sha1_to_hex(obj->sha1));
29 for (p = name; *p && *p != '\n'; p++)
30 fputc(*p, out);
31 fputc('\n', out);
43 -
44 - free(name);
32 }
33
34 static void mark_blob_uninteresting(struct blob *blob)
revision.h
+1 -2
@@ -258,8 +258,7 @@ extern void mark_tree_uninteresting(struct tree *tree);
258
259 char *path_name(struct strbuf *path, const char *name);
260
261 -extern void show_object_with_name(FILE *, struct object *,
262 - struct strbuf *, const char *);
261 +extern void show_object_with_name(FILE *, struct object *, const char *);
262
263 extern void add_pending_object(struct rev_info *revs,
264 struct object *obj, const char *name);