SwiftUI Hierarchical List Performance

I'm struggling with getting a hierarchical SwiftUI List to perform well with large data sets.

Here's a demo repo: https://github.com/lemonmojo/swiftui-hierarchical-list-performance

There are three main problems:

  1. Rendering of the list is slow if there are many items. (Just start the app and wait for the list to be rendered.)
  2. Changing the selected item is very slow. (Tap/Click an item and wait for the selection to change.)
  3. Updating the list is slow. (Press the "Shuffle" button.)

On an iPhone 13 Pro, it takes 6 seconds from tapping the app icon to the list being rendered. This was timed with a release build. Once the list has been rendered, it takes 8 seconds for the selection to change when tapping an item. Tapping the "Shuffle" button results in a 2 seconds delay before the updated list is rendered.

All three problems are much more pronounced on macOS (tested on a Mac Studio M2) where it even takes minutes(!) for the app to become responsive.

Instruments shows that 99% of the CPU time is spent somewhere deep inside SwiftUI. Various attempts have been made to fix the problems (as documented in the code) but none of them have been successful.

There are two different data sets included in the demo. One can switch between them by commenting out one of the let rootItem … declarations and commenting in the other in ContentView.swift. The default example has a flat list with 100.000 items. The "real-world" example has 4 folders, each containing 25.000 items which is faster to render initially, but as soon as you start expanding folders it's slow again.

Is there any way to make SwiftUI's list perform well on iOS and macOS with data sets greater than a couple thousand items? I'm especially worried about the selection performance. Why is selecting an item so slow once the list has been rendered?

I've created a repo that uses AppKit's NSOutlineView here and another one using UIKit's UICollectionView here. Obviously, both are blazing fast and have none of the issues I encounter with SwiftUI List.

Any ideas on how to improve the SwiftUI performance?

Many thx, Felix

I have no recommendations for speeding this up, but I would ask if you've raised a Feedback report for this?

If not, please do so at https://www.apple.com/feedback/ then post the FB number here.

I have no specific recommendation either, but found this thread which addresses some lists performance issues and proposes solutions: https://fatbobman.com/en/posts/optimize_the_response_efficiency_of_list/

I did not test, but hope that may help.

@darkpaw Yes, I will file a FB regarding this in the upcoming days.

@Claude31 I also stumbled upon that article during my research for a solution. Unfortunately, like so many of the solutions out there, it only seems to help/apply to flat lists, not hierarchical ones.

@darkpaw @Claude31

Submitted as FB14822105:

SwiftUI Hierarchical List Performance
 
 
Q