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:
- Over-allocation headroom: Allocate slightly more than needed to handle minor size increases without reallocation.
- Shrink threshold: Only shrink if new size is significantly smaller than allocated capacity (prevents thrashing at size boundaries).
- Logical vs physical dimensions: Track both the current view size and the allocated capacity separately.
§Invariants
capacity_width >= logical_widthandcapacity_height >= logical_height- Logical dimensions represent the actual usable area for rendering.
- Physical capacity may exceed logical dimensions by up to
ADAPTIVE_GROWTH_FACTOR. - Shrink only occurs when logical size drops below
ADAPTIVE_SHRINK_THRESHOLD * capacity.
§Failure Modes
| Condition | Behavior | Rationale |
|---|---|---|
| Capacity overflow | Clamp to u16::MAX | Prevents panic on extreme sizes |
| Zero dimensions | Delegate 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
impl AdaptiveDoubleBuffer
Sourcepub fn new(width: u16, height: u16) -> Self
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.
Sourcepub fn swap(&mut self)
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.
Sourcepub fn current(&self) -> &Buffer
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.
Sourcepub fn current_mut(&mut self) -> &mut Buffer
pub fn current_mut(&mut self) -> &mut Buffer
Mutable reference to the current (in-progress) frame buffer.
Sourcepub fn capacity_width(&self) -> u16
pub fn capacity_width(&self) -> u16
Allocated capacity width (may be larger than logical width).
Sourcepub fn capacity_height(&self) -> u16
pub fn capacity_height(&self) -> u16
Allocated capacity height (may be larger than logical height).
Sourcepub fn stats(&self) -> &AdaptiveStats
pub fn stats(&self) -> &AdaptiveStats
Get allocation statistics.
Sourcepub fn reset_stats(&mut self)
pub fn reset_stats(&mut self)
Reset allocation statistics.
Sourcepub fn resize(&mut self, width: u16, height: u16) -> bool
pub fn resize(&mut self, width: u16, height: u16) -> bool
Resize the logical dimensions. Returns true if dimensions changed.
This method minimizes allocations by:
- Reusing existing capacity when the new size fits.
- Only reallocating on significant shrink (below threshold).
- 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.
Sourcepub fn dimensions_match(&self, width: u16, height: u16) -> bool
pub fn dimensions_match(&self, width: u16, height: u16) -> bool
Check whether logical dimensions match the given values.
Sourcepub fn logical_bounds(&self) -> Rect
pub fn logical_bounds(&self) -> Rect
Get the logical bounding rect (for scissoring/rendering).
Sourcepub fn memory_efficiency(&self) -> f64
pub fn memory_efficiency(&self) -> f64
Calculate memory efficiency (logical cells / capacity cells).