subconsciousnetwork / subconscious

Apache License 2.0
6 stars 0 forks source link

Editor performance improvements #1109

Closed bfollington closed 5 months ago

bfollington commented 5 months ago

While investigating https://github.com/subconsciousnetwork/subconscious/issues/1108 I profiled the plainEditor() and found it was extremely expensive on each keystroke. It's incredibly expensive to set the .text property on the underlying view and doing so can result in cursor desync.

I suspect this is also responsible for https://github.com/subconsciousnetwork/subconscious/issues/566.

Setting .text frequently causes updateUIView() to take a LONG time (up to 400ms in debug mode) which means isUIViewUpdating stays true for a long time and we start dropping input events if the user is typing rapidly. This is what is responsible for the autocorrect cursor desync issue linked above and can be replicated by typing very fast into the simulator. It's harder to trigger in release, on-device, but it happens there too.

In brief: there's a pathological update cycle happening that results in setting .text twice when we can set it 0 times. I've documented the specifics in the code.

I made two changes:

  1. enhance the change detection logic so we can trust view.text more often instead of resetting it
  2. split the rendering of the new gradient BG into another layer from the editor itself (minor, but helpful)

The difference is more exaggerated on simulator, but it's night and day.

Before

https://github.com/subconsciousnetwork/subconscious/assets/5009316/1eec6b50-8abf-4ddb-ab9e-ce0504597587

After

https://github.com/subconsciousnetwork/subconscious/assets/5009316/39af615e-3802-48da-82a0-af04265b8df2