ls-refs: reuse buffer when sending refs
In the initial reference advertisement, the Git server will first announce all of its references to the client. The logic is handled in `send_ref()`, which will allocate a new buffer for each refline it is about to send. This is quite wasteful: instead of allocating a new buffer each time, we can just reuse a buffer. Improve this by passing in a buffer via the `ls_refs_data` struct which is then reused on each reference. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Aug 25, 2021 at 15:49 UTC
f54b9f21cab23209697d9985c16a7bf4ee7b4241
1 file changed
+11
-8
ls-refs.c
+11
-8
@@ -65,6 +65,7 @@ struct ls_refs_data {
65
unsigned peel;
66
unsigned symrefs;
67
struct strvec prefixes;
68
+ struct strbuf buf;
69
unsigned unborn : 1;
70
};
71
@@ -73,7 +74,8 @@ static int send_ref(const char *refname, const struct object_id *oid,
74
{
75
struct ls_refs_data *data = cb_data;
76
const char *refname_nons = strip_namespace(refname);
76
- struct strbuf refline = STRBUF_INIT;
77
+
78
+ strbuf_reset(&data->buf);
79
80
if (ref_is_hidden(refname_nons, refname))
81
return 0;
@@ -82,9 +84,9 @@ static int send_ref(const char *refname, const struct object_id *oid,
84
return 0;
85
86
if (oid)
85
- strbuf_addf(&refline, "%s %s", oid_to_hex(oid), refname_nons);
87
+ strbuf_addf(&data->buf, "%s %s", oid_to_hex(oid), refname_nons);
88
else
87
- strbuf_addf(&refline, "unborn %s", refname_nons);
89
+ strbuf_addf(&data->buf, "unborn %s", refname_nons);
90
if (data->symrefs && flag & REF_ISSYMREF) {
91
struct object_id unused;
92
const char *symref_target = resolve_ref_unsafe(refname, 0,
@@ -94,20 +96,19 @@ static int send_ref(const char *refname, const struct object_id *oid,
96
if (!symref_target)
97
die("'%s' is a symref but it is not?", refname);
98
97
- strbuf_addf(&refline, " symref-target:%s",
99
+ strbuf_addf(&data->buf, " symref-target:%s",
100
strip_namespace(symref_target));
101
}
102
103
if (data->peel && oid) {
104
struct object_id peeled;
105
if (!peel_iterated_oid(oid, &peeled))
104
- strbuf_addf(&refline, " peeled:%s", oid_to_hex(&peeled));
106
+ strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled));
107
}
108
107
- strbuf_addch(&refline, '\n');
108
- packet_write(1, refline.buf, refline.len);
109
+ strbuf_addch(&data->buf, '\n');
110
+ packet_write(1, data->buf.buf, data->buf.len);
111
110
- strbuf_release(&refline);
112
return 0;
113
}
114
@@ -145,6 +146,7 @@ int ls_refs(struct repository *r, struct strvec *keys,
146
147
memset(&data, 0, sizeof(data));
148
strvec_init(&data.prefixes);
149
+ strbuf_init(&data.buf, 0);
150
151
ensure_config_read();
152
git_config(ls_refs_config, NULL);
@@ -173,6 +175,7 @@ int ls_refs(struct repository *r, struct strvec *keys,
175
send_ref, &data, 0);
176
packet_flush(1);
177
strvec_clear(&data.prefixes);
178
+ strbuf_release(&data.buf);
179
return 0;
180
}
181