]> git.hungrycats.org Git - bees/commitdiff
scan-next: elide the past-EOF tail's backing temp
authorZygo Blaxell <bees@furryterror.org>
Fri, 24 Jul 2026 11:53:48 +0000 (07:53 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 19:00:04 +0000 (15:00 -0400)
The executor backed the past-EOF unreachable tail with its own zero temp
so the tail hole carried an openable ref, because a btrfs EXTENT_DATA
num_bytes is sector-aligned and a dst ref therefore spans into the
past-EOF padding of its last block — sweep_join walked into the tail and
demanded a source, even though make_clipped_file_pair then dropped the
pair at the file's i_size.  The temp was pure scaffolding for a range
that produces no dedupe.

Make the tail genuinely ref-less from the executor's view instead: clamp
dst-ref enumeration to reachable_size (the end of the last non-unreachable
region = the mid-block EOF) in scan_next_build_tree_pairs, so the sweep
never enters the padding.  The tail's plan-tree hole is then left as a
bare HoleExtent — no backing temp — preserving the src_size == dst_lsize
span check as a ref-less no-op.  The clamp equals logical_size whenever
there is no trailing unreachable region, so the common path is unchanged;
it can never truncate a reachable region, since reachable_size is the max
end over all non-unreachable regions.

Also make the shared reachable hole temp lazy: an extent whose only hole
is the past-EOF tail now allocates no temp at all (the single all-data
block with a mid-block EOF), where before it built an unused reachable
temp.  Interior genuinely-ref-less regions were already free — no back
reference names them, so dst enumeration never emitted a ref there; this
brings the sector-aligned EOF tail into the same no-op category.

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

index 5839c44f0c449ed9eee7e661a5b47c9641a7d8d7..02139ee2ef17db05032184b4da9c056b7b654abf 100644 (file)
@@ -887,20 +887,32 @@ scan_next_sweep_join_pairs(BeesContext &ctx,
 
 vector<BeesRangePair>
 scan_next_build_tree_pairs(BeesContext &ctx, const BeesExtent &prepared,
-       const BeesExtent &src_tree, optional<bool> want_prealloc = {})
+       const BeesExtent &src_tree, uint64_t reachable_size,
+       optional<bool> want_prealloc = {})
 {
        BEESNOTE("scan_next build_pairs " << to_hex(prepared.bytenr()) << (want_prealloc ? " prealloc" : " data"));
        BEESTRACE("scan_next build_pairs " << to_hex(prepared.bytenr()) << (want_prealloc ? " prealloc" : " data"));
        auto &layer = Borrower::current().layer();
        const auto lsize = prepared.logical_size(layer);
 
-       BEESTRACE("build dst scan_next_build_all_ref_slices, lsize = " << lsize << ", prepared extent = " << prepared);
+       // Enumerate dst refs only over the reachable extent.  A btrfs
+       // EXTENT_DATA num_bytes is sector-aligned, so a ref spans into the
+       // past-EOF padding of its last block even though no valid file data
+       // lives there; clamping to reachable_size (the end of the last
+       // non-unreachable region = the mid-block EOF) keeps the sweep-join off
+       // that padding, so the past-EOF tail carries no ref on either side and
+       // needs no backing temp (its plan-tree hole is left bare).
+       // reachable_size == lsize in the common case (no trailing unreachable
+       // region), leaving behavior unchanged.
+       const auto dst_span = min(reachable_size, lsize);
+       BEESTRACE("build dst scan_next_build_all_ref_slices, lsize = " << lsize
+               << ", dst_span = " << dst_span << ", prepared extent = " << prepared);
        // Build ALL dst ref slices from raw back-references so that
        // every file reference to the dst extent gets its own
        // FIDEDUPERANGE call.  The old path used refs() which calls
        // bees_visit_refs_impl and picks one "best" ref per position,
        // missing partial refs that overlap with a full ref.
-       const auto dst_refs = scan_next_build_all_ref_slices(ctx, prepared, lsize);
+       const auto dst_refs = scan_next_build_all_ref_slices(ctx, prepared, dst_span);
 
        BEESTRACE("sweep_join_pairs");
        return scan_next_sweep_join_pairs(ctx, dst_refs, src_tree, want_prealloc);
@@ -2756,12 +2768,12 @@ struct Planner {
 ///   EOF.  The backing hole temp is sized to reachable_size so the
 ///   EOF-tail hole's dedupe src ends at the temp's own EOF
 ///   (src_end == src_fsize).  Hole parts beyond reachable_size are the
-///   past-EOF remainder: they are backed by their own separate zero
-///   temps (so their refs stay openable for the executor) and produce
-///   no dedupe pair, since the dst has no ref past its own EOF — a
-///   covered no-op that keeps the rebuilt tree spanning the full dst
-///   extent.  Equals logical_size in the common case (no non-data
-///   mid-block EOF), giving the prior behavior.
+///   past-EOF remainder: they are left as bare HoleExtents (no backing
+///   temp), since the pair builder clamps dst-ref enumeration to
+///   reachable_size and never queries them — a ref-less no-op that keeps
+///   the rebuilt tree spanning the full dst extent.  Equals logical_size
+///   in the common case (no non-data mid-block EOF), giving the prior
+///   behavior.
 BeesExtent
 scan_next_back_holes_with_temp(BeesContext &ctx, const BeesExtent &plan_tree,
        uint64_t reachable_size, bool datacow)
@@ -2793,13 +2805,24 @@ scan_next_back_holes_with_temp(BeesContext &ctx, const BeesExtent &plan_tree,
                return scan_next_make_temp_hole_tree(ctx, logical_size, datacow);
        }
 
-       // Create a temp hole extent covering only the reachable extent.
-       const auto hole_temp = scan_next_make_temp_hole_tree(ctx, reachable_size, datacow);
+       // Shared temp hole covering the reachable extent, created lazily: an
+       // extent whose only hole is the past-EOF tail (kept bare below) then
+       // allocates no temp at all — the common shape for a single all-data
+       // block with a mid-block EOF.  Its end == reachable_size, so a hole
+       // whose m_end == reachable_size (the EOF-tail hole) dedupes with
+       // src_end == src_fsize.
+       optional<BeesExtent> hole_temp;
+       const auto reachable_hole_temp = [&]() -> const BeesExtent & {
+               if (!hole_temp) {
+                       hole_temp = scan_next_make_temp_hole_tree(ctx, reachable_size, datacow);
+               }
+               return *hole_temp;
+       };
 
        // The materialized tree is a SuperExtent whose parts are
        // action nodes (PostCopyExtent, DedupExtent, HoleExtent).
-       // Replace HoleExtent parts with slices of the temp hole file.
-       // Non-hole parts are preserved as-is.
+       // Replace reachable HoleExtent parts with slices of the temp hole
+       // file.  Non-hole parts and the past-EOF tail are preserved as-is.
        //
        // We can't use visit() or tree_walk here because they remap
        // child coordinates to the parent's space.  Instead, iterate
@@ -2815,35 +2838,22 @@ scan_next_back_holes_with_temp(BeesContext &ctx, const BeesExtent &plan_tree,
                        // Reachable hole — replace with a slice of the shared
                        // reachable hole temp.  m_begin/m_end are in parent
                        // space, which is where the hole sits in the temp file.
-                       // The temp file ends at reachable_size, so a hole whose
-                       // m_end == reachable_size (the EOF-tail hole) dedupes
-                       // with src_end == src_fsize.
                        parts.push_back(BeesTreeSuperPart {
-                               .m_child = BeesExtentSlice(hole_temp,
+                               .m_child = BeesExtentSlice(reachable_hole_temp(),
                                        part.m_begin, part.m_end),
                                .m_begin = part.m_begin,
                                .m_end = part.m_end,
                        });
                } else {
                        // Past-EOF hole beyond reachable_size (the unreachable
-                       // remainder of a non-data region that held the file's
-                       // mid-block EOF).  Back it with its own separate zero
-                       // temp rather than the shared reachable temp: the latter
-                       // ends at reachable_size, so slicing it here would run
-                       // past its end.  A dedicated temp keeps the hole's refs
-                       // openable (the executor opens every part's refs) while
-                       // leaving the shared temp's EOF exactly at reachable_size.
-                       // The dst has no ref past its own EOF, so this part
-                       // produces no dedupe pair — a covered no-op.
-                       const auto part_size = part.m_end - part.m_begin;
-                       const auto past_eof_temp =
-                               scan_next_make_temp_hole_tree(ctx, part_size, datacow);
-                       parts.push_back(BeesTreeSuperPart {
-                               .m_child = BeesExtentSlice(past_eof_temp,
-                                       0, part_size),
-                               .m_begin = part.m_begin,
-                               .m_end = part.m_end,
-                       });
+                       // remainder of a region that held the file's mid-block
+                       // EOF).  It carries no valid file data and the pair builder
+                       // clamps dst-ref enumeration to reachable_size, so this part
+                       // is never queried for a source and needs no backing temp.
+                       // Keep it as the bare HoleExtent so the rebuilt tree still
+                       // spans the full dst extent (the src_size == dst_lsize
+                       // check) — a ref-less no-op.
+                       parts.push_back(part);
                }
        }
 
@@ -2912,11 +2922,13 @@ scan_next_execute_dst_plan(const shared_ptr<BeesContext> &ctx,
        THROW_CHECK2(runtime_error, src_size, dst_lsize,
                src_size == dst_lsize);
 
-       // Build dedupe pairs: prealloc first, then data.
+       // Build dedupe pairs: prealloc first, then data.  Pass reachable_size
+       // so dst-ref enumeration stops at the mid-block EOF and never demands a
+       // source for the bare past-EOF tail hole.
        auto prealloc_pairs = scan_next_build_tree_pairs(*ctx, dst,
-               src_tree, true);
+               src_tree, reachable_size, true);
        auto data_pairs = scan_next_build_tree_pairs(*ctx, dst,
-               src_tree, false);
+               src_tree, reachable_size, false);
 
        BEESLOGC(DEBUG, Plan, "scan_next_execute_dst_plan dst "
                << to_hex(dst.bytenr())