Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Avoid intermediate strings in some StringBuilder formatting #22111

Merged
merged 2 commits into from
Jan 21, 2019

Conversation

stephentoub
Copy link
Member

Several appends to string builders are first creating strings and then appending those strings, and they're not using the existing Append(primitive) overloads because they want to customize the format string or provider as part of appending the value. This fixes a few of those cases, using another internal AppendSpanFormattable overload on StringBuilder.

Example:

private DateTime _dt = new DateTime(2019, 1, 20, 7, 37, 59, DateTimeKind.Utc);
private char[] _buffer = new char[1024];
[Benchmark] public bool DTTryFormat() => _dt.TryFormat(_buffer, out int charsWritten);

allocates 64 bytes before and 0 after.

cc: @jkotas, @tarekgh

Several appends to string builders are first creating strings and then appending those strings, and they're not using the existing Append(primitive) overloads because they want to customize the format string or provider as part of appending the value.  This fixes a few of those cases, using another internal AppendSpanFormattable overload on StringBuilder.
Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM modulo one nit.

@stephentoub stephentoub merged commit 401e931 into dotnet:master Jan 21, 2019
@stephentoub stephentoub deleted the appendspan branch January 21, 2019 21:20
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…oreclr#22111)

* Avoid intermediate strings in some StringBuilder formatting

Several appends to string builders are first creating strings and then appending those strings, and they're not using the existing Append(primitive) overloads because they want to customize the format string or provider as part of appending the value.  This fixes a few of those cases, using another internal AppendSpanFormattable overload on StringBuilder.

* Address PR feedback


Commit migrated from dotnet/coreclr@401e931
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants