reftable/stack: return stack segments directly

The `stack_table_sizes_for_compaction()` function returns individual sizes of each reftable table. This function is only called by `reftable_stack_auto_compact()` to decide which tables need to be compacted, if any. Modify the function to directly return the segments, which avoids the extra step of receiving the sizes only to pass it to `suggest_compaction_segment()`. A future commit will also add functionality for checking whether auto-compaction is necessary without performing it. This change allows code re-usability in that context. Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Acked-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Nov 8, 2025 at 22:51 UTC 135f491f83d4763bdc61642eb0126ce2e6ada286
1 file changed +12 -11
reftable/stack.c
+12 -11
@@ -1626,7 +1626,8 @@ struct segment suggest_compaction_segment(uint64_t *sizes, size_t n,
1626 return seg;
1627 }
1628
1629 -static uint64_t *stack_table_sizes_for_compaction(struct reftable_stack *st)
1629 +static int stack_segments_for_compaction(struct reftable_stack *st,
1630 + struct segment *seg)
1631 {
1632 int version = (st->opts.hash_id == REFTABLE_HASH_SHA1) ? 1 : 2;
1633 int overhead = header_size(version) - 1;
@@ -1634,29 +1635,29 @@ static uint64_t *stack_table_sizes_for_compaction(struct reftable_stack *st)
1635
1636 REFTABLE_CALLOC_ARRAY(sizes, st->merged->tables_len);
1637 if (!sizes)
1637 - return NULL;
1638 + return REFTABLE_OUT_OF_MEMORY_ERROR;
1639
1640 for (size_t i = 0; i < st->merged->tables_len; i++)
1641 sizes[i] = st->tables[i]->size - overhead;
1642
1642 - return sizes;
1643 + *seg = suggest_compaction_segment(sizes, st->merged->tables_len,
1644 + st->opts.auto_compaction_factor);
1645 + reftable_free(sizes);
1646 +
1647 + return 0;
1648 }
1649
1650 int reftable_stack_auto_compact(struct reftable_stack *st)
1651 {
1652 struct segment seg;
1648 - uint64_t *sizes;
1653 + int err;
1654
1655 if (st->merged->tables_len < 2)
1656 return 0;
1657
1653 - sizes = stack_table_sizes_for_compaction(st);
1654 - if (!sizes)
1655 - return REFTABLE_OUT_OF_MEMORY_ERROR;
1656 -
1657 - seg = suggest_compaction_segment(sizes, st->merged->tables_len,
1658 - st->opts.auto_compaction_factor);
1659 - reftable_free(sizes);
1658 + err = stack_segments_for_compaction(st, &seg);
1659 + if (err)
1660 + return err;
1661
1662 if (segment_size(&seg) > 0)
1663 return stack_compact_range(st, seg.start, seg.end - 1,