feat: track borrowed name-level dependencies across read-only calls

This commit is contained in:
2026-10-24 01:39:49 +08:00
parent 89b817447e
commit a775d5c827
9 changed files with 221 additions and 42 deletions
+143 -20
View File
@@ -41,6 +41,8 @@ struct ReadOnly {
protected: BTreeSet<ObjectRef>,
reads: Mutex<BTreeSet<ObjectRef>>,
regions: Mutex<Vec<BTreeMap<ObjectRef, bool>>>,
dependencies: Mutex<BTreeMap<ReadKey, ReadDependency>>,
dependency_regions: Mutex<Vec<BTreeMap<ReadKey, ReadDependency>>>,
}
#[derive(Debug)]
@@ -63,6 +65,49 @@ pub struct ReadAccess {
pub content_read: bool,
}
/// Borrowed observations; Object handles never retain the observed graph.
/// Named values are immutable comparison snapshots, not managed owning edges.
#[derive(Debug, Clone, PartialEq)]
pub enum ReadDependency {
Object {
object: ObjectRef,
version: u64,
},
DictionaryKind {
object: ObjectRef,
},
Name {
object: ObjectRef,
name: String,
value: Option<crate::Value>,
},
}
impl ReadDependency {
pub fn object(&self) -> ObjectRef {
match self {
Self::Object { object, .. }
| Self::DictionaryKind { object }
| Self::Name { object, .. } => *object,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
enum ReadKey {
Object(ObjectRef),
DictionaryKind(ObjectRef),
Name(ObjectRef, String),
}
#[derive(Debug)]
pub struct TrackedReads {
pub accesses: Vec<ReadAccess>,
pub dependencies: Vec<ReadDependency>,
}
#[derive(Debug)]
pub struct ReadSet {
pub objects: Vec<ObjectRef>,
pub dependencies: Vec<ReadDependency>,
}
struct ValidationScope<'a> {
heap: &'a Heap,
thread: ThreadId,
@@ -128,23 +173,34 @@ impl Heap {
protected,
reads: Mutex::new(BTreeSet::new()),
regions: Mutex::new(Vec::new()),
dependencies: Mutex::new(BTreeMap::new()),
dependency_regions: Mutex::new(Vec::new()),
});
self.read_only.len()
}
pub(crate) fn end_read_only(&mut self, scope: usize) -> Result<Vec<ObjectRef>, VmError> {
Ok(self.end_read_only_precise(scope)?.objects)
}
pub(crate) fn end_read_only_precise(&mut self, scope: usize) -> Result<ReadSet, VmError> {
if scope == 0 || scope != self.read_only.len() {
return Err(VmError::InvalidArgument("read-only scope nesting mismatch"));
}
Ok(self
.read_only
.pop()
.unwrap()
.reads
.into_inner()
.expect("read tracker poisoned")
.into_iter()
.collect())
let scope = self.read_only.pop().unwrap();
Ok(ReadSet {
objects: scope
.reads
.into_inner()
.expect("read tracker poisoned")
.into_iter()
.collect(),
dependencies: scope
.dependencies
.into_inner()
.expect("read tracker poisoned")
.into_values()
.collect(),
})
}
pub(crate) fn begin_read_tracking(&self) -> Result<usize, VmError> {
@@ -153,6 +209,11 @@ impl Heap {
))?;
let mut regions = scope.regions.lock().expect("read tracker poisoned");
regions.push(BTreeMap::new());
scope
.dependency_regions
.lock()
.expect("read tracker poisoned")
.push(BTreeMap::new());
Ok(regions.len())
}
@@ -168,22 +229,32 @@ impl Heap {
&self,
token: usize,
) -> Result<Vec<ReadAccess>, VmError> {
Ok(self.end_read_tracking_precise(token)?.accesses)
}
pub(crate) fn end_read_tracking_precise(&self, token: usize) -> Result<TrackedReads, VmError> {
let scope = self.read_only.last().ok_or(VmError::InvalidArgument(
"read tracking requires a read-only scope",
))?;
let mut regions = scope.regions.lock().expect("read tracker poisoned");
if token == 0 || token != regions.len() {
let mut precise = scope
.dependency_regions
.lock()
.expect("read tracker poisoned");
if token == 0 || token != regions.len() || token != precise.len() {
return Err(VmError::InvalidArgument("read tracking nesting mismatch"));
}
Ok(regions
.pop()
.unwrap()
.into_iter()
.map(|(object, content_read)| ReadAccess {
object,
content_read,
})
.collect())
Ok(TrackedReads {
accesses: regions
.pop()
.unwrap()
.into_iter()
.map(|(object, content_read)| ReadAccess {
object,
content_read,
})
.collect(),
dependencies: precise.pop().unwrap().into_values().collect(),
})
}
pub(crate) fn is_read_only(&self) -> bool {
@@ -209,7 +280,45 @@ impl Heap {
compute()
}
pub(crate) fn record_read(&self, reference: ObjectRef) {
pub(crate) fn record_read(&self, reference: ObjectRef, version: u64) {
if self.read_only.is_empty() {
return;
}
self.record_observation(reference, ReadKey::Object(reference), || {
ReadDependency::Object {
object: reference,
version,
}
});
}
pub(crate) fn record_dictionary_kind(&self, reference: ObjectRef) {
self.record_observation(reference, ReadKey::DictionaryKind(reference), || {
ReadDependency::DictionaryKind { object: reference }
});
}
pub(crate) fn record_name(
&self,
reference: ObjectRef,
name: &str,
value: &Option<crate::Value>,
) {
if self.read_only.is_empty() {
return;
}
self.record_observation(reference, ReadKey::Name(reference, name.into()), || {
ReadDependency::Name {
object: reference,
name: name.into(),
value: value.clone(),
}
});
}
fn record_observation(
&self,
reference: ObjectRef,
key: ReadKey,
build: impl Fn() -> ReadDependency,
) {
if self.read_only.is_empty() {
return;
}
@@ -220,6 +329,20 @@ impl Heap {
.contains_key(&std::thread::current().id());
for scope in &self.read_only {
if scope.protected.contains(&reference) {
scope
.dependencies
.lock()
.expect("read tracker poisoned")
.entry(key.clone())
.or_insert_with(&build);
for region in scope
.dependency_regions
.lock()
.expect("read tracker poisoned")
.iter_mut()
{
region.entry(key.clone()).or_insert_with(&build);
}
scope
.reads
.lock()