pub struct TensorGarbageCollector {
pub tensors: HashMap<u64, TensorRef>,
pub roots: Vec<u64>,
}Expand description
Mark-and-sweep garbage collector for tensor allocations.
Tracks a set of TensorRef descriptors connected by dependency edges,
and periodically reclaims those that are unreachable from the named root
set and carry no external references.
Fields§
§tensors: HashMap<u64, TensorRef>All registered tensor descriptors keyed by tensor_id.
roots: Vec<u64>Tensor IDs that are always considered reachable (GC roots).
Implementations§
Source§impl TensorGarbageCollector
impl TensorGarbageCollector
Sourcepub fn register(&mut self, tensor: TensorRef)
pub fn register(&mut self, tensor: TensorRef)
Register a tensor with the collector.
If a tensor with the same tensor_id already exists it is replaced.
Sourcepub fn add_root(&mut self, tensor_id: u64)
pub fn add_root(&mut self, tensor_id: u64)
Add tensor_id to the GC root set.
Roots are always considered reachable regardless of incoming edges. Duplicates are silently ignored.
Sourcepub fn remove_root(&mut self, tensor_id: u64)
pub fn remove_root(&mut self, tensor_id: u64)
Remove tensor_id from the GC root set.
Does nothing if the ID is not present.
Sourcepub fn pin(&mut self, tensor_id: u64)
pub fn pin(&mut self, tensor_id: u64)
Mark a registered tensor as pinned so it is never collected.
Does nothing if tensor_id is not registered.
Sourcepub fn add_ref(&mut self, tensor_id: u64)
pub fn add_ref(&mut self, tensor_id: u64)
Increment the external reference count for tensor_id.
Does nothing if tensor_id is not registered.
Sourcepub fn remove_ref(&mut self, tensor_id: u64)
pub fn remove_ref(&mut self, tensor_id: u64)
Decrement the external reference count for tensor_id (saturating at 0).
Does nothing if tensor_id is not registered.
Sourcepub fn reachable_set(&self) -> Vec<u64>
pub fn reachable_set(&self) -> Vec<u64>
Compute the set of tensor IDs reachable from roots and pinned tensors without mutating collector state.
Reachability is determined by BFS expansion through
TensorRef::dependencies edges.
Sourcepub fn collect(&mut self) -> GcStats
pub fn collect(&mut self) -> GcStats
Run a complete mark-and-sweep garbage collection cycle.
The collection proceeds through three GcPhases:
GcPhase::MarkRoots— seed the reachable set from roots and pinned tensors.GcPhase::Trace— BFS-expand through dependency edges.GcPhase::Sweep— remove unreachable tensors withref_count == 0.
Returns GcStats describing the result.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TensorGarbageCollector
impl RefUnwindSafe for TensorGarbageCollector
impl Send for TensorGarbageCollector
impl Sync for TensorGarbageCollector
impl Unpin for TensorGarbageCollector
impl UnsafeUnpin for TensorGarbageCollector
impl UnwindSafe for TensorGarbageCollector
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more