From 0dd36a73e492159221293bd63fbe46bded43d0d1 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Fri, 8 Apr 2022 15:59:59 +0300 Subject: [PATCH] lib-index: Always write tail offset the same as head offset The mail_index_write() must not be called unless this is safe to do. This prevents unnecessarily reading through dovecot.index.log between tail..head offsets, which can be expensive due to modseq calculation. --- src/lib-index/mail-index-write.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/lib-index/mail-index-write.c b/src/lib-index/mail-index-write.c index 9fb5784480..689cb9cdc1 100644 --- a/src/lib-index/mail-index-write.c +++ b/src/lib-index/mail-index-write.c @@ -78,12 +78,18 @@ static int mail_index_recreate(struct mail_index *index) output = o_stream_create_fd_file(fd, 0, FALSE); o_stream_cork(output); - base_size = I_MIN(map->hdr.base_header_size, sizeof(map->hdr)); - o_stream_nsend(output, &map->hdr, base_size); + struct mail_index_header hdr = map->hdr; + /* Write tail_offset the same as head_offset. This function must not + be called unless it's safe to do this. See the explanations in + mail_index_sync_commit(). */ + hdr.log_file_tail_offset = hdr.log_file_head_offset; + + base_size = I_MIN(hdr.base_header_size, sizeof(hdr)); + o_stream_nsend(output, &hdr, base_size); o_stream_nsend(output, MAIL_INDEX_MAP_HDR_OFFSET(map, base_size), - map->hdr.header_size - base_size); + hdr.header_size - base_size); o_stream_nsend(output, map->rec_map->records, - map->rec_map->records_count * map->hdr.record_size); + map->rec_map->records_count * hdr.record_size); if (o_stream_finish(output) < 0) { mail_index_file_set_syscall_error(index, path, "write()"); ret = -1; -- 2.47.3