Skip to content

Commit d23c644

Browse files
authored
Perf: Reduce tracing related allocations (#479)
1 parent 29b41d2 commit d23c644

File tree

2 files changed

+22
-40
lines changed

2 files changed

+22
-40
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/Common/ActivityCorrelator.cs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,15 @@ namespace Microsoft.Data.Common
1414

1515
internal static class ActivityCorrelator
1616
{
17-
internal class ActivityId
17+
internal sealed class ActivityId
1818
{
19-
internal Guid Id { get; private set; }
20-
internal uint Sequence { get; private set; }
19+
internal readonly Guid Id;
20+
internal readonly uint Sequence;
2121

22-
internal ActivityId()
22+
internal ActivityId(uint sequence)
2323
{
2424
this.Id = Guid.NewGuid();
25-
this.Sequence = 0; // the first event will start 1
26-
}
27-
28-
// copy-constructor
29-
internal ActivityId(ActivityId activity)
30-
{
31-
this.Id = activity.Id;
32-
this.Sequence = activity.Sequence;
33-
}
34-
35-
internal void Increment()
36-
{
37-
unchecked
38-
{
39-
++this.Sequence;
40-
}
25+
this.Sequence = sequence;
4126
}
4227

4328
public override string ToString()
@@ -61,10 +46,9 @@ internal static ActivityId Current
6146
{
6247
if (t_tlsActivity == null)
6348
{
64-
t_tlsActivity = new ActivityId();
49+
t_tlsActivity = new ActivityId(1);
6550
}
66-
67-
return new ActivityId(t_tlsActivity);
51+
return t_tlsActivity;
6852
}
6953
}
7054

@@ -74,14 +58,9 @@ internal static ActivityId Current
7458
/// <returns>ActivityId</returns>
7559
internal static ActivityId Next()
7660
{
77-
if (t_tlsActivity == null)
78-
{
79-
t_tlsActivity = new ActivityId();
80-
}
81-
82-
t_tlsActivity.Increment();
61+
t_tlsActivity = new ActivityId( (t_tlsActivity?.Sequence ?? 0) + 1);
8362

84-
return new ActivityId(t_tlsActivity);
63+
return t_tlsActivity;
8564
}
8665
}
8766
}

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,10 @@ internal void Deactivate(bool connectionIsDoomed)
979979
{
980980
// Called when the connection that owns us is deactivated.
981981
SqlClientEventSource.Log.AdvanceTrace("<sc.TdsParser.Deactivate|ADV> {0}# deactivating", ObjectID);
982-
SqlClientEventSource.Log.StateDumpEvent("<sc.TdsParser.Deactivate|STATE> {0}# {1}", ObjectID, TraceString());
982+
if (SqlClientEventSource.Log.IsStateDumpEnabled())
983+
{
984+
SqlClientEventSource.Log.StateDumpEvent("<sc.TdsParser.Deactivate|STATE> {0}# {1}", ObjectID, TraceString());
985+
}
983986

984987
if (MARSOn)
985988
{
@@ -12631,13 +12634,13 @@ private bool TryProcessUDTMetaData(SqlMetaDataPriv metaData, TdsParserStateObjec
1263112634
;
1263212635
internal string TraceString()
1263312636
{
12634-
return String.Format(/*IFormatProvider*/ null,
12637+
return string.Format(/*IFormatProvider*/ null,
1263512638
StateTraceFormatString,
12636-
null == _physicalStateObj,
12637-
null == _pMarsPhysicalConObj,
12639+
null == _physicalStateObj ? bool.TrueString : bool.FalseString,
12640+
null == _pMarsPhysicalConObj ? bool.TrueString : bool.FalseString,
1263812641
_state,
1263912642
_server,
12640-
_fResetConnection,
12643+
_fResetConnection ? bool.TrueString : bool.FalseString,
1264112644
null == _defaultCollation ? "(null)" : _defaultCollation.TraceString(),
1264212645
_defaultCodePage,
1264312646
_defaultLCID,
@@ -12649,17 +12652,17 @@ internal string TraceString()
1264912652
_retainedTransactionId,
1265012653
_nonTransactedOpenResultCount,
1265112654
null == _connHandler ? "(null)" : _connHandler.ObjectID.ToString((IFormatProvider)null),
12652-
_fMARS,
12655+
_fMARS ? bool.TrueString : bool.FalseString,
1265312656
null == _sessionPool ? "(null)" : _sessionPool.TraceString(),
12654-
_isYukon,
12657+
_isYukon ? bool.TrueString : bool.FalseString,
1265512658
null == _sniSpnBuffer ? "(null)" : _sniSpnBuffer.Length.ToString((IFormatProvider)null),
1265612659
_physicalStateObj != null ? "(null)" : _physicalStateObj.ErrorCount.ToString((IFormatProvider)null),
1265712660
_physicalStateObj != null ? "(null)" : _physicalStateObj.WarningCount.ToString((IFormatProvider)null),
1265812661
_physicalStateObj != null ? "(null)" : _physicalStateObj.PreAttentionErrorCount.ToString((IFormatProvider)null),
1265912662
_physicalStateObj != null ? "(null)" : _physicalStateObj.PreAttentionWarningCount.ToString((IFormatProvider)null),
12660-
null == _statistics,
12661-
_statisticsIsInTransaction,
12662-
_fPreserveTransaction,
12663+
null == _statistics ? bool.TrueString : bool.FalseString,
12664+
_statisticsIsInTransaction ? bool.TrueString : bool.FalseString,
12665+
_fPreserveTransaction ? bool.TrueString : bool.FalseString,
1266312666
null == _connHandler ? "(null)" : _connHandler.ConnectionOptions.MultiSubnetFailover.ToString((IFormatProvider)null));
1266412667
}
1266512668

0 commit comments

Comments
 (0)