Repository navigation
Layout Diff
Blinc separates layout computation from visual rendering, enabling incremental updates without full tree rebuilds.
Blinc uses Taffy for flexbox, grid and block layout. Taffy's algorithms run over Blinc's own node storage, LayoutTree, through Taffy's tree traits. Each node holds its style, its children, its layout and Taffy's layout cache, and a LayoutNodeId is the node's key, with no second id to map.
LayoutTree lays out again only the part of the tree that an edit can reach. During layout, each node keeps a log of the queries its parent asked it, with the answers it gave. A query asks for a size under given constraints, or for a full layout. After an edit, the changed node is asked its logged queries again, and its subtree is laid out again in the process. If every answer is the same, the parent's layout cannot change, so layout stops at that node and only its subtree is rounded to pixels. If an answer differs, the parent is asked again in the same way, one level up. Only an edit whose answers change all the way up lays the tree out from the root.
A text change inside a box with a fixed size stops at the box. A month added to a clipped carousel stops at the carousel. A change that moves its siblings, such as a label that grows inside an auto-sized row, goes up as far as the sizes change.
Two environment switches control this:
-
BLINC_LAYOUT_BOUNDARIES=0lays the tree out from the root after every edit, as Taffy's own tree does. It is safe to run with, but slower. -
BLINC_LAYOUT_CHECK=1lays the tree out from scratch after every layout, and panics when any node's layout differs from the incremental result. It is for tests, and is much slower.
Layout and visual properties are handled separately:
| Layout Properties | Visual Properties |
|---|---|
width, height
|
background |
padding, margin
|
border_color |
flex_direction |
shadow |
justify_content |
opacity |
align_items |
transform |
gap |
rounded |
This separation allows visual-only updates without layout recomputation.
struct RenderTree {
layout_tree: LayoutTree, // nodes, styles and layouts
render_props: HashMap<NodeId, RenderProps>, // Visual properties
dirty_nodes: HashSet<NodeId>, // Nodes needing update
scroll_state: HashMap<NodeId, ScrollState>,
motion_bindings: HashMap<NodeId, MotionBinding>,
}Layout is computed on-demand when the tree structure changes:
// Compute layout for the tree
render_tree.compute_layout(root_id, AvailableSpace {
width: window_width,
height: window_height,
});
// Get computed bounds for a node
let layout = render_tree.get_layout(node_id);
// Returns: Layout { x, y, width, height }The diff system determines the minimum changes needed when UI structure updates.
Every element has a hash computed from its properties:
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
struct DivHash(u64);
impl Div {
// Hash of this element only (excludes children)
fn compute_hash(&self) -> DivHash {
let mut hasher = DefaultHasher::new();
self.width.hash(&mut hasher);
self.height.hash(&mut hasher);
self.background.hash(&mut hasher);
// ... all properties
DivHash(hasher.finish())
}
// Hash including entire subtree
fn compute_tree_hash(&self) -> DivHash {
let mut hasher = DefaultHasher::new();
self.compute_hash().hash(&mut hasher);
for child in &self.children {
child.compute_tree_hash().hash(&mut hasher);
}
DivHash(hasher.finish())
}
}Changes are classified to determine the update strategy:
struct ChangeCategory {
layout: bool, // Requires layout recomputation
visual: bool, // Only visual properties changed
children: bool, // Children added/removed/reordered
handlers: bool, // Event handlers changed
}| Category | Example Changes | Action |
|---|---|---|
| Visual only |
bg, opacity, shadow
|
Update RenderProps |
| Layout |
width, padding, gap
|
Recompute layout |
| Children | Add/remove child | Rebuild subtree |
| Handlers | Event callback changed | Update handlers |
When children change, the diff algorithm matches old and new children:
enum ChildDiff {
Unchanged { index: usize }, // Same content, same position
Moved { old_idx: usize, new_idx: usize }, // Same content, moved
Modified { old_idx: usize, new_idx: usize, diff: Box<DiffResult> },
Added { index: usize }, // New child
Removed { index: usize }, // Old child gone
}- Compute hashes for all old and new children
- Build hash map of old children by hash
- Match new children to old by hash lookup
- Detect moves when hash matches at different position
- Classify remaining as added or removed
- Merge same-position changes as modifications
fn diff_children(old: &[Div], new: &[Div]) -> Vec<ChildDiff> {
let old_hashes: Vec<_> = old.iter().map(|c| c.compute_tree_hash()).collect();
let new_hashes: Vec<_> = new.iter().map(|c| c.compute_tree_hash()).collect();
let mut old_by_hash: HashMap<DivHash, usize> = old_hashes
.iter()
.enumerate()
.map(|(i, h)| (*h, i))
.collect();
let mut diffs = Vec::new();
for (new_idx, new_hash) in new_hashes.iter().enumerate() {
if let Some(old_idx) = old_by_hash.remove(new_hash) {
if old_idx == new_idx {
diffs.push(ChildDiff::Unchanged { index: new_idx });
} else {
diffs.push(ChildDiff::Moved { old_idx, new_idx });
}
} else {
diffs.push(ChildDiff::Added { index: new_idx });
}
}
// Remaining old children were removed
for (old_idx, _) in old_by_hash {
diffs.push(ChildDiff::Removed { index: old_idx });
}
diffs
}The incremental update process returns what changed:
enum UpdateResult {
NoChanges, // Nothing to do
VisualOnly(Vec<PropUpdate>), // Apply prop updates only
LayoutChanged, // Props + recompute layout
ChildrenChanged, // Rebuild subtrees + layout
}The reconcile function determines actions from diffs:
struct ReconcileActions {
prop_updates: Vec<(NodeId, RenderProps)>, // Visual updates
subtree_rebuild_ids: Vec<NodeId>, // Nodes to rebuild
needs_layout: bool, // Layout recomputation needed
}
fn reconcile(old: &Div, new: &Div) -> ReconcileActions {
let changes = categorize_changes(old, new);
let mut actions = ReconcileActions::default();
if changes.visual && !changes.layout && !changes.children {
// Visual-only: just update props
actions.prop_updates.push((node_id, new.to_render_props()));
}
if changes.layout {
actions.needs_layout = true;
}
if changes.children {
actions.subtree_rebuild_ids.push(node_id);
actions.needs_layout = true;
}
actions
}incremental_update(root, new_element)
│
├── Compare hashes: old_tree_hash vs new_tree_hash
│ └── Same? Return NoChanges
│
├── Compare element hashes: old_hash vs new_hash
│ └── Same? Children might have changed
│
├── Categorize changes
│ ├── Visual only? Queue prop updates
│ ├── Layout changed? Mark layout dirty
│ └── Children changed? Diff children recursively
│
└── Return UpdateResult
Every node stores a topology hash. The hash covers the node's kind (div, text, SVG or image), its element id, its text, SVG or image content, and the topology hashes of its children. incremental_update compares each stored hash with the hash of the new element tree and rebuilds only the nodes whose hashes differ:
- If a child has the same topology, the update skips it.
- If a child has the same kind, id and content but differs somewhere below, the child stays and the walk continues below it.
- If a child differs in kind, id or content, the update replaces it. Its siblings keep their nodes, along with their handlers, scroll positions and animations.
- If a child differs and any child of the node has an id, or if the number of children changed, the update rebuilds the children of that node. A child with an id and the same topology keeps its node.
incremental_update returns ChildrenChanged only if it built a node. When a list updates in place after its data changes, the stored hashes of its parents go stale. A later update finds that the list already matches the new tree, so it builds nothing and returns LayoutChanged.
A change in the shape of the tree can change which stylesheet rules a node matches, even if the update does not touch the node. The caller of incremental_update passes the result to RenderTree::restyle_after_update, which runs the rules again on the nodes whose matches can have changed.
During an update, the tree records every node whose children were rebuilt or removed. This page calls them changed parents. The rules run again on:
- The nodes whose props the update replaced.
- Each changed parent and its children.
:nth-child,:last-childand the sibling combinators depend on where a node sits among its siblings, so a node can start or stop matching when a sibling is added or removed. - Everything under each changed parent, if the stylesheet has a rule like
.row:last-child .cell. That rule matches a.cellonly when its.rowis the last child. When a.rowgains or loses a next sibling, every.cellunder it can change its match. - The ancestors of each changed parent, if the stylesheet has a rule like
.card:has(.badge). A.cardmatches that rule only while it contains a.badge, so a change below it can change the match.
The update already applied the rules to the subtrees it built, so they are not in this list. The cost depends on how many nodes are in this list. It does not depend on the size of the tree.
Two cases run the rules on every node instead. The first is a rule like .page:has(.flag) .panel, where :has sits on an ancestor of the element the rule styles. A change anywhere under .page can add or remove a .flag, and that can change the match of every .panel under it. The restyle does not look for those nodes, so a stylesheet with such a rule runs the rules on every node after each update that builds nodes. The second is the first build of a tree, which records no changed parents.
The system uses global queues for deferred updates:
// Global pending updates (thread-local)
static PENDING_PROP_UPDATES: RefCell<Vec<(NodeId, RenderProps)>>;
static PENDING_SUBTREE_REBUILDS: RefCell<Vec<NodeId>>;
static NEEDS_REDRAW: AtomicBool;The windowed app processes queues each frame:
fn process_pending_updates(&mut self) {
// Apply prop updates (no layout needed)
for (node_id, props) in drain_prop_updates() {
self.render_tree.update_props(node_id, props);
}
// Rebuild dirty subtrees
for node_id in drain_subtree_rebuilds() {
self.rebuild_subtree(node_id);
}
// Recompute layout if needed
if self.layout_dirty {
self.render_tree.compute_layout(self.root_id, self.available_space);
self.layout_dirty = false;
}
// Trigger redraw if visual changes occurred
if NEEDS_REDRAW.swap(false, Ordering::SeqCst) {
self.request_redraw();
}
}- O(1) per element to compute hash
- O(1) equality check via hash comparison
- No deep property-by-property comparison needed
- O(n) to build hash map
- O(n) to match children
- Detects moves without position-based assumptions
| Scenario | What Runs |
|---|---|
| Hover color change | Update 1 RenderProps |
| Text content change | Rebuild 1 text node |
| Add item to list | Insert node, layout affected subtree |
| Reorder list | Move nodes, minimal layout |
- Layout only recomputes when structure/dimensions change
- Visual-only changes skip layout entirely
- Taffy caches intermediate results
- An edit lays out again only the subtree whose answers to its parent change, as described in Taffy Integration
Getting Started
Migrating
Mobile Development
Example Gallery
- Example Gallery
- Canvas Element
- Canvas Kit Interactive
- Carousel Demo - Selector API Showcase
- Chrome-Style Tabs
- blinc_cn Components
- Run with
cargo run -p blinc_app_examples --features cn --example cn_pickers_demo. - Code Element
- Complex SVG
- CSS Debug
- CSS Visual Features
- Layer Effects
- Emoji and HTML Entities
- @flow Shader
- Fluid Surface
- Skeleton animation with glTF +
blinc_canvas_kit. - Gpu Pass Demo
- Image CSS Styling
- Image Layer Test
- Keyframe Animation Canvas
- Markdown Editor
- 3D Mesh Demo — renders the Khronos glTF
DamagedHelmetsample model - Motion Demo
- Music Player Glass Card
- Node-editor demo — pre-wired graph with three node types, typed
- Notch Menu Bar
- Overflow Fade
- Overlay System
- Pointer Query
- Rich Text Element
- Rich Text Editor
- Scroll Container
- Semantic @flow
- Sortable
- Stateful API + Signal-bound modifiers demo.
- End-to-end 3D demo wiring Blinc's SceneKit3D renderer up to
- Unified Styling API
- SVG Animation
- Table Builder
- Tabler Icons
- Minimal text positioning test
- Text Input Widgets
- KHR_texture_transform
- Theme System
- Timeline Animation
- Typography
- Wet Glass
- Windowed Application
Web Development
Core Concepts
- Elements & Layout
- Styling & Materials
- CSS Styling
- Theming
- Event Handling
- Accessibility
- State Management
- Lists & Conditional Content
- Element Refs
Animation
Components
Component Library (blinc_cn)
- Overview
- Button
- Card
- Icons
- Typography
- Dialog
- Form Components
- Date, Time and Colour Pickers
- Rich Text Editor
- Code Editor
- Navigation
- Feedback
- Layout
- Data Display
Widgets
- Buttons & Inputs
- Text & Rich Text
- Code Editor
- Scroll Containers
- Virtualized List
- Canvas Drawing
- Gestures
- Notched Surfaces
- Images & SVG
- Audio & Video
- Markdown Rendering
Canvas Kit
Advanced
- Routing & Navigation
- Multi-Window
- System Integration
- Element Query API
- Overlay System
- Custom State Machines
- Pointer Query
- Performance Tips
- Flow Shaders
- 3D Rendering
- Custom GPU Passes
- Hot reloading
Architecture
Contributing