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
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
[](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
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.
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;
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(
/// 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(
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);