Repository navigation
Escape Text and Textf at render time - #357
Conversation
`Text` and `Textf` return a `text` node that escapes as it renders through `writeEscaped`, like attribute values do, instead of escaping into a new string at construction. A page built and rendered once per request saves one allocation per text node that needs escaping; a tree rendered many times escapes on every render instead of once. `escapeString` had no callers left, so it is gone, and the internal test and fuzz target now pin `writeEscaped` against `text/template` through both an `io.StringWriter` and a writer that only has `Write`. See #352, #356
|
@Hendrikto this is essentially the same as #352, what do you think? |
|
One note on the trade-off. The only case this makes slower is a tree built once and rendered many times, which now escapes its text on every render instead of once. That is the case #323 is about: a node rendered once into a string and written from then on would pay the escaping once and skip the tree walk too, so it would end up ahead of where eager escaping is today. Whatever shape that lands in, it is the natural companion to this change. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #357 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 6 6
Lines 681 685 +4
=========================================
+ Hits 681 685 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
We discussed this before, e.g. in #314, and back then, I came to the conclusion that the trade-off is likely not worth it. However, now with everything that changed in the meantime, the verdict may change. I will have a closer look. Off the top of my head, there are definitely advantages to this approach. We could even make Today I am pretty busy, but I will evaluate this branch when I find the time. |
|
Take your time, as always. :-) |
|
Note to self: update readme benchmark when/if this merges. |
|
I had a closer look, and it is really non-obvious whether this change makes sense. I get why you consulted me 😄 ResultsFirst of all, here are my benchmark results: benchmark resultsThese results were obtained as follows: Then I resorted the results such that all "construct and render" tests are grouped. In summary, we get
All results are extremely statistically significant, and the only exception to the pattern is +29.40% sec/op for ConsiderationsIn essence, this PR is hugely beneficial when constructing and rendering nodes, and enormously detrimental when rendering pre-built nodes. The question then becomes mainly how to weight those two use cases. On one hand, you could argue that "construct and render" is the default, and probably the most common case, so we should optimize for that. On the other hand, this is also the "performance is no concern" option. Constructing and rendering is likely what most people start out with, and is almost always good enough. When performance does become an issue, this is exactly when one would switch to "rendering pre-built" option. Does it make sense to optimize the case when performance is no concern and pessimize the performance-optimized use case? On the other hand, optimizing the default construct and render path even further would also decrease the probability of ever having to think about performance and switch away from the default case. From my and @markuswustenberg's experience, construct and render is used almost exclusively. For the rest of my reasoning, see this comment. RecommendationAs @markuswustenberg said that he wants to optimize for the construct and render path, I recommend this PR be merged, as it is definitely a huge improvement to that path, as confirmed by benchmarks across different machines and architectures. As that comes at the cost of decreased performance for the pre-built case, we might want to offer a fully static alternative, as discussed in #363. That would get us the best of both worlds: a performance-optimized default case, as well as a fully optimized static case for when even the optimized default becomes an issue. |
|
@Hendrikto thank you for your comment! I think it makes sense to take the absolute numbers into consideration as well. The pre-built docs still render in nanoseconds, but percentage-wise, it's a big difference, because the initial number is tiny. And yeah, memory allocation would be per-render, but fewer allocations in the default case. I agree that construct and render is probably the default, and I'd like to optimize for that. (In fact, I don't think I've ever done anything else in my apps.) I wouldn't call it "performance is no concern" though. For static content, I'm working on something like #363. But maybe there needs to be a better story for the case where someone would like to optimize by pre-building a component. Hmm. I'll think about it some more. |
In that case, that makes the analysis much simpler: If you want to optimize for the construct and render case, this PR is definitely an improvement. I did confirm with my own benchmarks on a different architecture.
Maybe I could have phrased this more clearly. What I meant is
So construct and render is the default, and you would never reach for anything else, unless performance becomes a concern. So maybe I should have called it the "you did not have to think about performance yet" case.
Something like that would have been my suggestion as well. I also use the construct and render path almost exclusively. If you have anything dynamic, you are almost forced to, unless you want to make really fine-grained components and cache just the static parts. If we have an optimized default construct and render path, and a fully static path, I guess the middle ground of pre-building nodes becomes mostly irrelevant. You would either use construct and render of fully static. That way, we make the default more performant, such that we have to think about performance even less, and if/when we do, we can switch to an even more optimized alternative. |
Excellent, thank you.
Yeah, true, and I think this is what most people do (and what all the examples etc. do). I think the design lends itself to that.
Yeah, exactly. Okay, that settles it, I'll merge this. Thank you again for your input, much appreciated! |
TextandTextfreturn atextnode that escapes as it renders throughwriteEscaped, the same path attribute values take since Avoid another allocation #352, instead of escaping into a new string at construction and rendering that asraw. Rendered bytes are unchanged.escapeStringhad no callers left and is removed. The internal test and fuzz target now pinwriteEscapedagainsttext/template.HTMLEscapeStringon the same inputs as before, through both astrings.Builderand a writer that only hasWrite, so both branches are held to the reference.TextandTextfkeep their allocations since the highlight uses short clean text, and the other rows are updated so the whole table comes from one run.textsatisfiesfmt.Stringerwith the escaped string, asrawdid forTextbefore, with a test for it; a second opinion caught that the first version had dropped it.The trade is the one #352 made for attributes. A page built and rendered once per request saves one allocation and the copy for every text node that needs escaping. A tree built once and rendered many times escapes on every render instead of once. A side effect in the first case's favour:
Textinside anIf(false, ...)is still constructed, since Go evaluates the argument, but no longer escapes a string nobody renders.Eager (main, with the
BenchmarkTexttable from #356) versus this branch, Apple M4, Go 1.27.1, interleaved, n=10.construct and renderis the per-request path:render pre-builtrows go from about 2 ns, one write of a ready string, to the full escaping cost on every render: 7.5 ns for short clean text, 128 ns for long text with little escaping, 239 ns for long text with much, onio.Discard. Writers withoutWriteStringalso gain one or two allocations per render on escaping rows.BenchmarkRealisticPage, which is what a request looks like:See #352, #356.