Skip to content

Commit de1e67d

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 bd64516 commit de1e67d

File tree

9 files changed

+26
-58
lines changed

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->oid.hash, 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->oid.hash);

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_object_file(&obj->oid))
@@ -188,15 +186,13 @@ static void finish_object(struct object *obj,
188186
parse_object(obj->oid.hash);
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->oid.hash));

pack-bitmap.c

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

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)
419418
{
420419
struct bitmap *base = data;
421420
int bitmap_pos;
422421

423422
bitmap_pos = bitmap_position(object->oid.hash);
424423

425-
if (bitmap_pos < 0) {
426-
char *name = path_name(path, last);
424+
if (bitmap_pos < 0)
427425
bitmap_pos = ext_index_add_object(object, name);
428-
free(name);
429-
}
430426

431427
bitmap_set(base, bitmap_pos);
432428
}
@@ -894,9 +890,8 @@ struct bitmap_test_data {
894890
size_t seen;
895891
};
896892

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)
900895
{
901896
struct bitmap_test_data *tdata = data;
902897
int bitmap_pos;

reachable.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,14 @@ static int add_one_ref(const char *path, const struct object_id *oid,
4343
* The traversal will have already marked us as SEEN, so we
4444
* only need to handle any progress reporting here.
4545
*/
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)
4847
{
4948
update_progress(data);
5049
}
5150

5251
static void mark_commit(struct commit *c, void *data)
5352
{
54-
mark_object(&c->object, NULL, NULL, data);
53+
mark_object(&c->object, NULL, data);
5554
}
5655

5756
struct recent_data {

revision.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,14 @@ volatile show_early_output_fn_t show_early_output;
2525
static const char *term_bad;
2626
static const char *term_good;
2727

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

4332
fprintf(out, "%s ", oid_to_hex(&obj->oid));
4433
for (p = name; *p && *p != '\n'; p++)
4534
fputc(*p, out);
4635
fputc('\n', out);
47-
48-
free(name);
4936
}
5037

5138
static void mark_blob_uninteresting(struct blob *blob)

revision.h

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

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

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

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

0 commit comments

Comments
 (0)