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 <art27@cantab.net>
|
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.366sLooking forward to the other PRs 👍 |
| r.compileTemplates() | ||
| } | ||
|
|
||
| r.templatesLk.RLock() |
There was a problem hiding this comment.
Is this lock/unlock necessary? Since all other locks just in compileTemplates ?
There was a problem hiding this comment.
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 <art27@cantab.net>
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 <art27@cantab.net>
* 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 <art27@cantab.net> * 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 <art27@cantab.net>
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 art27@cantab.net