7. Performance & Entropy Heuristics
Making WFC fast -- smarter cell selection, acceleration structures, and techniques for real-time generation.
Why cell selection order matters
The core WFC loop asks: which cell do we collapse next? A random pick works, but a smarter choice can dramatically reduce contradictions and speed up the run.
The standard heuristic is minimum entropy: always collapse the cell with the fewest remaining options. Ties are broken randomly. This keeps the "wave front" tight and pushes contradictions to the surface early.
Shannon entropy vs option count
Counting remaining options is fast but coarse. If options have weights (as most interesting tilesets do), Shannon entropy gives a more nuanced measure:
H = -sum(w_i * log(w_i))
where each w_i is normalised so the weights in a cell sum to 1.
A cell with three options of equal weight has higher entropy than a cell with three options where one dominates. Shannon entropy captures this, leading to better collapse ordering in practice.
Cell selection showdown
Three grids collapse simultaneously with different cell-selection heuristics. Watch how minimum entropy keeps the wavefront tight while random selection scatters it.
Incremental entropy updates
Recomputing entropy from scratch after every propagation is wasteful. Because propagation only removes options, we can maintain a running entropy value per cell and update it incrementally when options are eliminated.
Keep a per-cell accumulator of sum(w) and sum(w * log(w)). When an option
with weight w_k is removed, subtract its contribution and recompute H from
the accumulators in O(1).
Propagation acceleration
Constraint propagation is the hot loop. Several tricks speed it up:
Bitmask domains
Represent each cell's remaining options as a bitmask. Adjacency checks become bitwise AND operations -- testing dozens of options in a single CPU instruction.
For a tileset of up to 64 tiles, the domain fits in a single u64.
Larger tilesets use a small array of machine words.
Propagation worklist
Instead of scanning every cell after each collapse, maintain a worklist (queue or stack) of cells whose domain changed. Only their neighbours need re-checking. This turns propagation from O(grid) per step to O(affected cells).
Spatial locality
Process the worklist in a cache-friendly order. For 2D grids, a simple approach is to sort pending cells by their position so that memory accesses are sequential.
Propagation cost visualiser
Each step highlights every cell visited during propagation. The counter tracks total cell-visits across the run -- a proxy for real-world propagation cost.
Chunked and streaming generation
For very large outputs -- game worlds, infinite scrollers -- collapsing the entire grid at once is impractical. Instead:
- Divide the output into overlapping chunks.
- Collapse each chunk independently, with boundary cells seeded from already-collapsed neighbours.
- Stitch chunks together, resolving boundary conflicts with a thin strip of re-propagation.
This enables streaming generation: collapse a chunk, send it to the renderer, start the next chunk while the player is still exploring the first.
Parallelism
WFC is inherently sequential -- each collapse depends on the previous propagation. But there are opportunities for parallelism:
- Chunk-level parallelism: independent chunks collapse on separate threads.
- Propagation parallelism: within a single propagation pass, cells whose neighbourhoods don't overlap can be updated concurrently.
- SIMD for bitmask operations: propagation inner loops benefit from vectorised bitwise operations on modern CPUs.
Chunked generation
A large grid is divided into overlapping chunks. Each chunk collapses independently, with boundary cells seeded from already-finished neighbours. Watch the chunks fill in one by one.
Benchmarks at a glance
| Technique | Typical speedup | Complexity |
|---|---|---|
| Bitmask domains | 5-20x | Low |
| Worklist propagation | 2-5x | Low |
| Incremental entropy | 1.5-3x | Low |
| Shannon vs count heuristic | 1.2-2x fewer contradictions | Low |
| Chunked generation | Enables unbounded output | Medium |
| Thread-level parallelism | ~linear in cores (chunked) | High |
Wrapping up
With the right heuristics and data structures, WFC scales from tiny demos to real-time procedural generation in games and creative tools. The techniques in this article -- minimum-entropy selection, bitmask domains, worklist propagation, and chunked streaming -- form the practical toolkit for anyone shipping WFC in production.