Performance Issues with UITextView and TextKit 2

I've been working with UITextView and TextKit 2, which became the default text engine since iOS 16, and I've encountered performance issues when handling very large text documents.

Even on iPhone 14 Pro, I've noticed stuttering and frame drops when scrolling through the text view containing a large amount of text.

To reproduce the issue, you can use the following code:

// In iOS 16
let textView = UITextView()
textView.text = "some really large string (say 1 million characters)"

The scrolling performance in this scenario is not smooth. However, if you switch to TextKit 1 by accessing the layoutManager property, the performance significantly improves:

// In iOS 16
let textView = UITextView()
let _ = textView.layoutManager  // this enables the compatibility mode
textView.text = "some really large string (say 1 million characters)"

With this code, scrolling remains smooth even with large text documents. It's very disappointing to see TextKit 2 performing worse than TextKit 1, even though Apple claims TextKit 2 has significantly improved performance with noncontiguous layout.

Furthermore, the sample code for TextKit 2, available here, crashes when handling very large text due to excessive memory usage.

Has anyone else experienced similar performance issues with TextKit 2, and are there any potential solutions or workarounds to improve the performance?

Replies

This issue is particularly apparent with text containing multiple paragraphs.

Can't say for iOS, but since it shares the same TextKit codebase as macOS, I can say that scrolling performance degrades significantly above ~3000 lines. 10k lines is an absolute nightmare.

That being said, it seems to matter if a text view has line wrap or not. If it has no linewrap, the performance is significantly worse - which is ironic since you would imagine the layout can just assume fixed line heights.