pub struct StorageGarbageCollector {
pub config: StorageGcConfig,
pub objects: HashMap<GcObjectId, GcObject>,
pub roots: HashSet<GcObjectId>,
pub gc_runs: VecDeque<StorageGcRun>,
pub phase: GcPhase,
pub next_run_id: u64,
}Expand description
Production-grade mark-and-sweep garbage collector for content-addressed storage.
Objects form a directed acyclic graph (DAG) via GcObject::links.
GC roots and pinned objects anchor the live set; everything else that has
no external references (ref_count == 0) is eligible for collection.
Fields§
§config: StorageGcConfigRuntime configuration.
objects: HashMap<GcObjectId, GcObject>All tracked objects keyed by id.
roots: HashSet<GcObjectId>Set of explicit GC roots.
gc_runs: VecDeque<StorageGcRun>History of completed GC runs (capped at 256 entries).
phase: GcPhaseCurrent collector phase.
next_run_id: u64Counter used to assign monotonically increasing run ids.
Implementations§
Source§impl StorageGarbageCollector
impl StorageGarbageCollector
Sourcepub fn new(config: StorageGcConfig) -> Self
pub fn new(config: StorageGcConfig) -> Self
Create a new collector with the given configuration.
Sourcepub fn add_object(&mut self, object: GcObject) -> Result<(), GcError>
pub fn add_object(&mut self, object: GcObject) -> Result<(), GcError>
Add a new object. Returns GcError::ObjectAlreadyExists if an
object with the same id is already tracked.
Sourcepub fn remove_object(&mut self, id: &GcObjectId) -> Result<GcObject, GcError>
pub fn remove_object(&mut self, id: &GcObjectId) -> Result<GcObject, GcError>
Remove and return an object.
Returns errors when:
- The object does not exist.
- The object is pinned.
- The object has
ref_count > 0.
Sourcepub fn add_root(&mut self, id: &GcObjectId) -> bool
pub fn add_root(&mut self, id: &GcObjectId) -> bool
Mark an object as a GC root.
Returns true if the object exists (and was added as root), false
otherwise.
Sourcepub fn remove_root(&mut self, id: &GcObjectId) -> bool
pub fn remove_root(&mut self, id: &GcObjectId) -> bool
Remove an object from the GC root set.
Returns true if the object was a root (and has been removed).
Sourcepub fn increment_ref(&mut self, id: &GcObjectId) -> bool
pub fn increment_ref(&mut self, id: &GcObjectId) -> bool
Increment the external reference count for an object.
Returns true if the object was found, false otherwise.
Sourcepub fn decrement_ref(&mut self, id: &GcObjectId) -> bool
pub fn decrement_ref(&mut self, id: &GcObjectId) -> bool
Decrement the external reference count (floor 0).
Returns true if the object was found. When ref_count reaches 0 and
the object is neither a root nor pinned, it becomes a collection
candidate on the next GC run.
Sourcepub fn pin(&mut self, id: &GcObjectId) -> bool
pub fn pin(&mut self, id: &GcObjectId) -> bool
Pin an object so it is never swept.
Returns true if the object was found.
Sourcepub fn unpin(&mut self, id: &GcObjectId) -> bool
pub fn unpin(&mut self, id: &GcObjectId) -> bool
Unpin an object so it can be swept when unreachable.
Returns true if the object was found.
Sourcepub fn should_run(&self) -> bool
pub fn should_run(&self) -> bool
Returns true when live_bytes / max_live_bytes ≥ sweep_threshold_fraction.
Sourcepub fn mark_reachable(&mut self, now: u64) -> HashSet<GcObjectId>
pub fn mark_reachable(&mut self, now: u64) -> HashSet<GcObjectId>
BFS traversal from all roots and pinned objects.
Every reachable object gets last_marked = Some(now).
Returns the set of reachable ids.
Sourcepub fn run_gc(&mut self, now: u64) -> StorageGcRun
pub fn run_gc(&mut self, now: u64) -> StorageGcRun
Perform a full mark-and-sweep GC run.
Steps:
- Mark all objects reachable from roots / pinned objects.
- Sweep unreachable, unpinned, ref_count==0 objects.
- Record and return a
StorageGcRun.
Sourcepub fn live_bytes(&self) -> u64
pub fn live_bytes(&self) -> u64
Total bytes across all currently tracked objects.
Sourcepub fn reachable_bytes(&self, reachable: &HashSet<GcObjectId>) -> u64
pub fn reachable_bytes(&self, reachable: &HashSet<GcObjectId>) -> u64
Total bytes across the supplied reachable set.
Sourcepub fn object_count(&self) -> usize
pub fn object_count(&self) -> usize
Number of tracked objects.
Sourcepub fn root_count(&self) -> usize
pub fn root_count(&self) -> usize
Number of GC-root objects.
Sourcepub fn stats(&self) -> StorageGcStats
pub fn stats(&self) -> StorageGcStats
Return aggregate statistics.
Auto Trait Implementations§
impl Freeze for StorageGarbageCollector
impl RefUnwindSafe for StorageGarbageCollector
impl Send for StorageGarbageCollector
impl Sync for StorageGarbageCollector
impl Unpin for StorageGarbageCollector
impl UnsafeUnpin for StorageGarbageCollector
impl UnwindSafe for StorageGarbageCollector
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