Skip to content

Commit

Permalink
Reduce allocation in Activity.{Parent}Id (#86685)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed May 24, 2023
1 parent 69c9421 commit dafc07d
Showing 1 changed file with 12 additions and 3 deletions.
Expand Up @@ -225,8 +225,12 @@ public string DisplayName
// Convert flags to binary.
Span<char> flagsChars = stackalloc char[2];
HexConverter.ToCharsBuffer((byte)((~ActivityTraceFlagsIsSet) & _w3CIdFlags), flagsChars, 0, HexConverter.Casing.Lower);
string id = "00-" + _traceId + "-" + _spanId + "-" + flagsChars.ToString();

string id =
#if NET6_0_OR_GREATER
string.Create(null, stackalloc char[128], $"00-{_traceId}-{_spanId}-{flagsChars}");
#else
"00-" + _traceId + "-" + _spanId + "-" + flagsChars.ToString();
#endif
Interlocked.CompareExchange(ref _id, id, null);

}
Expand All @@ -253,7 +257,12 @@ public string DisplayName
{
Span<char> flagsChars = stackalloc char[2];
HexConverter.ToCharsBuffer((byte)((~ActivityTraceFlagsIsSet) & _parentTraceFlags), flagsChars, 0, HexConverter.Casing.Lower);
string parentId = "00-" + _traceId + "-" + _parentSpanId + "-" + flagsChars.ToString();
string parentId =
#if NET6_0_OR_GREATER
string.Create(null, stackalloc char[128], $"00-{_traceId}-{_parentSpanId}-{flagsChars}");
#else
"00-" + _traceId + "-" + _parentSpanId + "-" + flagsChars.ToString();
#endif
Interlocked.CompareExchange(ref _parentId, parentId, null);
}
else if (Parent != null)
Expand Down

0 comments on commit dafc07d

Please sign in to comment.