]> git.hungrycats.org Git - bees/commitdiff
scan-next: compose the plan tree from the searched edges
authorZygo Blaxell <bees@furryterror.org>
Wed, 2 Sep 2026 16:57:25 +0000 (12:57 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 19:00:06 +0000 (15:00 -0400)
Variant "edges" of the compose/search reconciliation comparison.
Carry each selected edge's used dst sub-span through ChosenPlan
(m_selected_spans, index-aligned with m_selected_matches) and hand
both to scan_next_compose_plan_tree, which now walks the edge spans
instead of re-deriving slices from the matches' full dst spans.  The
edges say exactly which dst bytes each match serves, so the composer
emits the slices the search validated verbatim — closing the DP-slice
vs compose-emission mismatch that fired the once-only compose trap on
dst-overlapping selected matches.  The compose-time trap stays as the
final invariant check; it should now never fire.

The match-alone sort in scan_next_try_build_dst_plan is removed: it
would break the match/span index alignment, and the selector already
returns both sorted by span begin (joint permutation sort).

Assisted-by: Claude-Code:claude-fable-5
Assisted-by: Claude-Code:claude-opus-5-5
src/bees-scan-next.cc

index 10e673f17bafdf36778c6d7c1a76c5d342b04ca6..55689bb137def4140b0aa4f8e4548a855baae2d4 100644 (file)
@@ -175,6 +175,11 @@ struct RegionSlice {
 struct ChosenPlan {
        vector<const CandidateGroup *> m_selected_groups;
        vector<ExtentMatch> m_selected_matches;
+       /// Dst sub-span each selected match's edge actually uses, aligned
+       /// index-for-index with m_selected_matches.  The search may use
+       /// disjoint sub-spans of dst-overlapping matches, so the composer
+       /// consumes these spans rather than the matches' full dst spans.
+       vector<PlanSearchRegion> m_selected_spans;
 
        /// Unclaimed gap slices in dst data regions.  Sorted by m_begin
        /// and non-overlapping.  Each emit splits the containing gap into
@@ -1199,6 +1204,8 @@ scan_next_choose_match_plan(const BeesExtent &dst,
                const auto &fm = flat[group_id];
                chosen.m_selected_groups.push_back(fm.m_group);
                chosen.m_selected_matches.push_back(*fm.m_match);
+               chosen.m_selected_spans.push_back(
+                       PlanSearchRegion{ edge.m_dst_begin, edge.m_dst_end });
        }
        chosen.m_matched_blocks = found->m_matched_blocks;
        // Replace the seeded full-data unclaimed gaps with the search's copy
@@ -1213,11 +1220,28 @@ scan_next_choose_match_plan(const BeesExtent &dst,
                [](const RegionSlice &a, const RegionSlice &b) {
                        return a.m_begin < b.m_begin;
                });
-       // Selected matches must be sorted by dst begin for the composer.
-       sort(chosen.m_selected_matches.begin(), chosen.m_selected_matches.end(),
-               [](const ExtentMatch &a, const ExtentMatch &b) {
-                       return a.m_dst_begin < b.m_dst_begin;
+       // The composer consumes edges in ascending used-span order.  Sort
+       // matches and spans together (index permutation) so the two stay
+       // aligned; the search emits them in forward order already, so this
+       // is defensive.
+       {
+               vector<size_t> perm(chosen.m_selected_matches.size());
+               for (size_t i = 0; i < perm.size(); ++i) perm[i] = i;
+               sort(perm.begin(), perm.end(), [&](size_t x, size_t y) {
+                       return chosen.m_selected_spans[x].m_begin
+                            < chosen.m_selected_spans[y].m_begin;
                });
+               vector<ExtentMatch> sm;
+               vector<PlanSearchRegion> ss;
+               sm.reserve(perm.size());
+               ss.reserve(perm.size());
+               for (const size_t i : perm) {
+                       sm.push_back(chosen.m_selected_matches[i]);
+                       ss.push_back(chosen.m_selected_spans[i]);
+               }
+               chosen.m_selected_matches = std::move(sm);
+               chosen.m_selected_spans   = std::move(ss);
+       }
 
        // The search's debt is reconciliation-checked against the
        // reconstructed plan via scan_next_dst_debt at the call site; here log
@@ -1244,23 +1268,29 @@ scan_next_choose_match_plan(const BeesExtent &dst,
        return chosen;
 }
 
-/// Compose a plan tree for a dst extent given its block map and
-/// a sorted, non-overlapping list of match slices.
+/// Compose a plan tree for a dst extent given its block map and the
+/// covering's selected edges: matches plus the dst sub-span each edge
+/// uses (index-aligned, sorted by span begin, spans non-overlapping).
 ///
 /// Produces a SuperExtent whose children cover the dst's full
 /// logical range:
-/// - DedupExtent(SubExtent(src)) for matched data regions
+/// - DedupExtent(SubExtent(src)) for each edge's used span
 /// - PreCopyExtent(SubExtent(dst)) for unmatched data
 /// - HoleExtent for zero/prealloc/unreachable regions
 ///
-/// Matches must be sorted by m_dst_begin and non-overlapping.
+/// The edges say exactly which dst bytes each match serves — the
+/// composer emits them verbatim rather than re-deriving slices from
+/// the matches' (possibly dst-overlapping) full spans.
 BeesExtent
 scan_next_compose_plan_tree(
        const BeesExtent &dst,
-       const vector<ExtentMatch> &matches)
+       const vector<ExtentMatch> &matches,
+       const vector<PlanSearchRegion> &spans)
 {
+       THROW_CHECK2(invalid_argument, matches.size(), spans.size(),
+               matches.size() == spans.size());
        BeesSuperExtentBuilder builder;
-       size_t match_idx = 0;
+       size_t edge_idx = 0;
        // Once-only enforcement on what is actually emitted: each dedupe
        // child's src slice, per src extent, must not overlap a slice this
        // plan already emitted.
@@ -1276,24 +1306,24 @@ scan_next_compose_plan_tree(
                        continue;
                }
 
-               // Data region — split by match boundaries.
+               // Data region — split by edge boundaries.
                uint64_t pos = region.m_begin;
                while (pos < region.m_end) {
-                       // Advance past matches that end at or before pos.
-                       while (match_idx < matches.size() &&
-                              matches[match_idx].m_dst_end <= pos) {
-                               ++match_idx;
+                       // Advance past edges that end at or before pos.
+                       while (edge_idx < spans.size() &&
+                              spans[edge_idx].m_end <= pos) {
+                               ++edge_idx;
                        }
 
-                       if (match_idx < matches.size() &&
-                           matches[match_idx].m_dst_begin <= pos) {
-                               // pos is inside a match — emit DedupExtent.
-                               // The m_src_begin/m_src_end fields give us the
-                               // src range paired with [m_dst_begin, m_dst_end);
-                               // sub-slice m_src to the aligned portion we're
-                               // emitting right now, then wrap with DedupExtent.
-                               const auto &m = matches[match_idx];
-                               const auto match_end = min(m.m_dst_end, region.m_end);
+                       if (edge_idx < spans.size() &&
+                           spans[edge_idx].m_begin <= pos) {
+                               // pos is inside an edge — emit DedupExtent.
+                               // The match's src fields pair its full dst span
+                               // [m_dst_begin, m_dst_end) with src; sub-slice
+                               // src to the edge portion we're emitting now,
+                               // then wrap with DedupExtent.
+                               const auto &m = matches[edge_idx];
+                               const auto match_end = min(spans[edge_idx].m_end, region.m_end);
                                const auto match_len = match_end - pos;
                                const auto src_offset = pos - m.m_dst_begin;
                                const auto sub_begin = m.m_src_begin + src_offset;
@@ -1329,10 +1359,10 @@ scan_next_compose_plan_tree(
                                builder.append(BeesExtentSlice(child, 0, match_len));
                                pos = match_end;
                        } else {
-                               // Gap before next match (or no more matches)
+                               // Gap before next edge (or no more edges)
                                // — emit PreCopyExtent for unmatched data.
-                               const auto gap_end = (match_idx < matches.size())
-                                       ? min(matches[match_idx].m_dst_begin, region.m_end)
+                               const auto gap_end = (edge_idx < spans.size())
+                                       ? min(spans[edge_idx].m_begin, region.m_end)
                                        : region.m_end;
                                const auto gap_len = gap_end - pos;
                                const auto src_sub = BeesExtent::subextent(
@@ -1900,7 +1930,7 @@ struct ExtentPlan {
 /// Try to build a DstPlan for one dst extent given candidate sources.
 /// Returns nullopt if the plan is not viable (no matches, cost too
 /// high, or choose_match_plan rejects).
-/// Pipeline: choose_match_plan → sort → compose_plan_tree →
+/// Pipeline: choose_match_plan → compose_plan_tree →
 ///           should_execute → validate → plan_cost.
 optional<DstPlan>
 scan_next_try_build_dst_plan(
@@ -1917,18 +1947,16 @@ scan_next_try_build_dst_plan(
                return {};
        }
 
-       sort(chosen->m_selected_matches.begin(),
-               chosen->m_selected_matches.end(),
-               [](const auto &lhs, const auto &rhs) {
-                       return lhs.m_dst_begin < rhs.m_dst_begin;
-               });
+       // Matches and their used spans arrive index-aligned and sorted by
+       // span begin from scan_next_choose_match_plan; re-sorting matches
+       // alone here would break the alignment.
        // The once-only rule is enforced on the emitted dedupe slices inside
        // scan_next_compose_plan_tree: the search admits src-overlapping
        // *matches* so long as the *slices* a covering uses are disjoint, so
        // a whole-interval check on the selected matches would false-fire
        // here.
        const auto plan_tree = scan_next_compose_plan_tree(
-               dst, chosen->m_selected_matches);
+               dst, chosen->m_selected_matches, chosen->m_selected_spans);
 
        auto *layerp = ctx->layer().get();
        const auto cost = scan_next_plan_cost(layerp, plan_tree);