Skip to main content

AdaptiveDoubleBuffer

Struct AdaptiveDoubleBuffer 

Source
pub struct AdaptiveDoubleBuffer { /* private fields */ }
Expand description

Adaptive double-buffered render target with allocation efficiency.

Wraps DoubleBuffer with capacity tracking to minimize allocations during resize storms. Key strategies:

  1. Over-allocation headroom: Allocate slightly more than needed to handle minor size increases without reallocation.
  2. Shrink threshold: Only shrink if new size is significantly smaller than allocated capacity (prevents thrashing at size boundaries).
  3. Logical vs physical dimensions: Track both the current view size and the allocated capacity separately.

§Invariants

  1. capacity_width >= logical_width and capacity_height >= logical_height
  2. Logical dimensions represent the actual usable area for rendering.
  3. Physical capacity may exceed logical dimensions by up to ADAPTIVE_GROWTH_FACTOR.
  4. Shrink only occurs when logical size drops below ADAPTIVE_SHRINK_THRESHOLD * capacity.

§Failure Modes

ConditionBehaviorRationale
Capacity overflowClamp to u16::MAXPrevents panic on extreme sizes
Zero dimensionsDelegate to DoubleBuffer (panic)Invalid state

§Performance

  • resize() is O(1) when the new size fits within capacity.
  • resize() is O(width × height) when reallocation is required.
  • Target: < 5% allocation overhead during resize storms.

Implementations§

Source§

impl AdaptiveDoubleBuffer

Source

pub fn new(width: u16, height: u16) -> Self

Create a new adaptive buffer with the given logical dimensions.

Initial capacity is set with growth headroom applied.

§Panics

Panics if width or height is 0.

Source

pub fn swap(&mut self)

O(1) swap: the current buffer becomes previous, and vice versa.

After swapping, call current_mut().clear() to prepare for the next frame.

Source

pub fn current(&self) -> &Buffer

Reference to the current (in-progress) frame buffer.

Note: The buffer may have larger dimensions than the logical size. Use logical_width() and logical_height() for rendering bounds.

Source

pub fn current_mut(&mut self) -> &mut Buffer

Mutable reference to the current (in-progress) frame buffer.

Source

pub fn previous(&self) -> &Buffer

Reference to the previous (last-presented) frame buffer.

Source

pub fn width(&self) -> u16

Logical width (the usable rendering area).

Source

pub fn height(&self) -> u16

Logical height (the usable rendering area).

Source

pub fn capacity_width(&self) -> u16

Allocated capacity width (may be larger than logical width).

Source

pub fn capacity_height(&self) -> u16

Allocated capacity height (may be larger than logical height).

Source

pub fn stats(&self) -> &AdaptiveStats

Get allocation statistics.

Source

pub fn reset_stats(&mut self)

Reset allocation statistics.

Source

pub fn resize(&mut self, width: u16, height: u16) -> bool

Resize the logical dimensions. Returns true if dimensions changed.

This method minimizes allocations by:

  1. Reusing existing capacity when the new size fits.
  2. Only reallocating on significant shrink (below threshold).
  3. Applying growth headroom to avoid immediate reallocation on growth.
§Performance
  • O(1) when new size fits within existing capacity.
  • O(width × height) when reallocation is required.
Source

pub fn dimensions_match(&self, width: u16, height: u16) -> bool

Check whether logical dimensions match the given values.

Source

pub fn logical_bounds(&self) -> Rect

Get the logical bounding rect (for scissoring/rendering).

Source

pub fn memory_efficiency(&self) -> f64

Calculate memory efficiency (logical cells / capacity cells).

Trait Implementations§

Source§

impl Debug for AdaptiveDoubleBuffer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.