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>
This commit is contained in:
parent
225bc32a98
commit
f54b9f21ca
19
ls-refs.c
19
ls-refs.c
@ -65,6 +65,7 @@ struct ls_refs_data {
|
|||||||
unsigned peel;
|
unsigned peel;
|
||||||
unsigned symrefs;
|
unsigned symrefs;
|
||||||
struct strvec prefixes;
|
struct strvec prefixes;
|
||||||
|
struct strbuf buf;
|
||||||
unsigned unborn : 1;
|
unsigned unborn : 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -73,7 +74,8 @@ static int send_ref(const char *refname, const struct object_id *oid,
|
|||||||
{
|
{
|
||||||
struct ls_refs_data *data = cb_data;
|
struct ls_refs_data *data = cb_data;
|
||||||
const char *refname_nons = strip_namespace(refname);
|
const char *refname_nons = strip_namespace(refname);
|
||||||
struct strbuf refline = STRBUF_INIT;
|
|
||||||
|
strbuf_reset(&data->buf);
|
||||||
|
|
||||||
if (ref_is_hidden(refname_nons, refname))
|
if (ref_is_hidden(refname_nons, refname))
|
||||||
return 0;
|
return 0;
|
||||||
@ -82,9 +84,9 @@ static int send_ref(const char *refname, const struct object_id *oid,
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if (oid)
|
if (oid)
|
||||||
strbuf_addf(&refline, "%s %s", oid_to_hex(oid), refname_nons);
|
strbuf_addf(&data->buf, "%s %s", oid_to_hex(oid), refname_nons);
|
||||||
else
|
else
|
||||||
strbuf_addf(&refline, "unborn %s", refname_nons);
|
strbuf_addf(&data->buf, "unborn %s", refname_nons);
|
||||||
if (data->symrefs && flag & REF_ISSYMREF) {
|
if (data->symrefs && flag & REF_ISSYMREF) {
|
||||||
struct object_id unused;
|
struct object_id unused;
|
||||||
const char *symref_target = resolve_ref_unsafe(refname, 0,
|
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,
|
|||||||
if (!symref_target)
|
if (!symref_target)
|
||||||
die("'%s' is a symref but it is not?", refname);
|
die("'%s' is a symref but it is not?", refname);
|
||||||
|
|
||||||
strbuf_addf(&refline, " symref-target:%s",
|
strbuf_addf(&data->buf, " symref-target:%s",
|
||||||
strip_namespace(symref_target));
|
strip_namespace(symref_target));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (data->peel && oid) {
|
if (data->peel && oid) {
|
||||||
struct object_id peeled;
|
struct object_id peeled;
|
||||||
if (!peel_iterated_oid(oid, &peeled))
|
if (!peel_iterated_oid(oid, &peeled))
|
||||||
strbuf_addf(&refline, " peeled:%s", oid_to_hex(&peeled));
|
strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled));
|
||||||
}
|
}
|
||||||
|
|
||||||
strbuf_addch(&refline, '\n');
|
strbuf_addch(&data->buf, '\n');
|
||||||
packet_write(1, refline.buf, refline.len);
|
packet_write(1, data->buf.buf, data->buf.len);
|
||||||
|
|
||||||
strbuf_release(&refline);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -145,6 +146,7 @@ int ls_refs(struct repository *r, struct strvec *keys,
|
|||||||
|
|
||||||
memset(&data, 0, sizeof(data));
|
memset(&data, 0, sizeof(data));
|
||||||
strvec_init(&data.prefixes);
|
strvec_init(&data.prefixes);
|
||||||
|
strbuf_init(&data.buf, 0);
|
||||||
|
|
||||||
ensure_config_read();
|
ensure_config_read();
|
||||||
git_config(ls_refs_config, NULL);
|
git_config(ls_refs_config, NULL);
|
||||||
@ -173,6 +175,7 @@ int ls_refs(struct repository *r, struct strvec *keys,
|
|||||||
send_ref, &data, 0);
|
send_ref, &data, 0);
|
||||||
packet_flush(1);
|
packet_flush(1);
|
||||||
strvec_clear(&data.prefixes);
|
strvec_clear(&data.prefixes);
|
||||||
|
strbuf_release(&data.buf);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user