2021-10-07 20:25:04 +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 "block.h"
|
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
#include "blocksource.h"
|
|
|
|
#include "basics.h"
|
|
|
|
#include "constants.h"
|
|
|
|
#include "record.h"
|
|
|
|
#include "test_framework.h"
|
|
|
|
#include "reftable-tests.h"
|
|
|
|
|
|
|
|
static void test_block_read_write(void)
|
|
|
|
{
|
|
|
|
const int header_off = 21; /* random */
|
|
|
|
char *names[30];
|
|
|
|
const int N = ARRAY_SIZE(names);
|
|
|
|
const int block_size = 1024;
|
|
|
|
struct reftable_block block = { NULL };
|
|
|
|
struct block_writer bw = {
|
|
|
|
.last_key = STRBUF_INIT,
|
|
|
|
};
|
2022-01-20 15:12:13 +00:00
|
|
|
struct reftable_record rec = {
|
|
|
|
.type = BLOCK_TYPE_REF,
|
|
|
|
};
|
2021-10-07 20:25:04 +00:00
|
|
|
int i = 0;
|
|
|
|
int n;
|
|
|
|
struct block_reader br = { 0 };
|
2023-12-11 09:08:07 +00:00
|
|
|
struct block_iter it = BLOCK_ITER_INIT;
|
2021-10-07 20:25:04 +00:00
|
|
|
int j = 0;
|
|
|
|
struct strbuf want = STRBUF_INIT;
|
|
|
|
|
2024-02-06 06:35:27 +00:00
|
|
|
REFTABLE_CALLOC_ARRAY(block.data, block_size);
|
2021-10-07 20:25:04 +00:00
|
|
|
block.len = block_size;
|
|
|
|
block.source = malloc_block_source();
|
|
|
|
block_writer_init(&bw, BLOCK_TYPE_REF, block.data, block_size,
|
|
|
|
header_off, hash_size(GIT_SHA1_FORMAT_ID));
|
|
|
|
|
2022-02-21 18:46:07 +00:00
|
|
|
rec.u.ref.refname = "";
|
|
|
|
rec.u.ref.value_type = REFTABLE_REF_DELETION;
|
|
|
|
n = block_writer_add(&bw, &rec);
|
|
|
|
EXPECT(n == REFTABLE_API_ERROR);
|
|
|
|
|
2021-10-07 20:25:04 +00:00
|
|
|
for (i = 0; i < N; i++) {
|
|
|
|
char name[100];
|
|
|
|
snprintf(name, sizeof(name), "branch%02d", i);
|
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
rec.u.ref.refname = name;
|
|
|
|
rec.u.ref.value_type = REFTABLE_REF_VAL1;
|
reftable/record: store "val1" hashes as static arrays
When reading ref records of type "val1", we store its object ID in an
allocated array. This results in an additional allocation for every
single ref record we read, which is rather inefficient especially when
iterating over refs.
Refactor the code to instead use an embedded array of `GIT_MAX_RAWSZ`
bytes. While this means that `struct ref_record` is bigger now, we
typically do not store all refs in an array anyway and instead only
handle a limited number of records at the same point in time.
Using `git show-ref --quiet` in a repository with ~350k refs this leads
to a significant drop in allocations. Before:
HEAP SUMMARY:
in use at exit: 21,098 bytes in 192 blocks
total heap usage: 2,116,683 allocs, 2,116,491 frees, 76,098,060 bytes allocated
After:
HEAP SUMMARY:
in use at exit: 21,098 bytes in 192 blocks
total heap usage: 1,419,031 allocs, 1,418,839 frees, 62,145,036 bytes allocated
Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2024-01-03 06:22:30 +00:00
|
|
|
memset(rec.u.ref.value.val1, i, GIT_SHA1_RAWSZ);
|
2021-10-07 20:25:04 +00:00
|
|
|
|
|
|
|
names[i] = xstrdup(name);
|
|
|
|
n = block_writer_add(&bw, &rec);
|
2022-01-20 15:12:13 +00:00
|
|
|
rec.u.ref.refname = NULL;
|
|
|
|
rec.u.ref.value_type = REFTABLE_REF_DELETION;
|
2021-10-07 20:25:04 +00:00
|
|
|
EXPECT(n == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
n = block_writer_finish(&bw);
|
|
|
|
EXPECT(n > 0);
|
|
|
|
|
|
|
|
block_writer_release(&bw);
|
|
|
|
|
|
|
|
block_reader_init(&br, &block, header_off, block_size, GIT_SHA1_RAWSZ);
|
|
|
|
|
|
|
|
block_reader_start(&br, &it);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
int r = block_iter_next(&it, &rec);
|
|
|
|
EXPECT(r >= 0);
|
|
|
|
if (r > 0) {
|
|
|
|
break;
|
|
|
|
}
|
2022-01-20 15:12:13 +00:00
|
|
|
EXPECT_STREQ(names[j], rec.u.ref.refname);
|
2021-10-07 20:25:04 +00:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
|
|
|
|
reftable_record_release(&rec);
|
|
|
|
block_iter_close(&it);
|
|
|
|
|
|
|
|
for (i = 0; i < N; i++) {
|
2023-12-11 09:08:07 +00:00
|
|
|
struct block_iter it = BLOCK_ITER_INIT;
|
2021-10-07 20:25:04 +00:00
|
|
|
strbuf_reset(&want);
|
|
|
|
strbuf_addstr(&want, names[i]);
|
|
|
|
|
|
|
|
n = block_reader_seek(&br, &it, &want);
|
|
|
|
EXPECT(n == 0);
|
|
|
|
|
|
|
|
n = block_iter_next(&it, &rec);
|
|
|
|
EXPECT(n == 0);
|
|
|
|
|
2022-01-20 15:12:13 +00:00
|
|
|
EXPECT_STREQ(names[i], rec.u.ref.refname);
|
2021-10-07 20:25:04 +00:00
|
|
|
|
|
|
|
want.len--;
|
|
|
|
n = block_reader_seek(&br, &it, &want);
|
|
|
|
EXPECT(n == 0);
|
|
|
|
|
|
|
|
n = block_iter_next(&it, &rec);
|
|
|
|
EXPECT(n == 0);
|
2022-01-20 15:12:13 +00:00
|
|
|
EXPECT_STREQ(names[10 * (i / 10)], rec.u.ref.refname);
|
2021-10-07 20:25:04 +00:00
|
|
|
|
|
|
|
block_iter_close(&it);
|
|
|
|
}
|
|
|
|
|
|
|
|
reftable_record_release(&rec);
|
|
|
|
reftable_block_done(&br.block);
|
|
|
|
strbuf_release(&want);
|
|
|
|
for (i = 0; i < N; i++) {
|
|
|
|
reftable_free(names[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int block_test_main(int argc, const char *argv[])
|
|
|
|
{
|
|
|
|
RUN_TEST(test_block_read_write);
|
|
|
|
return 0;
|
|
|
|
}
|