From 4278d0cc0631edf7fdfeff706b2a234665b4e760 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 16:25:44 +0000 Subject: [PATCH] dt64: LaneBinding::borrow_all, one check for every bound lane LaneBinding::lane re-checks every bound lane on each call, so reading all N lanes cost N^2 source lookups (155 us at 256 fields in #1987's lab, against 14 us for one check). borrow_all checks once and returns BoundLanes, an ExactSizeIterator over the bound lanes in ascending field order. Refusals return before any borrow; a vanished lane is reported at its position. No allocation. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XAqKMMWgnUU9g7Ldyn6sbk --- .../2026-11-10-dt64-lane-borrow-all.md | 25 +++++ .claude/board/entries/README.md | 3 +- crates/lance-graph-dt64/src/lane.rs | 56 +++++++++++ crates/lance-graph-dt64/src/lib.rs | 4 +- crates/lance-graph-dt64/tests/lane_socket.rs | 97 +++++++++++++++++++ 5 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 .claude/board/entries/2026-11-10-dt64-lane-borrow-all.md diff --git a/.claude/board/entries/2026-11-10-dt64-lane-borrow-all.md b/.claude/board/entries/2026-11-10-dt64-lane-borrow-all.md new file mode 100644 index 000000000..3badbb66f --- /dev/null +++ b/.claude/board/entries/2026-11-10-dt64-lane-borrow-all.md @@ -0,0 +1,25 @@ +# 2026-11-10 — `LaneBinding::borrow_all`: one check, N borrows + +**Status:** SHIPPED in this PR (`crates/lance-graph-dt64/src/lane.rs`). + +**Why.** `LaneBinding::lane` re-runs `check` on every call, and `check` +walks every bound lane. Reading all N bound lanes therefore cost N² source +lookups. D-DT64-GEMM-SHAPE-2 (#1987) measured this at 256 fields: 155 µs +per resolve through `lane`, against 14 µs for a single check followed by the +borrows. + +**What.** `borrow_all` runs `check` once and returns `BoundLanes`, an +`ExactSizeIterator` that borrows each bound lane in ascending field order. +- A refusal is returned before any lane is borrowed. +- A lane the source no longer holds yields `Err(Vanished { field })` at its + position, and the remaining lanes are still handed out. +- Nothing is allocated. + +**Tests** (`tests/lane_socket.rs`): +- lanes are borrowed in place and in field order, and match `lane`; +- N lookups against N² for `lane` per field; +- a refused check borrows nothing; +- a vanished lane is reported in place. + +**Open:** #1987's lab still uses `check` + `addr` + `borrow` by hand and can +switch to `borrow_all` once this lands. diff --git a/.claude/board/entries/README.md b/.claude/board/entries/README.md index 180b28810..4032f4ffc 100644 --- a/.claude/board/entries/README.md +++ b/.claude/board/entries/README.md @@ -25,10 +25,11 @@ index row, (3) no duplicate entry id. Checks 1 and 2 are deliberately opposite directions; the stranding this convention prevents shows up in exactly one of them, never both. -587 entries, 2026-08-06 .. 2026-11-08. +588 entries, 2026-08-06 .. 2026-11-10. | date | entry id | finding | file | |---|---|---|---| +| 2026-11-10 | `dt64-lane-borrow-all` | | [2026-11-10-dt64-lane-borrow-all.md](2026-11-10-dt64-lane-borrow-all.md) | | 2026-11-08 | `dr-isa-stream-lab` | | [2026-11-08-dr-isa-stream-lab.md](2026-11-08-dr-isa-stream-lab.md) | | 2026-11-07 | `D-DT64-7-L5b` | | [2026-11-07-dt64-l5b-historical-basis.md](2026-11-07-dt64-l5b-historical-basis.md) | | 2026-11-07 | `D-DT64-7-L5a` | | [2026-11-07-dt64-l5a-exact-basis-witness.md](2026-11-07-dt64-l5a-exact-basis-witness.md) | diff --git a/crates/lance-graph-dt64/src/lane.rs b/crates/lance-graph-dt64/src/lane.rs index 8ea2618b5..a0aa6e665 100644 --- a/crates/lance-graph-dt64/src/lane.rs +++ b/crates/lance-graph-dt64/src/lane.rs @@ -361,4 +361,60 @@ impl LaneBinding { let addr = self.addr(field).ok_or(LaneRefusal::NotBound { field })?; source.borrow(addr).ok_or(LaneRefusal::Vanished { field }) } + + /// [`LaneBinding::check`] once, then every bound lane, in ascending field + /// order. + /// + /// Calling [`LaneBinding::lane`] per field re-runs the whole check on + /// each call, and the check walks every bound lane, so reading all N + /// lanes that way does N² source lookups. This does N. Because the one + /// check covers every bound lane, any refusal (frame, route, basis, + /// mapping, scope, row order) is returned before a single lane is + /// borrowed. A lane the source no longer holds yields + /// `Err(Vanished { field })` at its position; the iterator does not stop + /// there. Nothing is allocated. + /// + /// The returned lanes are as current as the check: like `lane`, this + /// holds no lock on the source. + pub fn borrow_all<'b, 's, V: ClassView + ?Sized, S: LaneSource>( + &'b self, + store: &BindStore, + view: &V, + frames: &FrameStore, + source: &'s S, + ) -> Result, LaneRefusal> { + self.check(store, view, frames, source)?; + Ok(BoundLanes { + lanes: self.lanes.iter(), + source, + }) + } } + +/// Every lane of a checked [`LaneBinding`], borrowed one at a time from the +/// source, in ascending field order. Made by [`LaneBinding::borrow_all`]. +pub struct BoundLanes<'b, 's, S: LaneSource> { + lanes: core::slice::Iter<'b, (u8, LaneAddr)>, + source: &'s S, +} + +impl<'s, S: LaneSource> Iterator for BoundLanes<'_, 's, S> { + type Item = Result<(u8, S::Lane<'s>), LaneRefusal>; + + fn next(&mut self) -> Option { + let (field, addr) = self.lanes.next()?; + let field = *field; + Some( + self.source + .borrow(addr) + .map(|lane| (field, lane)) + .ok_or(LaneRefusal::Vanished { field }), + ) + } + + fn size_hint(&self) -> (usize, Option) { + self.lanes.size_hint() + } +} + +impl ExactSizeIterator for BoundLanes<'_, '_, S> {} diff --git a/crates/lance-graph-dt64/src/lib.rs b/crates/lance-graph-dt64/src/lib.rs index ff55f9d39..91a0ec63f 100644 --- a/crates/lance-graph-dt64/src/lib.rs +++ b/crates/lance-graph-dt64/src/lib.rs @@ -104,7 +104,9 @@ pub use dr::{ }; pub mod lane; use dr::{RouteLog, RouteRefusal}; -pub use lane::{CoordSpace, LaneAddr, LaneBinding, LaneRefusal, LaneRequest, LaneSource, RouteAt}; +pub use lane::{ + BoundLanes, CoordSpace, LaneAddr, LaneBinding, LaneRefusal, LaneRequest, LaneSource, RouteAt, +}; /// A method's identity within its class, independent of its source name. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] diff --git a/crates/lance-graph-dt64/tests/lane_socket.rs b/crates/lance-graph-dt64/tests/lane_socket.rs index 73a5de71d..4a22ad8a0 100644 --- a/crates/lance-graph-dt64/tests/lane_socket.rs +++ b/crates/lance-graph-dt64/tests/lane_socket.rs @@ -1,6 +1,8 @@ //! D-LANESOCK-1: the property-to-lane socket. Every refusal fires on a //! realistic input, and a binding that passes borrows the source's own lane. +use std::cell::Cell; + use lance_graph_contract::class_view::{ClassId, ClassView, WideFieldMask}; use lance_graph_contract::ontology::{DisplayTemplate, FieldRef}; use lance_graph_dt64::dr::{Frame, FrameRefusal, FrameRevisions, FrameStore, Scope}; @@ -60,6 +62,9 @@ struct Source { /// The store this source's data lives with (it never takes one from a /// caller; see `LaneSource::frames`). frames: FrameStore, + /// `lane_of` calls, and `borrow` calls, so far. + lookups: Cell, + borrows: Cell, } impl Source { @@ -77,6 +82,8 @@ impl Source { relation_map: 1, coordinates: 1, }), + lookups: Cell::new(0), + borrows: Cell::new(0), } } } @@ -91,12 +98,14 @@ impl LaneSource for Source { _basis: &FieldRef, _frame: &Frame, ) -> Option { + self.lookups.set(self.lookups.get() + 1); if class != HYBRID || rev != SchemaRev(1) { return None; } self.map.iter().find(|(f, _)| *f == field).map(|(_, a)| *a) } fn borrow(&self, addr: &LaneAddr) -> Option<&[i32]> { + self.borrows.set(self.borrows.get() + 1); self.lanes.get(usize::from(addr.lane)).map(Vec::as_slice) } fn frames(&self) -> &FrameStore { @@ -415,3 +424,91 @@ fn finding_the_hot_check_does_not_see_a_view_reorder_under_a_positional_source() .check(&w.store, &swapped, &w.source.frames, &w.source) .is_ok()); } + +/// `borrow_all` hands out the source's own lanes, in field order, the same +/// ones `lane` hands out one at a time. +#[test] +fn borrow_all_yields_every_bound_lane_in_place_in_field_order() { + let w = world(); + let b = bind(&w, &WideFieldMask::from_positions(&[1, 0])).unwrap(); + let all = b + .borrow_all(&w.store, &w.view, &w.source.frames, &w.source) + .unwrap(); + assert_eq!(all.len(), 2); + let got: Vec<(u8, &[i32])> = all.map(Result::unwrap).collect(); + assert_eq!(got.iter().map(|(f, _)| *f).collect::>(), [0, 1]); + for (field, lane) in got { + assert!(std::ptr::eq( + lane, + w.source.lanes[usize::from(field)].as_slice() + )); + assert_eq!( + b.lane(&w.store, &w.view, &w.source.frames, &w.source, field), + Ok(lane) + ); + } +} + +/// One check for all lanes: N source lookups where `lane` per field does N². +#[test] +fn borrow_all_checks_once_where_lane_per_field_checks_each_time() { + let w = world(); + let b = bind(&w, &WideFieldMask::from_positions(&[0, 1])).unwrap(); + + w.source.lookups.set(0); + let n = b + .borrow_all(&w.store, &w.view, &w.source.frames, &w.source) + .unwrap() + .count(); + assert_eq!((n, w.source.lookups.get()), (2, 2)); + + w.source.lookups.set(0); + for f in [0, 1] { + b.lane(&w.store, &w.view, &w.source.frames, &w.source, f) + .unwrap(); + } + assert_eq!( + w.source.lookups.get(), + 4, + "lane per field re-checks every lane" + ); +} + +/// A refused check returns before any lane is borrowed. +#[test] +fn a_refused_borrow_all_borrows_nothing() { + let mut w = world(); + let b = bind(&w, &WideFieldMask::from_positions(&[0, 1])).unwrap(); + w.source.frames.advance(FrameRevisions { + source: 2, + relation_map: 1, + coordinates: 1, + }); + w.source.borrows.set(0); + assert_eq!( + b.borrow_all(&w.store, &w.view, &w.source.frames, &w.source) + .err(), + Some(LaneRefusal::Frame(FrameRefusal::SourceChanged)) + ); + assert_eq!(w.source.borrows.get(), 0); +} + +/// A lane the source no longer holds is reported at its position; the +/// other lanes are still handed out. +#[test] +fn a_vanished_lane_is_reported_in_place() { + let mut w = world(); + let b = bind(&w, &WideFieldMask::from_positions(&[0, 1])).unwrap(); + w.source.lanes.truncate(1); + let got: Vec<_> = b + .borrow_all(&w.store, &w.view, &w.source.frames, &w.source) + .unwrap() + .collect(); + assert_eq!( + got, + vec![ + Ok((0, w.source.lanes[0].as_slice())), + Err(LaneRefusal::Vanished { field: 1 }) + ] + ); +}