Arena

Struct Arena 

Source
pub struct Arena<R>
where R: for<'a> Rootable<'a>,
{ /* private fields */ }
Expand description

A generic, garbage collected arena.

Garbage collected arenas allow for isolated sets of garbage collected objects with zero-overhead garbage collected pointers. It provides incremental mark and sweep garbage collection which must be manually triggered outside the mutate method, and works best when units of work inside mutate can be kept relatively small. It is designed primarily to be a garbage collector for scripting language runtimes.

The arena API is able to provide extremely cheap Gc pointers because it is based around “generativity”. During construction and access, the root type is branded by a unique, invariant lifetime 'gc which ensures that Gc pointers must be contained inside the root object hierarchy and cannot escape the arena callbacks or be smuggled inside another arena. This way, the arena can be sure that during mutation, all Gc pointers come from the arena we expect them to come from, and that they’re all either reachable from root or have been allocated during the current mutate call. When not inside the mutate callback, the arena knows that all Gc pointers must be either reachable from root or they are unreachable and safe to collect. In this way, incremental garbage collection can be achieved (assuming “sufficiently small” calls to mutate) that is both extremely safe and zero overhead vs what you would write in C with raw pointers and manually ensuring that invariants are held.

Implementations§

Source§

impl<R> Arena<R>
where R: for<'a> Rootable<'a>, for<'a> Root<'a, R>: Sized,

Source

pub fn new<F>(f: F) -> Arena<R>
where F: for<'gc> FnOnce(&'gc Mutation<'gc>) -> Root<'gc, R>,

Create a new arena with the given garbage collector tuning parameters. You must provide a closure that accepts a &Mutation<'gc> and returns the appropriate root.

Source

pub fn try_new<F, E>(f: F) -> Result<Arena<R>, E>
where F: for<'gc> FnOnce(&'gc Mutation<'gc>) -> Result<Root<'gc, R>, E>,

Similar to new, but allows for constructor that can fail.

Source

pub fn map_root<R2>( self, f: impl for<'gc> FnOnce(&'gc Mutation<'gc>, Root<'gc, R>) -> Root<'gc, R2>, ) -> Arena<R2>
where R2: for<'a> Rootable<'a>, for<'a> Root<'a, R2>: Sized,

Source

pub fn try_map_root<R2, E>( self, f: impl for<'gc> FnOnce(&'gc Mutation<'gc>, Root<'gc, R>) -> Result<Root<'gc, R2>, E>, ) -> Result<Arena<R2>, E>
where R2: for<'a> Rootable<'a>, for<'a> Root<'a, R2>: Sized,

Source§

impl<R> Arena<R>
where R: for<'a> Rootable<'a>,

Source

pub fn mutate<F, T>(&self, f: F) -> T
where F: for<'gc> FnOnce(&'gc Mutation<'gc>, &'gc Root<'gc, R>) -> T,

The primary means of interacting with a garbage collected arena. Accepts a callback which receives a &Mutation<'gc> and a reference to the root, and can return any non garbage collected value. The callback may “mutate” any part of the object graph during this call, but no garbage collection will take place during this method.

Source

pub fn mutate_root<F, T>(&mut self, f: F) -> T
where F: for<'gc> FnOnce(&'gc Mutation<'gc>, &'gc mut Root<'gc, R>) -> T,

An alternative version of Arena::mutate which allows mutating the root set, at the cost of an extra write barrier.

Source

pub fn metrics(&self) -> &Metrics

Source

pub fn collection_phase(&self) -> CollectionPhase

Source§

impl<R> Arena<R>
where R: for<'a> Rootable<'a>, for<'a> Root<'a, R>: Collect,

Source

pub fn collect_debt(&mut self)

Run incremental garbage collection until the allocation debt is <= 0.0.

There is no minimum unit of work enforced here, so it may be faster to only call this method when the allocation debt is above some threshold.

This method will always return at least once when collection enters the Sleeping phase, i.e. it will never transition from the Sweeping phase to the Marking phase without returning in-between.

Source

pub fn mark_debt(&mut self) -> Option<MarkedArena<'_, R>>

Run only the marking part of incremental garbage collection until allocation debt is <= 0.0.

This does not transition collection past the Marked phase. Does nothing if the collection phase is Marked or Sweeping, otherwise acts like Arena::collect_debt.

Source

pub fn collect_all(&mut self)

Run the current garbage collection cycle to completion, stopping once garbage collection has restarted in the sleep phase. If the collector is currently in the sleep phase, this restarts the collection and performs a full collection before transitioning back to the sleep phase.

Source

pub fn mark_all(&mut self) -> Option<MarkedArena<'_, R>>

Runs all of the remaining marking part of the current garbage collection cycle.

Similarly to Arena::mark_debt, this does not transition collection past the Marked phase, and does nothing if the collector is currently in the Marked phase or the Sweeping phase.

Auto Trait Implementations§

§

impl<R> Freeze for Arena<R>
where <R as Rootable<'static>>::Root: Freeze,

§

impl<R> !RefUnwindSafe for Arena<R>

§

impl<R> !Send for Arena<R>

§

impl<R> !Sync for Arena<R>

§

impl<R> Unpin for Arena<R>
where <R as Rootable<'static>>::Root: Unpin,

§

impl<R> !UnwindSafe for Arena<R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.