fill: review-driven cleanup, fix MapSelected unsorted-selection bug, ~22% faster hot path#3762
Merged
jqnatividad merged 1 commit intomasterfrom Apr 27, 2026
Merged
fill: review-driven cleanup, fix MapSelected unsorted-selection bug, ~22% faster hot path#3762jqnatividad merged 1 commit intomasterfrom
jqnatividad merged 1 commit intomasterfrom
Conversation
…~22% faster hot path Correctness - MapSelected assumed ascending, deduped column indices; user-supplied selections like "3,1" or "1,1" silently dropped columns. Sort + dedup in the constructor and add regression tests. - Document that --default takes precedence over --first / forward-fill. - Add // safety: comment to the final-buffer-drain unwrap. Style / small perf - GroupValues::fill: as_ref().or_else().cloned().unwrap_or(field) avoids cloning the default value bytes for every empty field. - Hoist default_value clone into or_insert_with so it only runs on group insertion, not every row. - Replace `row[i] == b""` with is_empty(). Hot-path perf (~22% faster on the common no-backfill path) - Add fill_into() which writes filled rows directly into a reusable csv::ByteRecord using an O(1) HashSet<usize> for selected-column membership, eliminating two Vec<Vec<u8>> allocations per row. - Use get_mut() in the grouper lookup so key.clone() and the default value clone only happen on insertion of a new group, not every row. - Hoist the (default, first) match into a MemorizeKind enum computed once before the loop instead of per-row. Benchmarks on NYC_311_SR_2010-2020-sample-1M.csv (1M rows, 514MB, 3 selected cols with high empty rates), 5 runs each, hyperfine: forward fill: 1.829s -> 1.429s (1.28x faster) groupby fill: 1.870s -> 1.456s (1.28x faster) default fill: 1.758s -> 1.372s (1.28x faster) backfill: 1.815s -> 1.837s (within noise; slow path unchanged) Output bit-identical to baseline (md5 verified for forward + groupby). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 7 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves the fill command’s correctness, clarifies option precedence in the help text, and optimizes the non---backfill hot path by avoiding per-row heap allocations.
Changes:
- Fixes a correctness bug where
MapSelectedcould mis-handle unsorted or duplicate selection indices by sorting + deduping at construction time (with regression tests). - Documents that
--defaulttakes precedence over forward-fill /--first(making them no-ops when default is set). - Optimizes the no-
--backfillpath by filling directly into a reusablecsv::ByteRecordand reducing cloning on group insertion.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/cmd/fill.rs |
Fixes selection-order/dup handling, adds fill_into() fast path, and updates USAGE to document --default precedence. |
tests/test_fill.rs |
Adds regression tests for unsorted/duplicate selection and --default precedence over --first. |
docs/help/fill.md |
Regenerates help text to reflect --default precedence. |
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.
Summary
MapSelectedpreviously assumed selection indices were sorted and unique, so user-supplied selections likeqsv fill -- "3,1" file.csvorqsv fill -- "1,1" file.csvsilently dropped columns from the fill. Sort + dedup at construction; added regression tests.--defaulttakes precedence over--first/ forward-fill (previously silent).csv::ByteRecordvia a newfill_into()method, eliminating twoVec<Vec<u8>>allocations per row on the common no---backfillpath. Grouper lookup usesget_mut()sokey.clone()and the default-value clone only happen on insertion of a new group.// safety:comment on the final-buffer-drainunwrap(),as_ref().or_else().cloned()inGroupValues::fill, hoisteddefault_valueclone intoor_insert_with,is_empty()instead of== b"", hoisted the(default, first)match into aMemorizeKindenum computed once outside the loop.Benchmark
NYC_311_SR_2010-2020-sample-1M.csv(1M rows, 514 MB, 3 selected columns with high empty rates), 5 runs each viahyperfine:--groupbyfill--defaultfill--backfillOutput is bit-identical to baseline (md5 verified for forward + groupby modes on the full 1M-row file).
Test plan
cargo t fill -F all_features)fill_forward_unsorted_selection— coversMapSelectedordering bugfill_forward_duplicate_selection— covers dedup behaviorfill_default_overrides_first— locks in the documented precedencecargo +nightly clippy -F all_features -- -W clippy::perf— cleancargo +nightly fmt— applieddocs/help/fill.mdregenerated viaqsv --generate-help-md🤖 Generated with Claude Code