Reuse DatumVec allocation in metrics history truncation#36990
Open
antiguru wants to merge 1 commit into
Open
Conversation
partially_truncate_metrics_history decoded each snapshot row with Row::unpack, allocating a fresh Vec<Datum> per row in the hot truncation loop. Use a re-used DatumVec and borrow_with instead, which returns the allocation to the vector on drop and reuses it across rows.
5 tasks
antiguru
added a commit
that referenced
this pull request
Jun 11, 2026
…6991) ### Motivation The `handle_row` closure in `partially_truncate_status_history` decoded each snapshot row with `Row::unpack`, which allocates a fresh `Vec<Datum>` on every call. This runs in the hot per-row truncation loop over the status history shard. This is a sibling change to the metrics-history truncation PR (#36990); same pattern, different call site. ### Tips for reviewer The closure now captures a re-used `DatumVec` and decodes rows via `borrow_with`, which returns its allocation to the vector on drop and reuses it across rows. `extract_key`/`extract_time` take `&[Datum]`, and `&DatumVecBorrow` deref-coerces to `&[Datum]`, so the call sites are unchanged. The closure was already `mut`/`FnMut` (it mutates `latest_row_per_key`), so capturing the `DatumVec` by value and borrowing it mutably is fine. ### Checklist - [ ] This PR has adequate test coverage / QA involvement has been duly considered. ([trigger-ci for additional test/nightly runs](https://trigger-ci.dev.materialize.com/)) - [ ] This PR has an associated up-to-date [design doc](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/design/README.md), is a design doc ([template](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/design/00000000_template.md)), or is sufficiently small to not require a design. - [ ] If this PR evolves [an existing `$T ⇔ Proto$T` mapping](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/command-and-response-binary-encoding.md) (possibly in a backwards-incompatible way), then it is tagged with a `T-proto` label. - [ ] If this PR will require changes to cloud orchestration or tests, there is a companion cloud PR to account for those changes that is tagged with the release-blocker label ([example](MaterializeInc/cloud#5021)). - [ ] If this PR includes major [user-facing behavior changes](https://github.com/MaterializeInc/materialize/blob/main/doc/developer/guide-changes.md#what-changes-require-a-release-note), I have pinged the relevant PM to schedule a changelog entry. --- _Generated by [Claude Code](https://claude.ai/code/session_01Do1ud8hPYH5Pk3iM36AkEY)_ Co-authored-by: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Optimize memory allocation in the
partially_truncate_metrics_historyfunction by reusing a singleDatumVecallocation across multiple row unpacking operations instead of allocating a fresh vector for each row.Description
This change introduces a performance optimization in the metrics history truncation logic. Previously, each call to
row.unpack()would allocate a new vector to hold the unpacked datums. By reusing a singleDatumVecallocation across the loop, we avoid repeated allocations and deallocations.The change:
DatumVecto the imports frommz_reprDatumVecinstance before the looprow.unpack()withdatum_vec.borrow_with(row)to reuse the allocationThis is a straightforward refactoring that maintains identical behavior while reducing memory pressure during metrics history truncation operations.
Verification
This is a performance optimization with no functional changes. The behavior remains identical — we're just reusing an allocation. Existing tests for metrics history truncation will continue to pass.
https://claude.ai/code/session_01Do1ud8hPYH5Pk3iM36AkEY