Skip to content

Commit 2874114

Browse files
Perf: Reduce tracing related allocations (NetFx) (#483)
1 parent 6facb43 commit 2874114

File tree

2 files changed

+27
-44
lines changed

2 files changed

+27
-44
lines changed

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

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,15 @@ namespace Microsoft.Data.Common
1515
internal static class ActivityCorrelator
1616
{
1717

18-
internal class ActivityId
18+
internal sealed class ActivityId
1919
{
20-
internal Guid Id { get; private set; }
21-
internal UInt32 Sequence { get; private set; }
20+
internal readonly Guid Id;
21+
internal readonly uint Sequence;
2222

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

4429
public override string ToString()
@@ -51,7 +36,7 @@ public override string ToString()
5136
// The Sequence number will be incremented when each event happens.
5237
// Correlation along threads is consistent with the current XEvent mechanisam at server.
5338
[ThreadStaticAttribute]
54-
static ActivityId tlsActivity;
39+
static ActivityId t_tlsActivity;
5540

5641
/// <summary>
5742
/// Get the current ActivityId
@@ -60,12 +45,12 @@ internal static ActivityId Current
6045
{
6146
get
6247
{
63-
if (tlsActivity == null)
48+
if (t_tlsActivity == null)
6449
{
65-
tlsActivity = new ActivityId();
50+
t_tlsActivity = new ActivityId(1);
6651
}
6752

68-
return new ActivityId(tlsActivity);
53+
return t_tlsActivity;
6954
}
7055
}
7156

@@ -75,14 +60,9 @@ internal static ActivityId Current
7560
/// <returns>ActivityId</returns>
7661
internal static ActivityId Next()
7762
{
78-
if (tlsActivity == null)
79-
{
80-
tlsActivity = new ActivityId();
81-
}
82-
83-
tlsActivity.Increment();
84-
85-
return new ActivityId(tlsActivity);
63+
t_tlsActivity = new ActivityId(t_tlsActivity?.Sequence ?? 0);
64+
65+
return t_tlsActivity;
8666
}
8767
}
8868
}

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,10 @@ internal void Deactivate(bool connectionIsDoomed)
13141314
// Called when the connection that owns us is deactivated.
13151315

13161316
SqlClientEventSource.Log.AdvanceTrace("<sc.TdsParser.Deactivate|ADV> {0}# deactivating", ObjectID);
1317-
SqlClientEventSource.Log.StateDumpEvent("<sc.TdsParser.Deactivate|STATE> {0}#, {1}", ObjectID, TraceString());
1317+
if (SqlClientEventSource.Log.IsStateDumpEnabled())
1318+
{
1319+
SqlClientEventSource.Log.StateDumpEvent("<sc.TdsParser.Deactivate|STATE> {0}#, {1}", ObjectID, TraceString());
1320+
}
13181321

13191322
if (MARSOn)
13201323
{
@@ -13502,13 +13505,13 @@ internal ulong PlpBytesTotalLength(TdsParserStateObject stateObj)
1350213505
;
1350313506
internal string TraceString()
1350413507
{
13505-
return String.Format(/*IFormatProvider*/ null,
13508+
return string.Format(/*IFormatProvider*/ null,
1350613509
StateTraceFormatString,
13507-
null == _physicalStateObj,
13508-
null == _pMarsPhysicalConObj,
13510+
null == _physicalStateObj ? bool.TrueString : bool.FalseString,
13511+
null == _pMarsPhysicalConObj ? bool.TrueString : bool.FalseString,
1350913512
_state,
1351013513
_server,
13511-
_fResetConnection,
13514+
_fResetConnection ? bool.TrueString : bool.FalseString,
1351213515
null == _defaultCollation ? "(null)" : _defaultCollation.TraceString(),
1351313516
_defaultCodePage,
1351413517
_defaultLCID,
@@ -13520,19 +13523,19 @@ internal string TraceString()
1352013523
_retainedTransactionId,
1352113524
_nonTransactedOpenResultCount,
1352213525
null == _connHandler ? "(null)" : _connHandler.ObjectID.ToString((IFormatProvider)null),
13523-
_fMARS,
13526+
_fMARS ? bool.TrueString : bool.FalseString,
1352413527
null == _sessionPool ? "(null)" : _sessionPool.TraceString(),
13525-
_isShiloh,
13526-
_isShilohSP1,
13527-
_isYukon,
13528+
_isShiloh ? bool.TrueString : bool.FalseString,
13529+
_isShilohSP1 ? bool.TrueString : bool.FalseString,
13530+
_isYukon ? bool.TrueString : bool.FalseString,
1352813531
null == _sniSpnBuffer ? "(null)" : _sniSpnBuffer.Length.ToString((IFormatProvider)null),
1352913532
_physicalStateObj != null ? "(null)" : _physicalStateObj.ErrorCount.ToString((IFormatProvider)null),
1353013533
_physicalStateObj != null ? "(null)" : _physicalStateObj.WarningCount.ToString((IFormatProvider)null),
1353113534
_physicalStateObj != null ? "(null)" : _physicalStateObj.PreAttentionErrorCount.ToString((IFormatProvider)null),
1353213535
_physicalStateObj != null ? "(null)" : _physicalStateObj.PreAttentionWarningCount.ToString((IFormatProvider)null),
13533-
null == _statistics,
13534-
_statisticsIsInTransaction,
13535-
_fPreserveTransaction,
13536+
null == _statistics ? bool.TrueString : bool.FalseString,
13537+
_statisticsIsInTransaction ? bool.TrueString : bool.FalseString,
13538+
_fPreserveTransaction ? bool.TrueString : bool.FalseString,
1353613539
null == _connHandler ? "(null)" : _connHandler.ConnectionOptions.MultiSubnetFailover.ToString((IFormatProvider)null),
1353713540
null == _connHandler ? "(null)" : _connHandler.ConnectionOptions.TransparentNetworkIPResolution.ToString((IFormatProvider)null));
1353813541
}

0 commit comments

Comments
 (0)