Evergreen-lxl / Evergreen.lxl

🌳 Treesitter support for Lite XL.
MIT License
35 stars 7 forks source link

feat: optimise byte length calculations #67

Closed xcb-xwii closed 3 months ago

xcb-xwii commented 3 months ago

Optimise calculation of byte lengths by:

  1. Using a prefix sum instead of just adding everything up / taking length of text from Doc.get_text -> avoid stutters on large files (only the first input might stutter a bit now, as the length needs to be calculated once first)
  2. Using # instead of string.len to get lengths -> reduces length of stutters SIGNIFICANTLY (by around 200x for all files)

This avoids absolute terror when editing large files.

Without this change, when inserting at the end a 60k line file, 99%+ of time spent on inserting text into a document was just running accumulateLen:

| ...ffbc626d9214327eec2f27b81e36896f-1/data/core/docview     : on_text_input                 :   358  : 5.2338 : 59.2  :   23 |
| ...fbc626d9214327eec2f27b81e36896f-1/data/core/doc/init     : text_input                    :   522  : 5.2337 : 59.2  :   23 |
| ...fbc626d9214327eec2f27b81e36896f-1/data/core/doc/init     : insert                        :   490  : 5.2297 : 59.2  :   23 |
| ...14327eec2f27b81e36896f-1/user/plugins/evergreen/init     : raw_insert                    :    66  : 5.2287 : 59.2  :   23 |
| ...14327eec2f27b81e36896f-1/user/plugins/evergreen/init     : accumulateLen                 :    23  : 5.2037 : 58.9  :   18 |

With this change, the cost of computing the byte lengths are much lower:


| ...14327eec2f27b81e36896f-1/user/plugins/evergreen/init     : lenLines                      :    36  : 0.0024 : 0.0   :   12 |```
xcb-xwii commented 3 months ago

With some more testing it seems that the 200x figure is way higher than it actually is. 200x was mostly really the profiler slowing the function down.

Actual figure is like around 3x though, so this is still pretty worth it.

xcb-xwii commented 3 months ago

@TorchedSammy any thoughts? i think this is good to go