Repository navigation
Write unescaped runs in one call in escape_html - #1051
Merged
Merged
Conversation
escape_html is always called through a `&mut dyn Write`, so writing one byte at a time meant a dynamic call per byte of output. Write everything between two special characters in a single write_all instead.
Owner
|
Thanks |
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.
Without the
fast_escapefeature,escape_htmlwrote its output one byte at a time. Since it always gets a&mut dyn Write(it's stored asTera::escape_fn), that's a dynamicwrite_allcall for every byte of every autoescaped string. This change writes the plain runs between special characters in one call, so a string without anything to escape is now a single write.Output is the same. I added a small unit test for
escape_htmlthat covers the edge cases (empty input, specials at the start and end, adjacent specials, multibyte chars). It passes with and withoutfast_escape.Numbers from my machine (release build, default features):
{{ text }}, 128 KB string,.htmlescape_htmlvia fn pointer, 1 KB plain textescape_htmlvia fn pointer, 1 KB HTML-heavy textescape_htmlvia fn pointer, 60 B plain textThe existing
escapebench barely changes, because it callsescape_htmldirectly and the compiler inlines it and devirtualizes the writer. During a render the function is called through the pointer, which is what the rows above measure.