Skip to content

Commit

Permalink
Perf: Reduce tracing related allocations (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wraith2 authored Mar 23, 2020
1 parent 29b41d2 commit d23c644
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,15 @@ namespace Microsoft.Data.Common

internal static class ActivityCorrelator
{
internal class ActivityId
internal sealed class ActivityId
{
internal Guid Id { get; private set; }
internal uint Sequence { get; private set; }
internal readonly Guid Id;
internal readonly uint Sequence;

internal ActivityId()
internal ActivityId(uint sequence)
{
this.Id = Guid.NewGuid();
this.Sequence = 0; // the first event will start 1
}

// copy-constructor
internal ActivityId(ActivityId activity)
{
this.Id = activity.Id;
this.Sequence = activity.Sequence;
}

internal void Increment()
{
unchecked
{
++this.Sequence;
}
this.Sequence = sequence;
}

public override string ToString()
Expand All @@ -61,10 +46,9 @@ internal static ActivityId Current
{
if (t_tlsActivity == null)
{
t_tlsActivity = new ActivityId();
t_tlsActivity = new ActivityId(1);
}

return new ActivityId(t_tlsActivity);
return t_tlsActivity;
}
}

Expand All @@ -74,14 +58,9 @@ internal static ActivityId Current
/// <returns>ActivityId</returns>
internal static ActivityId Next()
{
if (t_tlsActivity == null)
{
t_tlsActivity = new ActivityId();
}

t_tlsActivity.Increment();
t_tlsActivity = new ActivityId( (t_tlsActivity?.Sequence ?? 0) + 1);

return new ActivityId(t_tlsActivity);
return t_tlsActivity;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,10 @@ internal void Deactivate(bool connectionIsDoomed)
{
// Called when the connection that owns us is deactivated.
SqlClientEventSource.Log.AdvanceTrace("<sc.TdsParser.Deactivate|ADV> {0}# deactivating", ObjectID);
SqlClientEventSource.Log.StateDumpEvent("<sc.TdsParser.Deactivate|STATE> {0}# {1}", ObjectID, TraceString());
if (SqlClientEventSource.Log.IsStateDumpEnabled())
{
SqlClientEventSource.Log.StateDumpEvent("<sc.TdsParser.Deactivate|STATE> {0}# {1}", ObjectID, TraceString());
}

if (MARSOn)
{
Expand Down Expand Up @@ -12631,13 +12634,13 @@ private bool TryProcessUDTMetaData(SqlMetaDataPriv metaData, TdsParserStateObjec
;
internal string TraceString()
{
return String.Format(/*IFormatProvider*/ null,
return string.Format(/*IFormatProvider*/ null,
StateTraceFormatString,
null == _physicalStateObj,
null == _pMarsPhysicalConObj,
null == _physicalStateObj ? bool.TrueString : bool.FalseString,
null == _pMarsPhysicalConObj ? bool.TrueString : bool.FalseString,
_state,
_server,
_fResetConnection,
_fResetConnection ? bool.TrueString : bool.FalseString,
null == _defaultCollation ? "(null)" : _defaultCollation.TraceString(),
_defaultCodePage,
_defaultLCID,
Expand All @@ -12649,17 +12652,17 @@ internal string TraceString()
_retainedTransactionId,
_nonTransactedOpenResultCount,
null == _connHandler ? "(null)" : _connHandler.ObjectID.ToString((IFormatProvider)null),
_fMARS,
_fMARS ? bool.TrueString : bool.FalseString,
null == _sessionPool ? "(null)" : _sessionPool.TraceString(),
_isYukon,
_isYukon ? bool.TrueString : bool.FalseString,
null == _sniSpnBuffer ? "(null)" : _sniSpnBuffer.Length.ToString((IFormatProvider)null),
_physicalStateObj != null ? "(null)" : _physicalStateObj.ErrorCount.ToString((IFormatProvider)null),
_physicalStateObj != null ? "(null)" : _physicalStateObj.WarningCount.ToString((IFormatProvider)null),
_physicalStateObj != null ? "(null)" : _physicalStateObj.PreAttentionErrorCount.ToString((IFormatProvider)null),
_physicalStateObj != null ? "(null)" : _physicalStateObj.PreAttentionWarningCount.ToString((IFormatProvider)null),
null == _statistics,
_statisticsIsInTransaction,
_fPreserveTransaction,
null == _statistics ? bool.TrueString : bool.FalseString,
_statisticsIsInTransaction ? bool.TrueString : bool.FalseString,
_fPreserveTransaction ? bool.TrueString : bool.FalseString,
null == _connHandler ? "(null)" : _connHandler.ConnectionOptions.MultiSubnetFailover.ToString((IFormatProvider)null));
}

Expand Down

0 comments on commit d23c644

Please sign in to comment.