Skip to content

Commit

Permalink
storage: shrink index vectors on flush
Browse files Browse the repository at this point in the history
This saves up to 24kib of memory per index, where
a segment is closed with very few entries in its index.

Related: redpanda-data#9375
  • Loading branch information
jcsp authored and ballard26 committed May 8, 2023
1 parent 946a645 commit 55d74a1
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 55d74a1

Please sign in to comment.