-
Notifications
You must be signed in to change notification settings - Fork 146
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
Use RWMutex instead of Mutex for locking templates #90
Conversation
The template render lock means that template rendering in unrolled is single-threaded and only one template can be rendered at a time by any project using unrolled. This is clearly not ideal. This PR suggests using a RWMutex instead restoring concurrent rendering to unrolled. Signed-off-by: Andrew Thornton <[email protected]>
I have an additional pr that can use fsnotify to recompile the templates on change to their directories. And for the asset routes the compileTemplates function should just be exposed so that a user like gitea can decide not to use isDevelopment but use their own function to call for recompilation |
This is an awesome find, thanks for the performance boost 😄 I checked in a quick benchmark to test the difference... your fix definitely helps a lot: goos: darwin
goarch: amd64
pkg: github.com/unrolled/render
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
(Before) BenchmarkHTML-12 500048 2170 ns/op 1386 B/op 20 allocs/op
(After) BenchmarkHTML-12 1909053 623.0 ns/op 1387 B/op 20 allocs/op
change
PASS
ok github.com/unrolled/render 2.366s Looking forward to the other PRs 👍 |
|
||
// If we are in development mode, recompile the templates on every HTML request. | ||
if r.opt.IsDevelopment { | ||
r.compileTemplates() | ||
} | ||
|
||
r.templatesLk.RLock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this lock/unlock necessary? Since all other locks just in compileTemplates ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes because this is using the templates. We can't replace the templates until they're not being used by anyone else.
As a second step improving unrolled#90, switch from using a RWMutex to use an atomic.Value and a reconstruct functions to ensure that the current templates are referenced in the helper func instead of a global reference. Signed-off-by: Andrew Thornton <[email protected]>
As a second step improving unrolled#90, only lock when absolutely necessary and reconstruct functions to ensure that the current templates are referenced in the helper func instead of a global reference. Closes unrolled#91 Signed-off-by: Andrew Thornton <[email protected]>
* Further improve locking (RWMutex version) As a second step improving #90, only lock when absolutely necessary and reconstruct functions to ensure that the current templates are referenced in the helper func instead of a global reference. Closes #91 Signed-off-by: Andrew Thornton <[email protected]> * Use fsnotify if using Directory and expose CompileTemplates If setting IsDevelopment, if we can, use an FsWatcher to recompile the templates in a separate goroutine. This should definitely increase the performance of the Development server. In order to make Asset based Renders have the same improvement - now that render compilation properly locks the templates we can expose the CompileTemplates function and allow downstream users to call this independently when their templates need recompilation. Contains #92 Signed-off-by: Andrew Thornton <[email protected]>
The template render lock means that template rendering in unrolled is single-threaded
and only one template can be rendered at a time by any project using unrolled. This is
clearly not ideal.
This PR suggests using a RWMutex instead which restores concurrent rendering to unrolled.
Signed-off-by: Andrew Thornton [email protected]