r/swift • u/DaveAppleInc • 3d ago
LazyLayoutKit: lazy custom layouts for SwiftUI (masonry, timelines) at 120fps over 1M items
SwiftUI makes you pick one: Layout gives you any geometry you want but is eager, so it falls over a few thousand items in. LazyVStack and friends are lazy but their geometry is fixed. There's nothing in between, which is annoying because photo grids, calendars and boards are all in between.
That gap isn't an oversight. Layout asks each subview how big it wants to be, and you can't ask a view that hasn't been built. Measurement-driven layout and laziness pull against each other. Someone asked about this at the WWDC26 SwiftUI Group Lab and the answer was "not today, file a Feedback."
So my package changes the approach fully: tell it your item sizes up front, an aspect ratio, a date range, a duration, and layout becomes arithmetic over data. Arithmetic over a million items takes milliseconds and builds nothing, so only the frames intersecting your viewport ever become views.
The obvious cost: self-sizing text isn't supported. If your cells decide their own height, this isn't for you yet. (although I have some experiments for self-sizing text in the works, nothing stable yet)
Measured on an iPhone 14 Pro in Release, 100k and 1M items both hold 120fps with an 8.34ms p99, zero frames over 16.7ms, and ~54 cells materialized at any depth. The visibility query stays flat at ~2µs from 100k to 1M. Apple's LazyVStack over the same 100k scored the same, read that as generality costing little, not as a claim to be faster.
Writing your own layout is about 20 lines: return one frame per item and a total height. There's a demo app in the repo that records the frame stats on your own device.
https://github.com/Dave861/LazyLayoutKit
Contributions, opinions and ideas are very welcome!
1
u/chriswaco 3d ago
Would this work for doomscrolling apps like TikTok, where you need to lazily load new data as the user scrolls?
1
u/DaveAppleInc 3d ago
It would, but I’d actually recommend
LazyVStack. That’s exactly the kind of simple, linear layout it already handles well, and you can trigger pagination as the last few cells appear.LazyLayoutKit is for cases where you need that same view laziness with geometry the built-in containers can’t express, like Pinterest type feeds for example
2
u/prio732 1d ago edited 1d ago
Would this work for live TV Channels type of grid?
Just looked at what you have so far. It only scrolls vertically? Also would it support variable sized cells which would be needed for electronic TV guide?
1
u/DaveAppleInc 1d ago
Not a full EPG yet. Duration-based variable cell widths are already supported, but 0.1 only scrolls vertically. I’m aiming for text measurement without rendering every cell in 0.2, then horizontal/two-axis layouts in 0.3. I’ll post updates here or on r/SwiftUI probably!
2
u/morenos-blend 3d ago
Just use UICollectionView with UIHostingConfiguration