tview icon indicating copy to clipboard operation
tview copied to clipboard

Optimizations for TextView

Open urandom opened this issue 6 years ago • 13 comments

Only clear the index when the textview dimensions change or the text itself is cleared. Add to the index when new text is added to the view.

Fixes #266

urandom avatar Apr 14 '19 14:04 urandom

I've created benchmarks for writing to and drawing TextViews (submitted as #356) and compared the results before and after applying these optimizations.


go test -run=NONE -bench=. -count=3

Before

After


benchcmp -best -changed

Comparison

tslocum avatar Oct 20 '19 00:10 tslocum

So, it has gotten worse? Because after shows more ns/operation and such

Bios-Marcel avatar Oct 20 '19 14:10 Bios-Marcel

Based on the benchmark results, these optimizations:

  • Increase the amount of time it takes to write to unwrapped TextViews by 350%
  • Do not affect the amount of time it takes to write to wrapped TextViews
  • Increase the number of allocations when writing to both unwrapped and wrapped TextViews by 1000%
  • Increase the allocation size when writing to unwrapped TextViews by 500%
  • Do not affect the allocation size of writing to wrapped TextViews
  • Reduce the amount of time it takes to draw unwrapped TextViews by 65%
  • Reduce the amount of time it takes to draw wrapped TextViews by 20%
  • Reduce the allocation size when drawing unwrapped TextViews by 40%
  • Reduce the allocation size when drawing wrapped TextViews by 20%

tslocum avatar Oct 21 '19 04:10 tslocum

This change optimizes for large amounts of text, especially one that constantly gets appended to (displaying a tailing log). Your benchmarks present a static text of 512 bytes, where performance is quite insignificant.

urandom avatar Oct 21 '19 07:10 urandom

Thanks for your feedback. I've updated the tests and benchmarks to include cases where a small amount of data is written to a TextView that contains a lot of data. You can see these changes in the updated pull request I linked earlier.

Updated results and comparison

tslocum avatar Oct 23 '19 23:10 tslocum

Any chance to have this merged? I am currently using tview for TUI for my experimental chat application and use TextView as a chat log.

foxcpp avatar Apr 24 '20 09:04 foxcpp

@tslocum, hey, since you are maintaining cview, what do you think about getting that PR in? Imo this is a good improvement. I would submit it but there are some non-trivial conflicts so I am not sure about them.

foxcpp avatar Apr 24 '20 11:04 foxcpp

I rebased it here: https://github.com/foxcpp/tview/tree/textview-opts but as I said, I am not sure if I did everything correctly. Also included my small optimization from #435.

foxcpp avatar Apr 24 '20 12:04 foxcpp

Thanks @foxcpp. I am interested in merging your changes into cview. I'll let you know once I've had a chance to review your changes.

tslocum avatar Apr 24 '20 22:04 tslocum

Hey @foxcpp, I only see your optimization to use TrimRightFunc in your updated branches. Were you able to review the benchmark results which seem to show these changes cause decreases in performance?

tslocum avatar Apr 26 '20 23:04 tslocum

@tslocum Woops, forgot to git push. Now it should be there. Also added your benchmarks.

Here is the benchcmp output master vs textview-opts branch: benchcmp.txt

There is a decrease in performance for Draw almost everywhere, enough to nullify my TrimRightFunc change, huh. Write gets +500% improvement for scrollable TextView, though.

foxcpp avatar Apr 27 '20 05:04 foxcpp

I believe you have the results backwards: when not wrapping text, Draw is improved 60% while Write is up to 600% slower (if old is master and new is textview-opts).

tslocum avatar Apr 27 '20 23:04 tslocum

Woops x2. You are right.

foxcpp avatar Apr 27 '20 23:04 foxcpp

Closing this as it's obsolete. Check out the new TextView implementation for details.

rivo avatar Aug 26 '23 14:08 rivo