Avoid ToString allocations for each ISpanFormattable value in string.Concat/Join(..., IEnumerable<T>)#86521
Merged
stephentoub merged 1 commit intodotnet:mainfrom May 22, 2023
Conversation
|
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsWe can use a T's ISpanFormattable implementation to avoid the individual ToStrings. We also don't need to maintain a separate implementation for Concat; at the cost of one branch per value, we can just reuse the Join implementation and pick up all of its optimizations for Concat. private IEnumerable<int> _intValues;
private IEnumerable<string> _stringValues;
private IEnumerable<object> _objectValues;
[GlobalSetup]
public void Setup()
{
var r = new Random(42);
_intValues = Enumerable.Range(0, 5).Select(_ => r.Next(int.MinValue, int.MaxValue)).ToList();
_stringValues = _intValues.Select(i => i.ToString()).ToList();
_objectValues = Enumerable.Range(0, 5).Select(i => new MyObj()).ToList();
}
[Benchmark] public string ConcatInt() => string.Concat(_intValues);
[Benchmark] public string ConcatString() => string.Concat<string>(_stringValues);
[Benchmark] public string JoinInt() => string.Join(", ", _intValues);
[Benchmark] public string JoinObject() => string.Join(", ", _objectValues);
private sealed class MyObj
{
public override string ToString() => "Something";
}
|
This was referenced May 19, 2023
…Concat/Join(..., IEnumerable<T>) We can use a T's ISpanFormattable implementation to avoid the individual ToStrings. We also don't need to maintain a separate implementation for Concat; at the cost of one branch per value, we can just reuse the Join implementation and pick up all of its optimizations for Concat.
d3255c1 to
28e66f8
Compare
tannergooding
approved these changes
May 22, 2023
This was referenced May 31, 2023
Closed
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
We can use a T's ISpanFormattable implementation to avoid the individual ToStrings. We also don't need to maintain a separate implementation for Concat; at the cost of one branch per value, we can just reuse the Join implementation and pick up all of its optimizations for Concat.