Skip to content

Commit

Permalink
Merge pull request #9904 from jcsp/issue-9375-trim-indices
Browse files Browse the repository at this point in the history
storage: shrink index vectors on flush
  • Loading branch information
dotnwat authored Apr 10, 2023
2 parents f672c22 + f5e6b57 commit 671984a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/v/storage/index_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ struct index_state
position_index[i]};
}

void shrink_to_fit() {
relative_offset_index.shrink_to_fit();
relative_time_index.shrink_to_fit();
position_index.shrink_to_fit();
}

std::optional<std::tuple<uint32_t, offset_time_index, uint64_t>>
find_entry(model::timestamp ts) {
const auto idx = offset_time_index{ts, with_offset};
Expand Down
6 changes: 6 additions & 0 deletions src/v/storage/segment_index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ ss::future<> segment_index::flush() {
}
_needs_persistence = false;
clear_cached_disk_usage();

// Flush is usually called when we either shrunk the index (truncate)
// or when we're no longer going to append (close): in either case,
// it is a good time to free speculatively allocated memory.
_state.shrink_to_fit();

return with_file(open(), [this](ss::file backing_file) {
return flush_to_file(std::move(backing_file));
});
Expand Down

0 comments on commit 671984a

Please sign in to comment.