Skip to content

Commit c68ca7a

Browse files
pks-tgitster
authored andcommitted
reftable/reader: add comments to table_iter_next()
While working on the optimizations in the preceding patches I stumbled upon `table_iter_next()` multiple times. It is quite easy to miss the fact that we don't call `table_iter_next_in_block()` twice, but that the second call is in fact `table_iter_next_block()`. Add comments to explain what exactly is going on here to make things more obvious. While at it, touch up the code to conform to our code style better. Note that one of the refactorings merges two conditional blocks into one. Before, we had the following code: ``` err = table_iter_next_block(&next, ti); if (err != 0) { ti->is_finished = 1; } table_iter_block_done(ti); if (err != 0) { return err; } ``` As `table_iter_block_done()` does not care about `is_finished`, the conditional blocks can be merged into one block: ``` err = table_iter_next_block(&next, ti); table_iter_block_done(ti); if (err != 0) { ti->is_finished = 1; return err; } ``` This is both easier to reason about and more performant because we have one branch less. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a418a7a commit c68ca7a

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

reftable/reader.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -357,24 +357,32 @@ static int table_iter_next(struct table_iter *ti, struct reftable_record *rec)
357357

358358
while (1) {
359359
struct table_iter next = TABLE_ITER_INIT;
360-
int err = 0;
361-
if (ti->is_finished) {
360+
int err;
361+
362+
if (ti->is_finished)
362363
return 1;
363-
}
364364

365+
/*
366+
* Check whether the current block still has more records. If
367+
* so, return it. If the iterator returns positive then the
368+
* current block has been exhausted.
369+
*/
365370
err = table_iter_next_in_block(ti, rec);
366-
if (err <= 0) {
371+
if (err <= 0)
367372
return err;
368-
}
369373

374+
/*
375+
* Otherwise, we need to continue to the next block in the
376+
* table and retry. If there are no more blocks then the
377+
* iterator is drained.
378+
*/
370379
err = table_iter_next_block(&next, ti);
371-
if (err != 0) {
372-
ti->is_finished = 1;
373-
}
374380
table_iter_block_done(ti);
375-
if (err != 0) {
381+
if (err) {
382+
ti->is_finished = 1;
376383
return err;
377384
}
385+
378386
table_iter_copy_from(ti, &next);
379387
block_iter_close(&next.bi);
380388
}

0 commit comments

Comments
 (0)