-
-
Notifications
You must be signed in to change notification settings - Fork 412
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Provide faster version of Formatter.Format that allows for skipping expensive recalculations of CSS #937
Conversation
Formatter.Format currently spends a lot of its time reconstructing and re-compressing a CSS map from the given style, which in most cases will not be changing between calls to Format(). Expose a FastFormat() method that allows the caller to precompute this CSS map to improve performance.
A couple of thoughts:
|
Style data structures are immutable and the pointer is obviously guaranteed to be unique, so that should suffice. |
According to pprof, for a use case calling Format once per line many times, (note this is on a pre-2.0 version but the result should be similar on latest), the original writeHTML takes ~350ms cumulative, while with FastFormat it takes ~50ms. Good point, memoizing on pointer should be fine, I'll try a rework. My golang-fu is weak so let me know if I should be using something other than a hand-rigged map... |
Updated to use a simple map with RW lock. |
NP thanks for your help, but I've whipped up a quick LRU cache that should be sufficient. I'll send a PR in a bit. |
I did this, not to step on your toes, I appreciate the contribution, but because your intuition that there are no small, simple solutions for LRU caches in Go is correct, and I wouldn't want to introduce a heavyweight dependency, and as you're new to Go I didn't want to burden you with having to come up with something that could be relatively complex. Again, I appreciate the contribution. |
Closed in favour of #938 |
Absolutely no problem, I just just wanted to get the ball rolling with this PR, thanks for the quick response! |
Formatter.Format currently spends most of its time (for small calls) reconstructing and re-compressing a CSS map from the given style, which in most cases will not be changing between calls to Format().
Expose public StyleToCSS() and FastFormat() methods that allow the caller to precompute this CSS map to improve performance.
This PR just proposes the simplest backwards-compatible change to allow the performance gains in question. A redesign of Formatter.Format might be called for, but memoizing the CSS map result may be expensive too if style names can't be assumed to uniquely identify an unmodified style.