Faster to_strict_string
, accepts
, and slightly faster to_functional_string
#1033
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 followed the pattern in
to_functional_string
withto_strict_string
, and the initial version was actually slower on average than the original that constructs a ton of temporary strings.I was puzzled by this, but then realized that Rust was not able to turn the
CType::Infer
values I was creating at the beginning of the loop into constants, and was actually constructing those strings on each call. Because the number of these quasi-constant strings needed forto_strict_string
was much higher, this pushed the average number of string allocations up instead of down.So I used
OnceLock
to lazily create a singleton for each of these quasi-constants and that gave me the speedup, and I re-used this into_functional_string
to make that slightly faster, as well.While rewriting
to_strict_string
I realized that theaccepts
method, which was the actual largest bottleneck (via usingto_strict_string
mostly), didn't need to call thedegroup
method because the string output would be identical either way, so I dropped that and got a further performance boost. Now theHello, World
example program takes just 2 seconds to compile on my laptop, and running it in Firefox is now faster than what the native compiler was a couple of days ago at just under 3 seconds (while Chrome is sitting at about 5.5 seconds).