-
Notifications
You must be signed in to change notification settings - Fork 263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Memoization to Speed Up Textarea Rendering #427
Implement Memoization to Speed Up Textarea Rendering #427
Conversation
3319f7c
to
637866f
Compare
Please merge this! |
You can try to use https://github.com/go-go-golems/bobatea which is my alternative version, until it's merged. I haven't tried it out outside of my go workspace, so I hope it works for you. |
Hi! Thank so much for this PR, I am going to test this out very soon so we can get this in! We really appreciate the work on this one. |
90254c4
to
a8d6c52
Compare
Really solid work @wesen. Thank you so much for these improvements! I noticed a ton of performance benefits with this. One thing I did change (when testing with 1000 lines of text) it was still slow until I changed the cache capacity to match the number of lines. Instead of hardcoding this, I make it so that the capacity matches the |
This pull request introduces a significant performance improvement to the
textarea rendering process by implementing memoization to cache the results of
the wrapping computation. This change reduces the computational overhead
associated with repeated calculations, particularly for larger text areas.
This at least partially fixes charmbracelet/bubbletea#831
although insertion and rewrapping are still "suboptimal". I think it does the job
quite nicely in practice.
Key Changes:
Introduction of
memoization.go
: This new file contains theimplementation of a generic memoization cache with a Least Recently Used
(LRU) eviction policy. The cache is thread-safe and can be used with any type
that implements the
Hasher
interface.Addition of
MemoCache
toModel
struct: TheModel
struct intextarea.go
now includes aMemoCache
instance for caching the results ofthe wrapping computation.
Modification of
wrap
function calls: All calls to thewrap
functionin
textarea.go
have been replaced with calls to the newmemoizedWrap
function. This function checks the cache for a precomputed result before
falling back to the
wrap
function if necessary.Addition of
WrapInput
struct andHash
method: TheWrapInput
structand its associated
Hash
method have been added to facilitate the hashing ofinputs to the wrapping computation for cache storage.
Important: I reused my generic Memoization which bumps go to 1.18. I'm not sure how consequent it is to introduce that kind of change incidentally, but didn't want to remove the generics for now. I can "instantiate" it all to string if sticking with 1.17 is desired.
Disclaimer: I'm pretty bullish on LLMs for programming. The memoization code, the fuzzer and the unit tests are heavily copiloted and GPT4-ed. I haven't tested the concurrency of the data structure explicitly, I'm happy to add a bunch of tests and fuzzers for that as well.