Skip to content

Commit 9a59b65

Browse files
pks-tgitster
authored andcommitted
reftable/reader: avoid copying index iterator
When doing an indexed seek we need to walk down the multi-level index until we finally hit a record of the desired indexed type. This loop performs a copy of the index iterator on every iteration, which is both hard to understand and completely unnecessary. Refactor the code so that we use a single iterator to walk down the indices, only. Note that while this should improve performance, the improvement is negligible in all but the most unreasonable repositories. This is because the effect is only really noticeable when we have to walk down many levels of indices, which is not something that a repository would typically have. So the motivation for this change is really only about readability. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d537ce6 commit 9a59b65

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

reftable/reader.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -510,13 +510,11 @@ static int reader_seek_indexed(struct reftable_reader *r,
510510
.type = BLOCK_TYPE_INDEX,
511511
.u.idx = { .last_key = STRBUF_INIT },
512512
};
513-
struct table_iter index_iter = TABLE_ITER_INIT;
514-
struct table_iter empty = TABLE_ITER_INIT;
515-
struct table_iter next = TABLE_ITER_INIT;
513+
struct table_iter ti = TABLE_ITER_INIT, *malloced;
516514
int err = 0;
517515

518516
reftable_record_key(rec, &want_index.u.idx.last_key);
519-
err = reader_start(r, &index_iter, reftable_record_type(rec), 1);
517+
err = reader_start(r, &ti, reftable_record_type(rec), 1);
520518
if (err < 0)
521519
goto done;
522520

@@ -526,7 +524,7 @@ static int reader_seek_indexed(struct reftable_reader *r,
526524
* highest layer that identifies the relevant index block as well as
527525
* the record inside that block that corresponds to our wanted key.
528526
*/
529-
err = reader_seek_linear(&index_iter, &want_index);
527+
err = reader_seek_linear(&ti, &want_index);
530528
if (err < 0)
531529
goto done;
532530

@@ -552,44 +550,36 @@ static int reader_seek_indexed(struct reftable_reader *r,
552550
* all levels of the index only to find out that the key does
553551
* not exist.
554552
*/
555-
err = table_iter_next(&index_iter, &index_result);
553+
err = table_iter_next(&ti, &index_result);
556554
if (err != 0)
557555
goto done;
558556

559-
err = reader_table_iter_at(r, &next, index_result.u.idx.offset,
560-
0);
557+
err = reader_table_iter_at(r, &ti, index_result.u.idx.offset, 0);
561558
if (err != 0)
562559
goto done;
563560

564-
err = block_iter_seek_key(&next.bi, &next.br, &want_index.u.idx.last_key);
561+
err = block_iter_seek_key(&ti.bi, &ti.br, &want_index.u.idx.last_key);
565562
if (err < 0)
566563
goto done;
567564

568-
if (next.typ == reftable_record_type(rec)) {
565+
if (ti.typ == reftable_record_type(rec)) {
569566
err = 0;
570567
break;
571568
}
572569

573-
if (next.typ != BLOCK_TYPE_INDEX) {
570+
if (ti.typ != BLOCK_TYPE_INDEX) {
574571
err = REFTABLE_FORMAT_ERROR;
575-
break;
572+
goto done;
576573
}
577-
578-
table_iter_close(&index_iter);
579-
index_iter = next;
580-
next = empty;
581574
}
582575

583-
if (err == 0) {
584-
struct table_iter *malloced = reftable_calloc(1, sizeof(*malloced));
585-
*malloced = next;
586-
next = empty;
587-
iterator_from_table_iter(it, malloced);
588-
}
576+
REFTABLE_ALLOC_ARRAY(malloced, 1);
577+
*malloced = ti;
578+
iterator_from_table_iter(it, malloced);
589579

590580
done:
591-
table_iter_close(&next);
592-
table_iter_close(&index_iter);
581+
if (err)
582+
table_iter_close(&ti);
593583
reftable_record_release(&want_index);
594584
reftable_record_release(&index_result);
595585
return err;

0 commit comments

Comments
 (0)