mickael-menu / ShadowVim

Neovim π˜ͺ𝘯𝘴π˜ͺπ˜₯𝘦 Xcode, for real.
GNU General Public License v3.0
345 stars 2 forks source link

Optimizing the buffer synchronization #24

Open mickael-menu opened 1 year ago

mickael-menu commented 1 year ago

ShadowVim currently has a very straightforward approach to synchronizing changes between Neovim and Xcode. It is not very performant nor visually appealing.

:warning: Fixing this is not high on my priority list, as I'd like to integrate more Nvim features first (e.g. command line). If you're enjoying ShadowVim and wish to give back, please consider giving a hand on the following areas of improvements.

Character instead of line-wise changes

The main reason for the visual artifacts is that Neovim sends line-wise change events. So every time you do a change, ShadowVim will update whole lines in Xcode.

https://user-images.githubusercontent.com/58686775/220295684-cf339290-f521-4dfc-8ab5-7eb15fd2b007.mov

A possible fix would be to resolve character indexes instead of line ones to apply a lines change event, here: https://github.com/mickael-menu/ShadowVim/blob/d745f682d41c2d4f4137a1b7f8a533d51a74f1b8/Sources/Mediator/Buffer/BufferMediator.swift#L263

I already added a basic optim to perform character-wise changes only when modifying the end of a line, to make it more bearable when inserting a lot of text:

https://github.com/mickael-menu/ShadowVim/blob/d745f682d41c2d4f4137a1b7f8a533d51a74f1b8/Sources/Mediator/Buffer/BufferMediator.swift#L288-L290

Grouping line changes

When modifying several lines of changes natively from Xcode (e.g. after inserting an Xcode snippet or pasting content from the pasteboard with ⌘V), they are applied line by line back to Nvim, after computing a diff from the current Nvim state.

This is straightforward, thanks to CollectionDifference. A downside, besides the general performance, is that Nvim registers this as a lot of separate change events instead of a compound one. This is very visible when undoing the change.

For example in the following video, I'm inserting an Xcode snippet, then hit u to undo the change, then C-r to redo it. An improvement could be to analyze the CompletionDifference to group consecutive line changes and send them to Nvim in one go.

https://user-images.githubusercontent.com/58686775/220426921-c21fddf8-41ff-45e5-b606-53f93b687a3c.mov

Grouping Neovim's buf_lines_event

Sometimes Neovim sends in one go multiple buffer change events (buf_lines_event) changing the same line multiple times (character by character). You can see this when triggering Neovim's completion with C-n, like in the following video. ShadowVim applies the changes in order, which is of course useless and inefficient in this case. A possible optimization would be to coalesce events changing the same lines and received in a really short span.

To know when to stop coalescing events, we can listened to the flush redraw event in the UI protocol.

https://user-images.githubusercontent.com/58686775/220431433-db673baa-8a56-4950-b58e-2b1481cf0bc0.mov

mickael-menu commented 1 year ago

https://github.com/mickael-menu/ShadowVim/pull/43 introduces several optimizations: