Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .claude/board/entries/2026-11-10-dt64-lane-borrow-all.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 1 deletion .claude/board/entries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

588 entries, 2026-08-06 .. 2026-11-10.
589 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-10 | `D-DT64-GEMM-SHAPE-1` | | [2026-11-10-dt64-gemm-shape.md](2026-11-10-dt64-gemm-shape.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) |
Expand Down
56 changes: 56 additions & 0 deletions crates/lance-graph-dt64/src/lane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BoundLanes<'b, 's, S>, 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<Self::Item> {
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<usize>) {
self.lanes.size_hint()
}
}

impl<S: LaneSource> ExactSizeIterator for BoundLanes<'_, '_, S> {}
4 changes: 3 additions & 1 deletion crates/lance-graph-dt64/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
97 changes: 97 additions & 0 deletions crates/lance-graph-dt64/tests/lane_socket.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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<usize>,
borrows: Cell<usize>,
}

impl Source {
Expand All @@ -77,6 +82,8 @@ impl Source {
relation_map: 1,
coordinates: 1,
}),
lookups: Cell::new(0),
borrows: Cell::new(0),
}
}
}
Expand All @@ -91,12 +98,14 @@ impl LaneSource for Source {
_basis: &FieldRef,
_frame: &Frame,
) -> Option<LaneAddr> {
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 {
Expand Down Expand Up @@ -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::<Vec<_>>(), [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 })
]
);
}
Loading