{{ message }}
Don't use an intermediate buffer when rendering templates#2863
Open
erdnaxeli wants to merge 1 commit into
Open
Don't use an intermediate buffer when rendering templates#2863erdnaxeli wants to merge 1 commit into
erdnaxeli wants to merge 1 commit into
Conversation
Contributor
Contributor
|
I looked into this a little bit. Seems that buffering is needed if we want to error handling like Echo has.
|
Contributor
|
note: this is for example different from Go standard library JSON encoding/marshalling which uses buffer internally and does not write before encoding is done. |
Contributor
|
So I am leaning into not changing this part and if it is absolutely needed ... custom function is the workaround (simplified) func Render(c *echo.Context, statusCode int, templateName string, data any) (err error) {
c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8)
c.Response().WriteHeader(statusCode)
return c.Echo().Renderer.Render(c, c.Response(), templateName, data)
}and use return Render(c, http.StatusOK, "index.html", map[string]string{"csrfToken": csrfToken}) |
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

I noticed that the context does not offer methods to write to the
http.ResponseWriterwithout using intermediate buffers. We can still access theResponseWriterbut then we need to callWriteHeadermanually, and do not forget to add the content type, which are handled by echo otherwise.So I added two methods,
BlobWriteandHTMLWritewhich instead of[]bytetake as parameter a functionfunc (io.Writer) error. I also added changed the methodRenderto useHTMLWrite, thus avoiding the intermediate buffer and writing directly to theResponseWriterobject.I wrote a benchmark for the
Rendermethod, here are the results without and with my change:Note that for bigger templates (like a real full HTML page) the difference would probably be even bigger.
The only downside of this is that when the
Renderfunction of theRendererobject is called, the headers have already been written to theResponseWriter. If the caller would change the behavior if the template rendering fails (like redirect to another page), it can't. Maybe there should be two methods: one that renders first (with an intermediate buffer), and another one that assume there will be no error and is more performant (without the intermediate buffer).And I am not a big fan of the naming I used, but I could not find something better.
Let me know what you think of that :)