Optimize StringBuilder.as_string
by constructing the string in-place and skipping unnecessary checks.
#100295
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.
Originally authored by @YYF233333 in #97777.
I'm extracting the
StringBuilder
part as it's semantically different from the rest of the PR.Benchmark
I have benchmarked the following code:
This yields:
487ms
on master (a40fc23)68ms
on this PR (26ac632e364e504b34821674b102a0f7a21112c0)Therefore, this PR can be expected to speed up uses of
StringBuilder
by7x
in extreme cases.Discussion
The reason for this speed up can be separated as follows:
StringBuilder
was callingString(const char *ptr, int clip_to)
_strlen_clipped
, to find the length of the string even though it was known.char *
is always a correct string, andchar32_t *
were taken fromString
where they are known to be correct). Therefore, the check can be skipped.