Skip to content

Commit 2824e18

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dc06dc8 commit 2824e18

9 files changed

+26
-58
lines changed

builtin/pack-objects.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,21 +2284,11 @@ static void show_commit(struct commit *commit, void *data)
22842284
index_commit_for_bitmap(commit);
22852285
}
22862286

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)
22902288
{
2291-
char *name = path_name(path, last);
2292-
22932289
add_preferred_base_object(name);
22942290
add_object_entry(obj->sha1, obj->type, name, 0);
22952291
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);
23022292
}
23032293

23042294
static void show_edge(struct commit *commit)
@@ -2480,8 +2470,7 @@ static int get_object_list_from_bitmap(struct rev_info *revs)
24802470
}
24812471

24822472
static void record_recent_object(struct object *obj,
2483-
struct strbuf *path,
2484-
const char *last,
2473+
const char *name,
24852474
void *data)
24862475
{
24872476
sha1_array_append(&recent_objects, obj->sha1);

builtin/rev-list.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ static void finish_commit(struct commit *commit, void *data)
177177
free_commit_buffer(commit);
178178
}
179179

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)
183181
{
184182
struct rev_list_info *info = cb_data;
185183
if (obj->type == OBJ_BLOB && !has_sha1_file(obj->sha1))
@@ -188,15 +186,13 @@ static void finish_object(struct object *obj,
188186
parse_object(obj->sha1);
189187
}
190188

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)
194190
{
195191
struct rev_list_info *info = cb_data;
196-
finish_object(obj, path, component, cb_data);
192+
finish_object(obj, name, cb_data);
197193
if (info->flags & REV_LIST_QUIET)
198194
return;
199-
show_object_with_name(stdout, obj, path, component);
195+
show_object_with_name(stdout, obj, name);
200196
}
201197

202198
static void show_edge(struct commit *commit)

list-objects.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static void process_blob(struct rev_info *revs,
1616
void *cb_data)
1717
{
1818
struct object *obj = &blob->object;
19+
size_t pathlen;
1920

2021
if (!revs->blob_objects)
2122
return;
@@ -24,7 +25,11 @@ static void process_blob(struct rev_info *revs,
2425
if (obj->flags & (UNINTERESTING | SEEN))
2526
return;
2627
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);
2833
}
2934

3035
/*
@@ -86,9 +91,8 @@ static void process_tree(struct rev_info *revs,
8691
}
8792

8893
obj->flags |= SEEN;
89-
show(obj, base, name, cb_data);
90-
9194
strbuf_addstr(base, name);
95+
show(obj, base->buf, cb_data);
9296
if (base->len)
9397
strbuf_addch(base, '/');
9498

@@ -207,7 +211,7 @@ void traverse_commit_list(struct rev_info *revs,
207211
continue;
208212
if (obj->type == OBJ_TAG) {
209213
obj->flags |= SEEN;
210-
show_object(obj, NULL, name, data);
214+
show_object(obj, name, data);
211215
continue;
212216
}
213217
if (!path)
@@ -219,7 +223,7 @@ void traverse_commit_list(struct rev_info *revs,
219223
}
220224
if (obj->type == OBJ_BLOB) {
221225
process_blob(revs, (struct blob *)obj, show_object,
222-
NULL, path, data);
226+
&base, path, data);
223227
continue;
224228
}
225229
die("unknown pending object %s (%s)",

list-objects.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define LIST_OBJECTS_H
33

44
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 *);
66
void traverse_commit_list(struct rev_info *, show_commit_fn, show_object_fn, void *);
77

88
typedef void (*show_edge_fn)(struct commit *);

pack-bitmap-write.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ static uint32_t find_object_pos(const unsigned char *sha1)
148148
return entry->in_pack_pos;
149149
}
150150

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)
153152
{
154153
struct bitmap *base = data;
155154
bitmap_set(base, find_object_pos(object->sha1));

pack-bitmap.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -422,19 +422,15 @@ static int ext_index_add_object(struct object *object, const char *name)
422422
return bitmap_pos + bitmap_git.pack->num_objects;
423423
}
424424

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)
427426
{
428427
struct bitmap *base = data;
429428
int bitmap_pos;
430429

431430
bitmap_pos = bitmap_position(object->sha1);
432431

433-
if (bitmap_pos < 0) {
434-
char *name = path_name(path, last);
432+
if (bitmap_pos < 0)
435433
bitmap_pos = ext_index_add_object(object, name);
436-
free(name);
437-
}
438434

439435
bitmap_set(base, bitmap_pos);
440436
}
@@ -902,9 +898,8 @@ struct bitmap_test_data {
902898
size_t seen;
903899
};
904900

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)
908903
{
909904
struct bitmap_test_data *tdata = data;
910905
int bitmap_pos;

reachable.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,14 @@ static int add_one_ref(const char *path, const unsigned char *sha1, int flag, vo
3636
* The traversal will have already marked us as SEEN, so we
3737
* only need to handle any progress reporting here.
3838
*/
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)
4140
{
4241
update_progress(data);
4342
}
4443

4544
static void mark_commit(struct commit *c, void *data)
4645
{
47-
mark_object(&c->object, NULL, NULL, data);
46+
mark_object(&c->object, NULL, data);
4847
}
4948

5049
struct recent_data {

revision.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,14 @@
2121

2222
volatile show_early_output_fn_t show_early_output;
2323

24-
char *path_name(struct strbuf *path, const char *name)
24+
void show_object_with_name(FILE *out, struct object *obj, const char *name)
2525
{
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;
3827

3928
fprintf(out, "%s ", sha1_to_hex(obj->sha1));
4029
for (p = name; *p && *p != '\n'; p++)
4130
fputc(*p, out);
4231
fputc('\n', out);
43-
44-
free(name);
4532
}
4633

4734
static void mark_blob_uninteresting(struct blob *blob)

revision.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@ extern void mark_tree_uninteresting(struct tree *tree);
258258

259259
char *path_name(struct strbuf *path, const char *name);
260260

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 *);
263262

264263
extern void add_pending_object(struct rev_info *revs,
265264
struct object *obj, const char *name);

0 commit comments

Comments
 (0)