Skip to content

core/chunked_vector: improve move construction and assignment - #326

Merged
travisdowns merged 2 commits into
redpanda-data:v26.3.xfrom
travisdowns:td-chunked-vector-move
Nov 4, 2026
Merged

travisdowns merged 2 commits into
redpanda-data:v26.3.xfrom
travisdowns:td-chunked-vector-move

Conversation

@travisdowns

@travisdowns travisdowns commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Improve chunked_vector move construction and assignment.

  • core/chunked_vector: move-construct members directly
  • tests/perf: add chunked_vector move benchmarks

Release build, clang-22, 1 core, per operation:

case before after
move construct, noinline, nontrivial T 4.16ns 2.06ns
std::vector<chunked_vector> realloc, nontrivial T 4.58ns 1.15ns
move assign, nontrivial T 3.24ns 1.80ns
.

The chunked_vector move constructor default-constructed the vector and then
ran the move assignment operator on it. When that operator is not inlined
(common, since it contains the loop that destroys the old contents), every
move construction pays for a call, a self-move check, a walk over the empty
destination fragments, and a full destructor of the moved-from source that
the compiler cannot prove is empty.

The first commit move-constructs each member directly and implements move
assignment as move-construct-and-swap. Both now inline, and the source's
destructor folds away after a move.

The second commit adds tests/perf/chunked_vector_perf.cc, which measures
move construction (inline loop, through a noinline helper, and as
std::vector<chunked_vector> reallocation) and move assignment, with int
and with an element that has an out-of-line destructor.

Tested: chunked_vector_test and chunked_vector_async_test pass in dev mode.

@travisdowns
travisdowns marked this pull request as ready for review November 4, 2026 14:08
@travisdowns
travisdowns requested review from StephanDollberg and ballard26 and a balanced review from Copilot November 4, 2026 14:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Construction paths invoke std::launder before a live object exists, causing undefined behavior, and move assignment lacks regression coverage for cleanup of nonempty destinations with nontrivial elements.

2 open issues
What changed in this PR

This PR reduces move-operation overhead in chunked_vector and adds benchmarks to measure the impact.

Changes:

  • Move-construct members directly and implement move assignment through move construction and swap.
  • Add benchmarks for move construction, move assignment, and outer-vector reallocation with trivial and nontrivial elements.
  • Register the new benchmark target.
File Description
tests/​perf/​CMakeLists.txt Registers the chunked_vector performance tests.
tests/​perf/​chunked_vector_perf.cc Adds move-operation and reallocation benchmarks.
include/​seastar/​core/​chunked_vector.hh Revises move operations and documents moved-from behavior.

Review effort: Balanced

Comment thread include/seastar/core/chunked_vector.hh Resolved
Comment thread tests/perf/chunked_vector_perf.cc Resolved
StephanDollberg
StephanDollberg previously approved these changes Nov 4, 2026
Move-construct each member, and implement move assignment as
move-construct-and-swap. This is much more efficient especially when
the elements have non-trivial destructors.

In a chunked_vector move benchmark (clang-22, release), per operation:

  move construct, out of line, nontrivial T:  4.16ns -> 2.06ns
  std::vector<chunked_vector> realloc:        4.58ns -> 1.15ns
  move assign, nontrivial T:                  3.24ns -> 1.80ns
Measures move construction and move assignment of chunked_vector, with
trivial (int) and nontrivial elements. Includes a case that uses
a noinline helper to avoid inlining adjacent moves into the loop.
@travisdowns
travisdowns enabled auto-merge (rebase) November 4, 2026 14:31
@travisdowns
travisdowns merged commit f3124bb into redpanda-data:v26.3.x Nov 4, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants