2021-10-07 20:25:02 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 Google LLC
|
|
|
|
|
|
|
|
Use of this source code is governed by a BSD-style
|
|
|
|
license that can be found in the LICENSE file or at
|
|
|
|
https://developers.google.com/open-source/licenses/bsd
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "record.h"
|
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
#include "basics.h"
|
|
|
|
#include "constants.h"
|
|
|
|
#include "test_framework.h"
|
|
|
|
#include "reftable-tests.h"
|
|
|
|
|
|
|
|
static void test_copy(struct reftable_record *rec)
|
|
|
|
{
|
2024-02-06 06:35:59 +00:00
|
|
|
struct reftable_record copy;
|
2022-01-20 15:12:13 +00:00
|
|
|
uint8_t typ;
|
|
|
|
|
|
|
|
typ = reftable_record_type(rec);
|
2024-02-06 06:35:59 +00:00
|
|
|
reftable_record_init(©, typ);
|
2021-10-07 20:25:02 +00:00
|
|
|
reftable_record_copy_from(©, rec, GIT_SHA1_RAWSZ);
|
|
|
|
/* do it twice to catch memory leaks */
|
|
|
|
reftable_record_copy_from(©, rec, GIT_SHA1_RAWSZ);
|
2022-01-20 15:12:11 +00:00
|
|
|
EXPECT(reftable_record_equal(rec, ©, GIT_SHA1_RAWSZ));
|
2022-01-20 15:12:14 +00:00
|
|
|
|
|
|
|
puts("testing print coverage:\n");
|
|
|
|
reftable_record_print(©, GIT_SHA1_RAWSZ);
|
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
reftable_record_release(©);
|
2021-10-07 20:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_varint_roundtrip(void)
|
|
|
|
{
|
|
|
|
uint64_t inputs[] = { 0,
|
|
|
|
1,
|
|
|
|
27,
|
|
|
|
127,
|
|
|
|
128,
|
|
|
|
257,
|
|
|
|
4096,
|
|
|
|
((uint64_t)1 << 63),
|
|
|
|
((uint64_t)1 << 63) + ((uint64_t)1 << 63) - 1 };
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(inputs); i++) {
|
|
|
|
uint8_t dest[10];
|
|
|
|
|
|
|
|
struct string_view out = {
|
|
|
|
.buf = dest,
|
|
|
|
.len = sizeof(dest),
|
|
|
|
};
|
|
|
|
uint64_t in = inputs[i];
|
|
|
|
int n = put_var_int(&out, in);
|
|
|
|
uint64_t got = 0;
|
|
|
|
|
|
|
|
EXPECT(n > 0);
|
|
|
|
out.len = n;
|
|
|
|
n = get_var_int(&got, &out);
|
|
|
|
EXPECT(n > 0);
|
|
|
|
|
|
|
|
EXPECT(got == in);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_common_prefix(void)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
const char *a, *b;
|
|
|
|
int want;
|
|
|
|
} cases[] = {
|
|
|
|
{ "abc", "ab", 2 },
|
|
|
|
{ "", "abc", 0 },
|
|
|
|
{ "abc", "abd", 2 },
|
|
|
|
{ "abc", "pqr", 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(cases); i++) {
|
|
|
|
struct strbuf a = STRBUF_INIT;
|
|
|
|
struct strbuf b = STRBUF_INIT;
|
|
|
|
strbuf_addstr(&a, cases[i].a);
|
|
|
|
strbuf_addstr(&b, cases[i].b);
|
|
|
|
EXPECT(common_prefix_size(&a, &b) == cases[i].want);
|
|
|
|
|
|
|
|
strbuf_release(&a);
|
|
|
|
strbuf_release(&b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_hash(uint8_t *h, int j)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < hash_size(GIT_SHA1_FORMAT_ID); i++) {
|
|
|
|
h[i] = (j >> i) & 0xff;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_reftable_ref_record_roundtrip(void)
|
|
|
|
{
|
reftable/record: use scratch buffer when decoding records
When decoding log records we need a temporary buffer to decode the
reflog entry's name, mail address and message. As this buffer is local
to the function we thus have to reallocate it for every single log
record which we're about to decode, which is inefficient.
Refactor the code such that callers need to pass in a scratch buffer,
which allows us to reuse it for multiple decodes. This reduces the
number of allocations when iterating through reflogs. Before:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 2,068,487 allocs, 2,068,365 frees, 305,122,946 bytes allocated
After:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 1,068,485 allocs, 1,068,363 frees, 281,122,886 bytes allocated
Note that this commit also drop some redundant calls to `strbuf_reset()`
right before calling `decode_string()`. The latter already knows to
reset the buffer, so there is no need for these.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-05 12:11:16 +00:00
|
|
|
struct strbuf scratch = STRBUF_INIT;
|
2021-10-07 20:25:02 +00:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (i = REFTABLE_REF_DELETION; i < REFTABLE_NR_REF_VALUETYPES; i++) {
|
2022-01-20 15:12:13 +00:00
|
|
|
struct reftable_record in = {
|
|
|
|
.type = BLOCK_TYPE_REF,
|
|
|
|
};
|
|
|
|
struct reftable_record out = { .type = BLOCK_TYPE_REF };
|
2021-10-07 20:25:02 +00:00
|
|
|
struct strbuf key = STRBUF_INIT;
|
|
|
|
uint8_t buffer[1024] = { 0 };
|
|
|
|
struct string_view dest = {
|
|
|
|
.buf = buffer,
|
|
|
|
.len = sizeof(buffer),
|
|
|
|
};
|
|
|
|
int n, m;
|
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
in.u.ref.value_type = i;
|
2021-10-07 20:25:02 +00:00
|
|
|
switch (i) {
|
|
|
|
case REFTABLE_REF_DELETION:
|
|
|
|
break;
|
|
|
|
case REFTABLE_REF_VAL1:
|
2022-01-20 15:12:13 +00:00
|
|
|
set_hash(in.u.ref.value.val1, 1);
|
2021-10-07 20:25:02 +00:00
|
|
|
break;
|
|
|
|
case REFTABLE_REF_VAL2:
|
2022-01-20 15:12:13 +00:00
|
|
|
set_hash(in.u.ref.value.val2.value, 1);
|
|
|
|
set_hash(in.u.ref.value.val2.target_value, 2);
|
2021-10-07 20:25:02 +00:00
|
|
|
break;
|
|
|
|
case REFTABLE_REF_SYMREF:
|
2022-01-20 15:12:13 +00:00
|
|
|
in.u.ref.value.symref = xstrdup("target");
|
2021-10-07 20:25:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-01-20 15:12:13 +00:00
|
|
|
in.u.ref.refname = xstrdup("refs/heads/master");
|
2021-10-07 20:25:02 +00:00
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
test_copy(&in);
|
2021-10-07 20:25:02 +00:00
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
EXPECT(reftable_record_val_type(&in) == i);
|
2021-10-07 20:25:02 +00:00
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
reftable_record_key(&in, &key);
|
|
|
|
n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
|
2021-10-07 20:25:02 +00:00
|
|
|
EXPECT(n > 0);
|
|
|
|
|
|
|
|
/* decode into a non-zero reftable_record to test for leaks. */
|
reftable/record: use scratch buffer when decoding records
When decoding log records we need a temporary buffer to decode the
reflog entry's name, mail address and message. As this buffer is local
to the function we thus have to reallocate it for every single log
record which we're about to decode, which is inefficient.
Refactor the code such that callers need to pass in a scratch buffer,
which allows us to reuse it for multiple decodes. This reduces the
number of allocations when iterating through reflogs. Before:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 2,068,487 allocs, 2,068,365 frees, 305,122,946 bytes allocated
After:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 1,068,485 allocs, 1,068,363 frees, 281,122,886 bytes allocated
Note that this commit also drop some redundant calls to `strbuf_reset()`
right before calling `decode_string()`. The latter already knows to
reset the buffer, so there is no need for these.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-05 12:11:16 +00:00
|
|
|
m = reftable_record_decode(&out, key, i, dest, GIT_SHA1_RAWSZ, &scratch);
|
2021-10-07 20:25:02 +00:00
|
|
|
EXPECT(n == m);
|
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
EXPECT(reftable_ref_record_equal(&in.u.ref, &out.u.ref,
|
|
|
|
GIT_SHA1_RAWSZ));
|
|
|
|
reftable_record_release(&in);
|
2021-10-07 20:25:02 +00:00
|
|
|
|
|
|
|
strbuf_release(&key);
|
2022-01-20 15:12:13 +00:00
|
|
|
reftable_record_release(&out);
|
2021-10-07 20:25:02 +00:00
|
|
|
}
|
reftable/record: use scratch buffer when decoding records
When decoding log records we need a temporary buffer to decode the
reflog entry's name, mail address and message. As this buffer is local
to the function we thus have to reallocate it for every single log
record which we're about to decode, which is inefficient.
Refactor the code such that callers need to pass in a scratch buffer,
which allows us to reuse it for multiple decodes. This reduces the
number of allocations when iterating through reflogs. Before:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 2,068,487 allocs, 2,068,365 frees, 305,122,946 bytes allocated
After:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 1,068,485 allocs, 1,068,363 frees, 281,122,886 bytes allocated
Note that this commit also drop some redundant calls to `strbuf_reset()`
right before calling `decode_string()`. The latter already knows to
reset the buffer, so there is no need for these.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-05 12:11:16 +00:00
|
|
|
|
|
|
|
strbuf_release(&scratch);
|
2021-10-07 20:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_reftable_log_record_equal(void)
|
|
|
|
{
|
|
|
|
struct reftable_log_record in[2] = {
|
|
|
|
{
|
|
|
|
.refname = xstrdup("refs/heads/master"),
|
|
|
|
.update_index = 42,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.refname = xstrdup("refs/heads/master"),
|
|
|
|
.update_index = 22,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
EXPECT(!reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
|
|
|
|
in[1].update_index = in[0].update_index;
|
|
|
|
EXPECT(reftable_log_record_equal(&in[0], &in[1], GIT_SHA1_RAWSZ));
|
|
|
|
reftable_log_record_release(&in[0]);
|
|
|
|
reftable_log_record_release(&in[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_reftable_log_record_roundtrip(void)
|
|
|
|
{
|
|
|
|
int i;
|
2022-01-20 15:12:14 +00:00
|
|
|
struct reftable_log_record in[] = {
|
2021-10-07 20:25:02 +00:00
|
|
|
{
|
|
|
|
.refname = xstrdup("refs/heads/master"),
|
|
|
|
.update_index = 42,
|
|
|
|
.value_type = REFTABLE_LOG_UPDATE,
|
|
|
|
.value = {
|
|
|
|
.update = {
|
|
|
|
.name = xstrdup("han-wen"),
|
|
|
|
.email = xstrdup("hanwen@google.com"),
|
|
|
|
.message = xstrdup("test"),
|
|
|
|
.time = 1577123507,
|
|
|
|
.tz_offset = 100,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.refname = xstrdup("refs/heads/master"),
|
|
|
|
.update_index = 22,
|
|
|
|
.value_type = REFTABLE_LOG_DELETION,
|
2022-01-20 15:12:14 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.refname = xstrdup("branch"),
|
|
|
|
.update_index = 33,
|
|
|
|
.value_type = REFTABLE_LOG_UPDATE,
|
2021-10-07 20:25:02 +00:00
|
|
|
}
|
|
|
|
};
|
reftable/record: use scratch buffer when decoding records
When decoding log records we need a temporary buffer to decode the
reflog entry's name, mail address and message. As this buffer is local
to the function we thus have to reallocate it for every single log
record which we're about to decode, which is inefficient.
Refactor the code such that callers need to pass in a scratch buffer,
which allows us to reuse it for multiple decodes. This reduces the
number of allocations when iterating through reflogs. Before:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 2,068,487 allocs, 2,068,365 frees, 305,122,946 bytes allocated
After:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 1,068,485 allocs, 1,068,363 frees, 281,122,886 bytes allocated
Note that this commit also drop some redundant calls to `strbuf_reset()`
right before calling `decode_string()`. The latter already knows to
reset the buffer, so there is no need for these.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-05 12:11:16 +00:00
|
|
|
struct strbuf scratch = STRBUF_INIT;
|
|
|
|
|
2021-10-07 20:25:02 +00:00
|
|
|
set_test_hash(in[0].value.update.new_hash, 1);
|
|
|
|
set_test_hash(in[0].value.update.old_hash, 2);
|
2022-01-20 15:12:14 +00:00
|
|
|
set_test_hash(in[2].value.update.new_hash, 3);
|
|
|
|
set_test_hash(in[2].value.update.old_hash, 4);
|
2021-10-07 20:25:02 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(in); i++) {
|
2022-01-20 15:12:13 +00:00
|
|
|
struct reftable_record rec = { .type = BLOCK_TYPE_LOG };
|
2021-10-07 20:25:02 +00:00
|
|
|
struct strbuf key = STRBUF_INIT;
|
|
|
|
uint8_t buffer[1024] = { 0 };
|
|
|
|
struct string_view dest = {
|
|
|
|
.buf = buffer,
|
|
|
|
.len = sizeof(buffer),
|
|
|
|
};
|
|
|
|
/* populate out, to check for leaks. */
|
2022-01-20 15:12:13 +00:00
|
|
|
struct reftable_record out = {
|
|
|
|
.type = BLOCK_TYPE_LOG,
|
|
|
|
.u.log = {
|
|
|
|
.refname = xstrdup("old name"),
|
|
|
|
.value_type = REFTABLE_LOG_UPDATE,
|
|
|
|
.value = {
|
|
|
|
.update = {
|
|
|
|
.name = xstrdup("old name"),
|
|
|
|
.email = xstrdup("old@email"),
|
|
|
|
.message = xstrdup("old message"),
|
|
|
|
},
|
2021-10-07 20:25:02 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
int n, m, valtype;
|
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
rec.u.log = in[i];
|
2021-10-07 20:25:02 +00:00
|
|
|
|
|
|
|
test_copy(&rec);
|
|
|
|
|
|
|
|
reftable_record_key(&rec, &key);
|
|
|
|
|
|
|
|
n = reftable_record_encode(&rec, dest, GIT_SHA1_RAWSZ);
|
|
|
|
EXPECT(n >= 0);
|
|
|
|
valtype = reftable_record_val_type(&rec);
|
2022-01-20 15:12:13 +00:00
|
|
|
m = reftable_record_decode(&out, key, valtype, dest,
|
reftable/record: use scratch buffer when decoding records
When decoding log records we need a temporary buffer to decode the
reflog entry's name, mail address and message. As this buffer is local
to the function we thus have to reallocate it for every single log
record which we're about to decode, which is inefficient.
Refactor the code such that callers need to pass in a scratch buffer,
which allows us to reuse it for multiple decodes. This reduces the
number of allocations when iterating through reflogs. Before:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 2,068,487 allocs, 2,068,365 frees, 305,122,946 bytes allocated
After:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 1,068,485 allocs, 1,068,363 frees, 281,122,886 bytes allocated
Note that this commit also drop some redundant calls to `strbuf_reset()`
right before calling `decode_string()`. The latter already knows to
reset the buffer, so there is no need for these.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-05 12:11:16 +00:00
|
|
|
GIT_SHA1_RAWSZ, &scratch);
|
2021-10-07 20:25:02 +00:00
|
|
|
EXPECT(n == m);
|
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
EXPECT(reftable_log_record_equal(&in[i], &out.u.log,
|
|
|
|
GIT_SHA1_RAWSZ));
|
2021-10-07 20:25:02 +00:00
|
|
|
reftable_log_record_release(&in[i]);
|
|
|
|
strbuf_release(&key);
|
2022-01-20 15:12:13 +00:00
|
|
|
reftable_record_release(&out);
|
2021-10-07 20:25:02 +00:00
|
|
|
}
|
reftable/record: use scratch buffer when decoding records
When decoding log records we need a temporary buffer to decode the
reflog entry's name, mail address and message. As this buffer is local
to the function we thus have to reallocate it for every single log
record which we're about to decode, which is inefficient.
Refactor the code such that callers need to pass in a scratch buffer,
which allows us to reuse it for multiple decodes. This reduces the
number of allocations when iterating through reflogs. Before:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 2,068,487 allocs, 2,068,365 frees, 305,122,946 bytes allocated
After:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 1,068,485 allocs, 1,068,363 frees, 281,122,886 bytes allocated
Note that this commit also drop some redundant calls to `strbuf_reset()`
right before calling `decode_string()`. The latter already knows to
reset the buffer, so there is no need for these.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-05 12:11:16 +00:00
|
|
|
|
|
|
|
strbuf_release(&scratch);
|
2021-10-07 20:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_u24_roundtrip(void)
|
|
|
|
{
|
|
|
|
uint32_t in = 0x112233;
|
|
|
|
uint8_t dest[3];
|
|
|
|
uint32_t out;
|
|
|
|
put_be24(dest, in);
|
|
|
|
out = get_be24(dest);
|
|
|
|
EXPECT(in == out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_key_roundtrip(void)
|
|
|
|
{
|
|
|
|
uint8_t buffer[1024] = { 0 };
|
|
|
|
struct string_view dest = {
|
|
|
|
.buf = buffer,
|
|
|
|
.len = sizeof(buffer),
|
|
|
|
};
|
|
|
|
struct strbuf last_key = STRBUF_INIT;
|
|
|
|
struct strbuf key = STRBUF_INIT;
|
|
|
|
struct strbuf roundtrip = STRBUF_INIT;
|
|
|
|
int restart;
|
|
|
|
uint8_t extra;
|
|
|
|
int n, m;
|
|
|
|
uint8_t rt_extra;
|
|
|
|
|
|
|
|
strbuf_addstr(&last_key, "refs/heads/master");
|
|
|
|
strbuf_addstr(&key, "refs/tags/bla");
|
|
|
|
extra = 6;
|
|
|
|
n = reftable_encode_key(&restart, dest, last_key, key, extra);
|
|
|
|
EXPECT(!restart);
|
|
|
|
EXPECT(n > 0);
|
|
|
|
|
reftable/record: decode keys in place
When reading a record from a block, we need to decode the record's key.
As reftable keys are prefix-compressed, meaning they reuse a prefix from
the preceding record's key, this is a bit more involved than just having
to copy the relevant bytes: we need to figure out the prefix and suffix
lengths, copy the prefix from the preceding record and finally copy the
suffix from the current record.
This is done by passing three buffers to `reftable_decode_key()`: one
buffer that holds the result, one buffer that holds the last key, and
one buffer that points to the current record. The final key is then
assembled by calling `strbuf_add()` twice to copy over the prefix and
suffix.
Performing two memory copies is inefficient though. And we can indeed do
better by decoding keys in place. Instead of providing two buffers, the
caller may only call a single buffer that is already pre-populated with
the last key. Like this, we only have to call `strbuf_setlen()` to trim
the record to its prefix and then `strbuf_add()` to add the suffix.
This refactoring leads to a noticeable performance bump when iterating
over 1 million refs:
Benchmark 1: show-ref: single matching ref (revision = HEAD~)
Time (mean ± σ): 112.2 ms ± 3.9 ms [User: 109.3 ms, System: 2.8 ms]
Range (min … max): 109.2 ms … 149.6 ms 1000 runs
Benchmark 2: show-ref: single matching ref (revision = HEAD)
Time (mean ± σ): 106.0 ms ± 3.5 ms [User: 103.2 ms, System: 2.7 ms]
Range (min … max): 103.2 ms … 133.7 ms 1000 runs
Summary
show-ref: single matching ref (revision = HEAD) ran
1.06 ± 0.05 times faster than show-ref: single matching ref (revision = HEAD~)
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-04 10:49:31 +00:00
|
|
|
strbuf_addstr(&roundtrip, "refs/heads/master");
|
|
|
|
m = reftable_decode_key(&roundtrip, &rt_extra, dest);
|
2021-10-07 20:25:02 +00:00
|
|
|
EXPECT(n == m);
|
|
|
|
EXPECT(0 == strbuf_cmp(&key, &roundtrip));
|
|
|
|
EXPECT(rt_extra == extra);
|
|
|
|
|
|
|
|
strbuf_release(&last_key);
|
|
|
|
strbuf_release(&key);
|
|
|
|
strbuf_release(&roundtrip);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_reftable_obj_record_roundtrip(void)
|
|
|
|
{
|
|
|
|
uint8_t testHash1[GIT_SHA1_RAWSZ] = { 1, 2, 3, 4, 0 };
|
|
|
|
uint64_t till9[] = { 1, 2, 3, 4, 500, 600, 700, 800, 9000 };
|
reftable/record: use scratch buffer when decoding records
When decoding log records we need a temporary buffer to decode the
reflog entry's name, mail address and message. As this buffer is local
to the function we thus have to reallocate it for every single log
record which we're about to decode, which is inefficient.
Refactor the code such that callers need to pass in a scratch buffer,
which allows us to reuse it for multiple decodes. This reduces the
number of allocations when iterating through reflogs. Before:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 2,068,487 allocs, 2,068,365 frees, 305,122,946 bytes allocated
After:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 1,068,485 allocs, 1,068,363 frees, 281,122,886 bytes allocated
Note that this commit also drop some redundant calls to `strbuf_reset()`
right before calling `decode_string()`. The latter already knows to
reset the buffer, so there is no need for these.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-05 12:11:16 +00:00
|
|
|
struct reftable_obj_record recs[3] = {
|
|
|
|
{
|
|
|
|
.hash_prefix = testHash1,
|
|
|
|
.hash_prefix_len = 5,
|
|
|
|
.offsets = till9,
|
|
|
|
.offset_len = 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.hash_prefix = testHash1,
|
|
|
|
.hash_prefix_len = 5,
|
|
|
|
.offsets = till9,
|
|
|
|
.offset_len = 9,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.hash_prefix = testHash1,
|
|
|
|
.hash_prefix_len = 5,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
struct strbuf scratch = STRBUF_INIT;
|
2021-10-07 20:25:02 +00:00
|
|
|
int i = 0;
|
reftable/record: use scratch buffer when decoding records
When decoding log records we need a temporary buffer to decode the
reflog entry's name, mail address and message. As this buffer is local
to the function we thus have to reallocate it for every single log
record which we're about to decode, which is inefficient.
Refactor the code such that callers need to pass in a scratch buffer,
which allows us to reuse it for multiple decodes. This reduces the
number of allocations when iterating through reflogs. Before:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 2,068,487 allocs, 2,068,365 frees, 305,122,946 bytes allocated
After:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 1,068,485 allocs, 1,068,363 frees, 281,122,886 bytes allocated
Note that this commit also drop some redundant calls to `strbuf_reset()`
right before calling `decode_string()`. The latter already knows to
reset the buffer, so there is no need for these.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-05 12:11:16 +00:00
|
|
|
|
2021-10-07 20:25:02 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(recs); i++) {
|
|
|
|
uint8_t buffer[1024] = { 0 };
|
|
|
|
struct string_view dest = {
|
|
|
|
.buf = buffer,
|
|
|
|
.len = sizeof(buffer),
|
|
|
|
};
|
2022-01-20 15:12:13 +00:00
|
|
|
struct reftable_record in = {
|
|
|
|
.type = BLOCK_TYPE_OBJ,
|
2022-03-28 19:10:04 +00:00
|
|
|
.u = {
|
|
|
|
.obj = recs[i],
|
|
|
|
},
|
2022-01-20 15:12:13 +00:00
|
|
|
};
|
2021-10-07 20:25:02 +00:00
|
|
|
struct strbuf key = STRBUF_INIT;
|
2022-01-20 15:12:13 +00:00
|
|
|
struct reftable_record out = { .type = BLOCK_TYPE_OBJ };
|
2021-10-07 20:25:02 +00:00
|
|
|
int n, m;
|
|
|
|
uint8_t extra;
|
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
test_copy(&in);
|
|
|
|
reftable_record_key(&in, &key);
|
|
|
|
n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
|
2021-10-07 20:25:02 +00:00
|
|
|
EXPECT(n > 0);
|
2022-01-20 15:12:13 +00:00
|
|
|
extra = reftable_record_val_type(&in);
|
|
|
|
m = reftable_record_decode(&out, key, extra, dest,
|
reftable/record: use scratch buffer when decoding records
When decoding log records we need a temporary buffer to decode the
reflog entry's name, mail address and message. As this buffer is local
to the function we thus have to reallocate it for every single log
record which we're about to decode, which is inefficient.
Refactor the code such that callers need to pass in a scratch buffer,
which allows us to reuse it for multiple decodes. This reduces the
number of allocations when iterating through reflogs. Before:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 2,068,487 allocs, 2,068,365 frees, 305,122,946 bytes allocated
After:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 1,068,485 allocs, 1,068,363 frees, 281,122,886 bytes allocated
Note that this commit also drop some redundant calls to `strbuf_reset()`
right before calling `decode_string()`. The latter already knows to
reset the buffer, so there is no need for these.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-05 12:11:16 +00:00
|
|
|
GIT_SHA1_RAWSZ, &scratch);
|
2021-10-07 20:25:02 +00:00
|
|
|
EXPECT(n == m);
|
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
|
2021-10-07 20:25:02 +00:00
|
|
|
strbuf_release(&key);
|
2022-01-20 15:12:13 +00:00
|
|
|
reftable_record_release(&out);
|
2021-10-07 20:25:02 +00:00
|
|
|
}
|
reftable/record: use scratch buffer when decoding records
When decoding log records we need a temporary buffer to decode the
reflog entry's name, mail address and message. As this buffer is local
to the function we thus have to reallocate it for every single log
record which we're about to decode, which is inefficient.
Refactor the code such that callers need to pass in a scratch buffer,
which allows us to reuse it for multiple decodes. This reduces the
number of allocations when iterating through reflogs. Before:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 2,068,487 allocs, 2,068,365 frees, 305,122,946 bytes allocated
After:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 1,068,485 allocs, 1,068,363 frees, 281,122,886 bytes allocated
Note that this commit also drop some redundant calls to `strbuf_reset()`
right before calling `decode_string()`. The latter already knows to
reset the buffer, so there is no need for these.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-05 12:11:16 +00:00
|
|
|
|
|
|
|
strbuf_release(&scratch);
|
2021-10-07 20:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_reftable_index_record_roundtrip(void)
|
|
|
|
{
|
2022-01-20 15:12:13 +00:00
|
|
|
struct reftable_record in = {
|
|
|
|
.type = BLOCK_TYPE_INDEX,
|
|
|
|
.u.idx = {
|
|
|
|
.offset = 42,
|
|
|
|
.last_key = STRBUF_INIT,
|
|
|
|
},
|
2021-10-07 20:25:02 +00:00
|
|
|
};
|
|
|
|
uint8_t buffer[1024] = { 0 };
|
|
|
|
struct string_view dest = {
|
|
|
|
.buf = buffer,
|
|
|
|
.len = sizeof(buffer),
|
|
|
|
};
|
reftable/record: use scratch buffer when decoding records
When decoding log records we need a temporary buffer to decode the
reflog entry's name, mail address and message. As this buffer is local
to the function we thus have to reallocate it for every single log
record which we're about to decode, which is inefficient.
Refactor the code such that callers need to pass in a scratch buffer,
which allows us to reuse it for multiple decodes. This reduces the
number of allocations when iterating through reflogs. Before:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 2,068,487 allocs, 2,068,365 frees, 305,122,946 bytes allocated
After:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 1,068,485 allocs, 1,068,363 frees, 281,122,886 bytes allocated
Note that this commit also drop some redundant calls to `strbuf_reset()`
right before calling `decode_string()`. The latter already knows to
reset the buffer, so there is no need for these.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-05 12:11:16 +00:00
|
|
|
struct strbuf scratch = STRBUF_INIT;
|
2021-10-07 20:25:02 +00:00
|
|
|
struct strbuf key = STRBUF_INIT;
|
2022-01-20 15:12:13 +00:00
|
|
|
struct reftable_record out = {
|
|
|
|
.type = BLOCK_TYPE_INDEX,
|
|
|
|
.u.idx = { .last_key = STRBUF_INIT },
|
|
|
|
};
|
2021-10-07 20:25:02 +00:00
|
|
|
int n, m;
|
|
|
|
uint8_t extra;
|
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
strbuf_addstr(&in.u.idx.last_key, "refs/heads/master");
|
|
|
|
reftable_record_key(&in, &key);
|
|
|
|
test_copy(&in);
|
2021-10-07 20:25:02 +00:00
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
EXPECT(0 == strbuf_cmp(&key, &in.u.idx.last_key));
|
|
|
|
n = reftable_record_encode(&in, dest, GIT_SHA1_RAWSZ);
|
2021-10-07 20:25:02 +00:00
|
|
|
EXPECT(n > 0);
|
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
extra = reftable_record_val_type(&in);
|
reftable/record: use scratch buffer when decoding records
When decoding log records we need a temporary buffer to decode the
reflog entry's name, mail address and message. As this buffer is local
to the function we thus have to reallocate it for every single log
record which we're about to decode, which is inefficient.
Refactor the code such that callers need to pass in a scratch buffer,
which allows us to reuse it for multiple decodes. This reduces the
number of allocations when iterating through reflogs. Before:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 2,068,487 allocs, 2,068,365 frees, 305,122,946 bytes allocated
After:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 1,068,485 allocs, 1,068,363 frees, 281,122,886 bytes allocated
Note that this commit also drop some redundant calls to `strbuf_reset()`
right before calling `decode_string()`. The latter already knows to
reset the buffer, so there is no need for these.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-05 12:11:16 +00:00
|
|
|
m = reftable_record_decode(&out, key, extra, dest, GIT_SHA1_RAWSZ,
|
|
|
|
&scratch);
|
2021-10-07 20:25:02 +00:00
|
|
|
EXPECT(m == n);
|
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
EXPECT(reftable_record_equal(&in, &out, GIT_SHA1_RAWSZ));
|
2021-10-07 20:25:02 +00:00
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
reftable_record_release(&out);
|
2021-10-07 20:25:02 +00:00
|
|
|
strbuf_release(&key);
|
reftable/record: use scratch buffer when decoding records
When decoding log records we need a temporary buffer to decode the
reflog entry's name, mail address and message. As this buffer is local
to the function we thus have to reallocate it for every single log
record which we're about to decode, which is inefficient.
Refactor the code such that callers need to pass in a scratch buffer,
which allows us to reuse it for multiple decodes. This reduces the
number of allocations when iterating through reflogs. Before:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 2,068,487 allocs, 2,068,365 frees, 305,122,946 bytes allocated
After:
HEAP SUMMARY:
in use at exit: 13,473 bytes in 122 blocks
total heap usage: 1,068,485 allocs, 1,068,363 frees, 281,122,886 bytes allocated
Note that this commit also drop some redundant calls to `strbuf_reset()`
right before calling `decode_string()`. The latter already knows to
reset the buffer, so there is no need for these.
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-03-05 12:11:16 +00:00
|
|
|
strbuf_release(&scratch);
|
2022-01-20 15:12:13 +00:00
|
|
|
strbuf_release(&in.u.idx.last_key);
|
2021-10-07 20:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int record_test_main(int argc, const char *argv[])
|
|
|
|
{
|
|
|
|
RUN_TEST(test_reftable_log_record_equal);
|
|
|
|
RUN_TEST(test_reftable_log_record_roundtrip);
|
|
|
|
RUN_TEST(test_reftable_ref_record_roundtrip);
|
|
|
|
RUN_TEST(test_varint_roundtrip);
|
|
|
|
RUN_TEST(test_key_roundtrip);
|
|
|
|
RUN_TEST(test_common_prefix);
|
|
|
|
RUN_TEST(test_reftable_obj_record_roundtrip);
|
|
|
|
RUN_TEST(test_reftable_index_record_roundtrip);
|
|
|
|
RUN_TEST(test_u24_roundtrip);
|
|
|
|
return 0;
|
|
|
|
}
|