Skip to content

Commit b31e3cc

Browse files
pks-tgitster
authored andcommitted
reftable/record: store "val2" hashes as static arrays
Similar to the preceding commit, convert ref records of type "val2" to store their object IDs in static arrays instead of allocating them for every single record. We're using the same benchmark as in the preceding commit, with `git show-ref --quiet` in a repository with ~350k refs. This time around though the effects aren't this huge. Before: HEAP SUMMARY: in use at exit: 21,163 bytes in 193 blocks total heap usage: 1,419,040 allocs, 1,418,847 frees, 62,153,868 bytes allocated After: HEAP SUMMARY: in use at exit: 21,163 bytes in 193 blocks total heap usage: 1,410,148 allocs, 1,409,955 frees, 61,976,068 bytes allocated This is because "val2"-type records are typically only stored for peeled tags, and the number of annotated tags in the benchmark repository is rather low. Still, it can be seen that this change leads to a reduction of allocations overall, even if only a small one. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7af607c commit b31e3cc

File tree

4 files changed

+6
-20
lines changed

4 files changed

+6
-20
lines changed

reftable/readwrite_test.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,6 @@ static void test_table_refs_for(int indexed)
547547
uint8_t hash[GIT_SHA1_RAWSZ];
548548
char fill[51] = { 0 };
549549
char name[100];
550-
uint8_t hash1[GIT_SHA1_RAWSZ];
551-
uint8_t hash2[GIT_SHA1_RAWSZ];
552550
struct reftable_ref_record ref = { NULL };
553551

554552
memset(hash, i, sizeof(hash));
@@ -558,20 +556,18 @@ static void test_table_refs_for(int indexed)
558556
name[40] = 0;
559557
ref.refname = name;
560558

561-
set_test_hash(hash1, i / 4);
562-
set_test_hash(hash2, 3 + i / 4);
563559
ref.value_type = REFTABLE_REF_VAL2;
564-
ref.value.val2.value = hash1;
565-
ref.value.val2.target_value = hash2;
560+
set_test_hash(ref.value.val2.value, i / 4);
561+
set_test_hash(ref.value.val2.target_value, 3 + i / 4);
566562

567563
/* 80 bytes / entry, so 3 entries per block. Yields 17
568564
*/
569565
/* blocks. */
570566
n = reftable_writer_add_ref(w, &ref);
571567
EXPECT(n == 0);
572568

573-
if (!memcmp(hash1, want_hash, GIT_SHA1_RAWSZ) ||
574-
!memcmp(hash2, want_hash, GIT_SHA1_RAWSZ)) {
569+
if (!memcmp(ref.value.val2.value, want_hash, GIT_SHA1_RAWSZ) ||
570+
!memcmp(ref.value.val2.target_value, want_hash, GIT_SHA1_RAWSZ)) {
575571
want_names[want_names_len++] = xstrdup(name);
576572
}
577573
}

reftable/record.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,7 @@ static void reftable_ref_record_copy_from(void *rec, const void *src_rec,
222222
memcpy(ref->value.val1, src->value.val1, hash_size);
223223
break;
224224
case REFTABLE_REF_VAL2:
225-
ref->value.val2.value = reftable_malloc(hash_size);
226225
memcpy(ref->value.val2.value, src->value.val2.value, hash_size);
227-
ref->value.val2.target_value = reftable_malloc(hash_size);
228226
memcpy(ref->value.val2.target_value,
229227
src->value.val2.target_value, hash_size);
230228
break;
@@ -298,8 +296,6 @@ void reftable_ref_record_release(struct reftable_ref_record *ref)
298296
reftable_free(ref->value.symref);
299297
break;
300298
case REFTABLE_REF_VAL2:
301-
reftable_free(ref->value.val2.target_value);
302-
reftable_free(ref->value.val2.value);
303299
break;
304300
case REFTABLE_REF_VAL1:
305301
break;
@@ -401,11 +397,9 @@ static int reftable_ref_record_decode(void *rec, struct strbuf key,
401397
return -1;
402398
}
403399

404-
r->value.val2.value = reftable_malloc(hash_size);
405400
memcpy(r->value.val2.value, in.buf, hash_size);
406401
string_view_consume(&in, hash_size);
407402

408-
r->value.val2.target_value = reftable_malloc(hash_size);
409403
memcpy(r->value.val2.target_value, in.buf, hash_size);
410404
string_view_consume(&in, hash_size);
411405
break;

reftable/record_test.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,7 @@ static void test_reftable_ref_record_roundtrip(void)
122122
set_hash(in.u.ref.value.val1, 1);
123123
break;
124124
case REFTABLE_REF_VAL2:
125-
in.u.ref.value.val2.value =
126-
reftable_malloc(GIT_SHA1_RAWSZ);
127125
set_hash(in.u.ref.value.val2.value, 1);
128-
in.u.ref.value.val2.target_value =
129-
reftable_malloc(GIT_SHA1_RAWSZ);
130126
set_hash(in.u.ref.value.val2.target_value, 2);
131127
break;
132128
case REFTABLE_REF_SYMREF:

reftable/reftable-record.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ struct reftable_ref_record {
4141
union {
4242
unsigned char val1[GIT_MAX_RAWSZ];
4343
struct {
44-
uint8_t *value; /* first value, malloced hash */
45-
uint8_t *target_value; /* second value, malloced hash */
44+
unsigned char value[GIT_MAX_RAWSZ]; /* first hash */
45+
unsigned char target_value[GIT_MAX_RAWSZ]; /* second hash */
4646
} val2;
4747
char *symref; /* referent, malloced 0-terminated string */
4848
} value;

0 commit comments

Comments
 (0)