Skip to content
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

Merged
merged 1 commit into from
May 12, 2021

Conversation

zeripath
Copy link
Contributor

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]

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]>
@zeripath
Copy link
Contributor Author

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

@unrolled
Copy link
Owner

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 👍

@unrolled unrolled merged commit 8d2b52a into unrolled:v1 May 12, 2021

// If we are in development mode, recompile the templates on every HTML request.
if r.opt.IsDevelopment {
r.compileTemplates()
}

r.templatesLk.RLock()
Copy link
Contributor

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 ?

Copy link
Contributor Author

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.

@zeripath zeripath deleted the concurrent-render branch May 12, 2021 07:50
zeripath added a commit to zeripath/render that referenced this pull request May 12, 2021
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]>
zeripath added a commit to zeripath/render that referenced this pull request May 12, 2021
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]>
unrolled pushed a commit that referenced this pull request May 26, 2021
* 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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants