Skip to content

Commit 9e23679

Browse files
author
Javad
authored
Optimizing Eventsource memory allocations and removing an extra comment. (#684)
1 parent cff40d0 commit 9e23679

File tree

81 files changed

+1569
-2417
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1569
-2417
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static private void TraceException(string trace, Exception e)
3939
Debug.Assert(null != e, "TraceException: null Exception");
4040
if (null != e)
4141
{
42-
SqlClientEventSource.Log.TraceEvent(trace, e);
42+
SqlClientEventSource.Log.TryTraceEvent(trace, e);
4343
}
4444
}
4545

src/Microsoft.Data.SqlClient/netcore/src/Common/src/Microsoft/Data/ProviderBase/DbConnectionFactory.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ abstract public DbProviderFactory ProviderFactory
4949

5050
public void ClearAllPools()
5151
{
52-
long scopeID = SqlClientEventSource.Log.ScopeEnterEvent("<prov.DbConnectionFactory.ClearAllPools|API");
52+
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("<prov.DbConnectionFactory.ClearAllPools|API");
5353
try
5454
{
5555
Dictionary<DbConnectionPoolKey, DbConnectionPoolGroup> connectionPoolGroups = _connectionPoolGroups;
@@ -64,14 +64,14 @@ public void ClearAllPools()
6464
}
6565
finally
6666
{
67-
SqlClientEventSource.Log.ScopeLeaveEvent(scopeID);
67+
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
6868
}
6969
}
7070

7171
public void ClearPool(DbConnection connection)
7272
{
7373
ADP.CheckArgumentNull(connection, nameof(connection));
74-
long scopeID = SqlClientEventSource.Log.ScopeEnterEvent("<prov.DbConnectionFactory.ClearPool|API> {0}", GetObjectId(connection));
74+
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("<prov.DbConnectionFactory.ClearPool|API> {0}", GetObjectId(connection));
7575
try
7676
{
7777
DbConnectionPoolGroup poolGroup = GetConnectionPoolGroup(connection);
@@ -82,15 +82,15 @@ public void ClearPool(DbConnection connection)
8282
}
8383
finally
8484
{
85-
SqlClientEventSource.Log.ScopeLeaveEvent(scopeID);
85+
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
8686
}
8787
}
8888

8989
public void ClearPool(DbConnectionPoolKey key)
9090
{
9191
Debug.Assert(key != null, "key cannot be null");
9292
ADP.CheckArgumentNull(key.ConnectionString, nameof(key) + "." + nameof(key.ConnectionString));
93-
long scopeID = SqlClientEventSource.Log.ScopeEnterEvent("<prov.DbConnectionFactory.ClearPool|API> connectionString");
93+
long scopeID = SqlClientEventSource.Log.TryScopeEnterEvent("<prov.DbConnectionFactory.ClearPool|API> connectionString");
9494
try
9595
{
9696
DbConnectionPoolGroup poolGroup;
@@ -102,7 +102,7 @@ public void ClearPool(DbConnectionPoolKey key)
102102
}
103103
finally
104104
{
105-
SqlClientEventSource.Log.ScopeLeaveEvent(scopeID);
105+
SqlClientEventSource.Log.TryScopeLeaveEvent(scopeID);
106106
}
107107
}
108108

@@ -126,7 +126,7 @@ internal DbConnectionInternal CreateNonPooledConnection(DbConnection owningConne
126126
{
127127
newConnection.MakeNonPooledObject(owningConnection);
128128
}
129-
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionFactory.CreateNonPooledConnection|RES|CPOOL> {0}, Non-pooled database connection created.", ObjectID);
129+
SqlClientEventSource.Log.TryTraceEvent("<prov.DbConnectionFactory.CreateNonPooledConnection|RES|CPOOL> {0}, Non-pooled database connection created.", ObjectID);
130130
return newConnection;
131131
}
132132

@@ -140,7 +140,7 @@ internal DbConnectionInternal CreatePooledConnection(DbConnectionPool pool, DbCo
140140
{
141141
newConnection.MakePooledConnection(pool);
142142
}
143-
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionFactory.CreatePooledConnection|RES|CPOOL> {0}, Pooled database connection created.", ObjectID);
143+
SqlClientEventSource.Log.TryTraceEvent("<prov.DbConnectionFactory.CreatePooledConnection|RES|CPOOL> {0}, Pooled database connection created.", ObjectID);
144144
return newConnection;
145145
}
146146

@@ -195,7 +195,7 @@ private DbConnectionPool GetConnectionPool(DbConnection owningObject, DbConnecti
195195
// however, don't rebuild connectionOptions if no pooling is involved - let new connections do that work
196196
if (connectionPoolGroup.IsDisabled && (null != connectionPoolGroup.PoolGroupOptions))
197197
{
198-
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionFactory.GetConnectionPool|RES|INFO|CPOOL> {0}, DisabledPoolGroup={1}", ObjectID, connectionPoolGroup.ObjectID);
198+
SqlClientEventSource.Log.TryTraceEvent("<prov.DbConnectionFactory.GetConnectionPool|RES|INFO|CPOOL> {0}, DisabledPoolGroup={1}", ObjectID, connectionPoolGroup?.ObjectID);
199199

200200
// reusing existing pool option in case user originally used SetConnectionPoolOptions
201201
DbConnectionPoolGroupOptions poolOptions = connectionPoolGroup.PoolGroupOptions;
@@ -303,7 +303,7 @@ internal DbConnectionPoolGroup GetConnectionPoolGroup(DbConnectionPoolKey key, D
303303
private void PruneConnectionPoolGroups(object state)
304304
{
305305
// when debugging this method, expect multiple threads at the same time
306-
SqlClientEventSource.Log.AdvancedTraceEvent("<prov.DbConnectionFactory.PruneConnectionPoolGroups|RES|INFO|CPOOL> {0}", ObjectID);
306+
SqlClientEventSource.Log.TryAdvancedTraceEvent("<prov.DbConnectionFactory.PruneConnectionPoolGroups|RES|INFO|CPOOL> {0}", ObjectID);
307307

308308
// First, walk the pool release list and attempt to clear each
309309
// pool, when the pool is finally empty, we dispose of it. If the
@@ -323,7 +323,7 @@ private void PruneConnectionPoolGroups(object state)
323323
if (0 == pool.Count)
324324
{
325325
_poolsToRelease.Remove(pool);
326-
SqlClientEventSource.Log.AdvancedTraceEvent("<prov.DbConnectionFactory.PruneConnectionPoolGroups|RES|INFO|CPOOL> {0}, ReleasePool={1}", ObjectID, pool.ObjectID);
326+
SqlClientEventSource.Log.TryAdvancedTraceEvent("<prov.DbConnectionFactory.PruneConnectionPoolGroups|RES|INFO|CPOOL> {0}, ReleasePool={1}", ObjectID, pool.ObjectID);
327327
}
328328
}
329329
}
@@ -347,7 +347,7 @@ private void PruneConnectionPoolGroups(object state)
347347
if (0 == poolsLeft)
348348
{
349349
_poolGroupsToRelease.Remove(poolGroup);
350-
SqlClientEventSource.Log.AdvancedTraceEvent("<prov.DbConnectionFactory.PruneConnectionPoolGroups|RES|INFO|CPOOL> {0}, ReleasePoolGroup={1}", ObjectID, poolGroup.ObjectID);
350+
SqlClientEventSource.Log.TryAdvancedTraceEvent("<prov.DbConnectionFactory.PruneConnectionPoolGroups|RES|INFO|CPOOL> {0}, ReleasePoolGroup={1}", ObjectID, poolGroup.ObjectID);
351351
}
352352
}
353353
}
@@ -410,7 +410,7 @@ internal void QueuePoolForRelease(DbConnectionPool pool, bool clearing)
410410
internal void QueuePoolGroupForRelease(DbConnectionPoolGroup poolGroup)
411411
{
412412
Debug.Assert(null != poolGroup, "null poolGroup?");
413-
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionFactory.QueuePoolGroupForRelease|RES|INFO|CPOOL> {0}, poolGroup={1}", ObjectID, poolGroup.ObjectID);
413+
SqlClientEventSource.Log.TryTraceEvent("<prov.DbConnectionFactory.QueuePoolGroupForRelease|RES|INFO|CPOOL> {0}, poolGroup={1}", ObjectID, poolGroup.ObjectID);
414414

415415
lock (_poolGroupsToRelease)
416416
{

src/Microsoft.Data.SqlClient/netcore/src/Common/src/Microsoft/Data/ProviderBase/DbConnectionInternal.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ internal void DeactivateConnection()
225225
{
226226
// Internal method called from the connection pooler so we don't expose
227227
// the Deactivate method publicly.
228-
SqlClientEventSource.Log.PoolerTraceEvent("<prov.DbConnectionInternal.DeactivateConnection|RES|INFO|CPOOL> {0}, Deactivating", ObjectID);
228+
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionInternal.DeactivateConnection|RES|INFO|CPOOL> {0}, Deactivating", ObjectID);
229229

230230
#if DEBUG
231231
int activateCount = Interlocked.Decrement(ref _activateCount);
@@ -249,14 +249,14 @@ internal void DeactivateConnection()
249249
protected internal void DoNotPoolThisConnection()
250250
{
251251
_cannotBePooled = true;
252-
SqlClientEventSource.Log.PoolerTraceEvent("<prov.DbConnectionInternal.DoNotPoolThisConnection|RES|INFO|CPOOL> {0}, Marking pooled object as non-poolable so it will be disposed", ObjectID);
252+
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionInternal.DoNotPoolThisConnection|RES|INFO|CPOOL> {0}, Marking pooled object as non-poolable so it will be disposed", ObjectID);
253253
}
254254

255255
/// <devdoc>Ensure that this connection cannot be put back into the pool.</devdoc>
256256
protected internal void DoomThisConnection()
257257
{
258258
_connectionIsDoomed = true;
259-
SqlClientEventSource.Log.PoolerTraceEvent("<prov.DbConnectionInternal.DoomThisConnection|RES|INFO|CPOOL> {0}, Dooming", ObjectID);
259+
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionInternal.DoomThisConnection|RES|INFO|CPOOL> {0}, Dooming", ObjectID);
260260
}
261261

262262
// Reset connection doomed status so it can be re-connected and pooled.
@@ -383,7 +383,7 @@ internal void PrePush(object expectedOwner)
383383
throw ADP.InternalError(ADP.InternalErrorCode.PushingObjectSecondTime); // pushing object onto stack a second time
384384
}
385385

386-
SqlClientEventSource.Log.PoolerTraceEvent("<prov.DbConnectionInternal.PrePush|RES|CPOOL> {0}, Preparing to push into pool, owning connection {1}, pooledCount={2}", ObjectID, 0, _pooledCount);
386+
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionInternal.PrePush|RES|CPOOL> {0}, Preparing to push into pool, owning connection {1}, pooledCount={2}", ObjectID, 0, _pooledCount);
387387
_pooledCount++;
388388
_owningObject.Target = null; // NOTE: doing this and checking for InternalError.PooledObjectHasOwner degrades the close by 2%
389389
}
@@ -412,7 +412,7 @@ internal void PostPop(object newOwner)
412412
}
413413
_owningObject.Target = newOwner;
414414
_pooledCount--;
415-
SqlClientEventSource.Log.PoolerTraceEvent("<prov.DbConnectionInternal.PostPop|RES|CPOOL> {0}, Preparing to pop from pool, owning connection {1}, pooledCount={2}", ObjectID, 0, _pooledCount);
415+
SqlClientEventSource.Log.TryPoolerTraceEvent("<prov.DbConnectionInternal.PostPop|RES|CPOOL> {0}, Preparing to pop from pool, owning connection {1}, pooledCount={2}", ObjectID, 0, _pooledCount);
416416

417417
//3 // The following tests are retail assertions of things we can't allow to happen.
418418
if (null != Pool)

src/Microsoft.Data.SqlClient/netcore/src/Common/src/Microsoft/Data/ProviderBase/DbConnectionPoolGroup.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ private bool MarkPoolGroupAsActive()
229229
if (PoolGroupStateIdle == _state)
230230
{
231231
_state = PoolGroupStateActive;
232-
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionPoolGroup.ClearInternal|RES|INFO|CPOOL> {0}, Active", ObjectID);
232+
SqlClientEventSource.Log.TryTraceEvent("<prov.DbConnectionPoolGroup.ClearInternal|RES|INFO|CPOOL> {0}, Active", ObjectID);
233233
}
234234
return (PoolGroupStateActive == _state);
235235
}
@@ -281,12 +281,12 @@ internal bool Prune()
281281
if (PoolGroupStateActive == _state)
282282
{
283283
_state = PoolGroupStateIdle;
284-
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionPoolGroup.ClearInternal|RES|INFO|CPOOL> {0}, Idle", ObjectID);
284+
SqlClientEventSource.Log.TryTraceEvent("<prov.DbConnectionPoolGroup.ClearInternal|RES|INFO|CPOOL> {0}, Idle", ObjectID);
285285
}
286286
else if (PoolGroupStateIdle == _state)
287287
{
288288
_state = PoolGroupStateDisabled;
289-
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionPoolGroup.ReadyToRemove|RES|INFO|CPOOL> {0}, Disabled", ObjectID);
289+
SqlClientEventSource.Log.TryTraceEvent("<prov.DbConnectionPoolGroup.ReadyToRemove|RES|INFO|CPOOL> {0}, Disabled", ObjectID);
290290
}
291291
}
292292
return (PoolGroupStateDisabled == _state);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ static private void TraceException(
3131
Debug.Assert(null != e, "TraceException: null Exception");
3232
if (null != e)
3333
{
34-
SqlClientEventSource.Log.AdvancedTraceEvent(trace, e.Message);
35-
SqlClientEventSource.Log.AdvancedTraceEvent("<comm.ADP.TraceException|ERR|ADV> Environment StackTrace = '{0}'", Environment.StackTrace);
34+
SqlClientEventSource.Log.TryAdvancedTraceEvent(trace, e.Message);
35+
SqlClientEventSource.Log.TryAdvancedTraceEvent("<comm.ADP.TraceException|ERR|ADV> Environment StackTrace = '{0}'", Environment.StackTrace);
3636
}
3737
}
3838

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/ProviderBase/DbConnectionFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ internal bool TryGetConnection(DbConnection owningConnection, TaskCompletionSour
129129
// connection creation failed on semaphore waiting or if max pool reached
130130
if (connectionPool.IsRunning)
131131
{
132-
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionFactory.GetConnection|RES|CPOOL> {0}, GetConnection failed because a pool timeout occurred.", ObjectID);
132+
SqlClientEventSource.Log.TryTraceEvent("<prov.DbConnectionFactory.GetConnection|RES|CPOOL> {0}, GetConnection failed because a pool timeout occurred.", ObjectID);
133133
// If GetConnection failed while the pool is running, the pool timeout occurred.
134134
throw ADP.PooledOpenTimeout();
135135
}
@@ -147,7 +147,7 @@ internal bool TryGetConnection(DbConnection owningConnection, TaskCompletionSour
147147

148148
if (connection == null)
149149
{
150-
SqlClientEventSource.Log.TraceEvent("<prov.DbConnectionFactory.GetConnection|RES|CPOOL> {0}, GetConnection failed because a pool timeout occurred and all retries were exhausted.", ObjectID);
150+
SqlClientEventSource.Log.TryTraceEvent("<prov.DbConnectionFactory.GetConnection|RES|CPOOL> {0}, GetConnection failed because a pool timeout occurred and all retries were exhausted.", ObjectID);
151151
// exhausted all retries or timed out - give up
152152
throw ADP.PooledOpenTimeout();
153153
}

0 commit comments

Comments
 (0)