diff mbox series

[05/10] t-reftable-block: use block_iter_reset() instead of block_iter_close()

Message ID 20240814121122.4642-6-chandrapratap3519@gmail.com (mailing list archive)
State Superseded
Headers show
Series t: port reftable/block_test.c to the unit testing framework | expand

Commit Message

Chandra Pratap Aug. 14, 2024, 12:03 p.m. UTC
block_iter_reset() restores a block iterator to its state at the time
of initialization without freeing any memory while block_iter_close()
deallocates the memory for the iterator.

In the current testing setup, a block iterator is allocated and
deallocated for every iteration of a loop, which hurts performance.
Improve upon this by using block_iter_reset() at the start of each
iteration instead. This has the added benifit of testing
block_iter_reset(), which currently remains untested.

Similarly, remove reftable_record_release() for a reftable record
that is still in use.

Mentored-by: Patrick Steinhardt <ps@pks.im>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
---
 t/unit-tests/t-reftable-block.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

Comments

Patrick Steinhardt Aug. 15, 2024, 9:40 a.m. UTC | #1
On Wed, Aug 14, 2024 at 05:33:13PM +0530, Chandra Pratap wrote:
> block_iter_reset() restores a block iterator to its state at the time
> of initialization without freeing any memory while block_iter_close()
> deallocates the memory for the iterator.
> 
> In the current testing setup, a block iterator is allocated and
> deallocated for every iteration of a loop, which hurts performance.
> Improve upon this by using block_iter_reset() at the start of each
> iteration instead. This has the added benifit of testing
> block_iter_reset(), which currently remains untested.

I don't think that performance is a good argument, but exercising the
reset function certainly is.

> Similarly, remove reftable_record_release() for a reftable record
> that is still in use.

This is a welcome change, too, to verify that reading into the same
record multiple times does not leak memory and otherwise works as
expected.

Patrick
diff mbox series

Patch

diff --git a/t/unit-tests/t-reftable-block.c b/t/unit-tests/t-reftable-block.c
index 0d73fb98d6..dfb7262a65 100644
--- a/t/unit-tests/t-reftable-block.c
+++ b/t/unit-tests/t-reftable-block.c
@@ -76,11 +76,8 @@  static void t_block_read_write(void)
 		j++;
 	}
 
-	reftable_record_release(&rec);
-	block_iter_close(&it);
-
 	for (i = 0; i < N; i++) {
-		struct block_iter it = BLOCK_ITER_INIT;
+		block_iter_reset(&it);
 		reftable_record_key(&recs[i], &want);
 
 		n = block_iter_seek_key(&it, &br, &want);
@@ -98,11 +95,10 @@  static void t_block_read_write(void)
 		n = block_iter_next(&it, &rec);
 		check_int(n, ==, 0);
 		check(reftable_record_equal(&recs[10 * (i / 10)], &rec, GIT_SHA1_RAWSZ));
-
-		block_iter_close(&it);
 	}
 
 	block_reader_release(&br);
+	block_iter_close(&it);
 	reftable_record_release(&rec);
 	reftable_block_done(&br.block);
 	strbuf_release(&want);