Skip to content

Commit ab95f6f

Browse files
authored
remove duplicate dictionary lookups (#4000)
1 parent 59afd23 commit ab95f6f

File tree

5 files changed

+30
-56
lines changed

5 files changed

+30
-56
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/Sql/SqlDataSourceEnumeratorManagedHelper.netcore.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ private static DataTable ParseServerEnumString(string serverInstances)
6161
if (InstanceDetails.Count > 0)
6262
{
6363
dataRow = dataTable.NewRow();
64-
dataRow[0] = InstanceDetails.ContainsKey(SqlDataSourceEnumeratorUtil.ServerNameCol) == true ?
65-
InstanceDetails[SqlDataSourceEnumeratorUtil.ServerNameCol] : string.Empty;
66-
dataRow[1] = InstanceDetails.ContainsKey(SqlDataSourceEnumeratorUtil.InstanceNameCol) == true ?
67-
InstanceDetails[SqlDataSourceEnumeratorUtil.InstanceNameCol] : string.Empty;
68-
dataRow[2] = InstanceDetails.ContainsKey(SqlDataSourceEnumeratorUtil.IsClusteredCol) == true ?
69-
InstanceDetails[SqlDataSourceEnumeratorUtil.IsClusteredCol] : string.Empty;
70-
dataRow[3] = InstanceDetails.ContainsKey(SqlDataSourceEnumeratorUtil.VersionNameCol) == true ?
71-
InstanceDetails[SqlDataSourceEnumeratorUtil.VersionNameCol] : string.Empty;
64+
dataRow[0] = InstanceDetails.TryGetValue(SqlDataSourceEnumeratorUtil.ServerNameCol, out string serverName) ?
65+
serverName : string.Empty;
66+
dataRow[1] = InstanceDetails.TryGetValue(SqlDataSourceEnumeratorUtil.InstanceNameCol, out string instanceName) ?
67+
instanceName : string.Empty;
68+
dataRow[2] = InstanceDetails.TryGetValue(SqlDataSourceEnumeratorUtil.IsClusteredCol, out string isClustered) ?
69+
isClustered : string.Empty;
70+
dataRow[3] = InstanceDetails.TryGetValue(SqlDataSourceEnumeratorUtil.VersionNameCol, out string versionName) ?
71+
versionName : string.Empty;
7272

7373
dataTable.Rows.Add(dataRow);
7474
}

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SQLFallbackDNSCache.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,8 @@ internal bool AddDNSInfo(SQLDNSInfo item)
2828
{
2929
if (item != null)
3030
{
31-
if (DNSInfoCache.ContainsKey(item.FQDN))
32-
{
33-
34-
DeleteDNSInfo(item.FQDN);
35-
}
36-
37-
return DNSInfoCache.TryAdd(item.FQDN, item);
31+
DNSInfoCache[item.FQDN] = item;
32+
return true;
3833
}
3934

4035
return false;

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependency.cs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -817,29 +817,21 @@ private static bool AddToServerUserHash(string server, IdentityUserNamePair iden
817817
{
818818
Dictionary<IdentityUserNamePair, List<DatabaseServicePair>> identityDatabaseHash;
819819

820-
if (!s_serverUserHash.ContainsKey(server))
820+
if (!s_serverUserHash.TryGetValue(server, out identityDatabaseHash))
821821
{
822822
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependency.AddToServerUserHash|DEP> Hash did not contain server, adding.");
823823
identityDatabaseHash = new Dictionary<IdentityUserNamePair, List<DatabaseServicePair>>();
824824
s_serverUserHash.Add(server, identityDatabaseHash);
825825
}
826-
else
827-
{
828-
identityDatabaseHash = s_serverUserHash[server];
829-
}
830826

831827
List<DatabaseServicePair> databaseServiceList;
832828

833-
if (!identityDatabaseHash.ContainsKey(identityUser))
829+
if (!identityDatabaseHash.TryGetValue(identityUser, out databaseServiceList))
834830
{
835831
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependency.AddToServerUserHash|DEP> Hash contained server but not user, adding user.");
836832
databaseServiceList = new List<DatabaseServicePair>();
837833
identityDatabaseHash.Add(identityUser, databaseServiceList);
838834
}
839-
else
840-
{
841-
databaseServiceList = identityDatabaseHash[identityUser];
842-
}
843835

844836
if (!databaseServiceList.Contains(databaseService))
845837
{
@@ -870,15 +862,12 @@ private static void RemoveFromServerUserHash(string server, IdentityUserNamePair
870862
{
871863
Dictionary<IdentityUserNamePair, List<DatabaseServicePair>> identityDatabaseHash;
872864

873-
if (s_serverUserHash.ContainsKey(server))
865+
if (s_serverUserHash.TryGetValue(server, out identityDatabaseHash))
874866
{
875-
identityDatabaseHash = s_serverUserHash[server];
876-
877867
List<DatabaseServicePair> databaseServiceList;
878868

879-
if (identityDatabaseHash.ContainsKey(identityUser))
869+
if (identityDatabaseHash.TryGetValue(identityUser, out databaseServiceList))
880870
{
881-
databaseServiceList = identityDatabaseHash[identityUser];
882871

883872
int index = databaseServiceList.IndexOf(databaseService);
884873
if (index >= 0)
@@ -934,15 +923,17 @@ internal static string GetDefaultComposedOptions(string server, string failoverS
934923

935924
lock (s_serverUserHash)
936925
{
937-
if (!s_serverUserHash.ContainsKey(server))
926+
Dictionary<IdentityUserNamePair, List<DatabaseServicePair>> identityDatabaseHash;
927+
928+
if (!s_serverUserHash.TryGetValue(server, out identityDatabaseHash))
938929
{
939930
if (0 == s_serverUserHash.Count)
940931
{
941932
// Special error for no calls to start.
942933
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependency.GetDefaultComposedOptions|DEP|ERR> ERROR - no start calls have been made, about to throw.");
943934
throw SQL.SqlDepDefaultOptionsButNoStart();
944935
}
945-
else if (!string.IsNullOrEmpty(failoverServer) && s_serverUserHash.ContainsKey(failoverServer))
936+
else if (!string.IsNullOrEmpty(failoverServer) && s_serverUserHash.TryGetValue(failoverServer, out identityDatabaseHash))
946937
{
947938
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependency.GetDefaultComposedOptions|DEP> using failover server instead\n");
948939
server = failoverServer;
@@ -954,11 +945,9 @@ internal static string GetDefaultComposedOptions(string server, string failoverS
954945
}
955946
}
956947

957-
Dictionary<IdentityUserNamePair, List<DatabaseServicePair>> identityDatabaseHash = s_serverUserHash[server];
958-
959948
List<DatabaseServicePair> databaseList = null;
960949

961-
if (!identityDatabaseHash.ContainsKey(identityUser))
950+
if (!identityDatabaseHash.TryGetValue(identityUser, out databaseList))
962951
{
963952
if (identityDatabaseHash.Count > 1)
964953
{
@@ -977,10 +966,6 @@ internal static string GetDefaultComposedOptions(string server, string failoverS
977966
}
978967
}
979968
}
980-
else
981-
{
982-
databaseList = identityDatabaseHash[identityUser];
983-
}
984969

985970
DatabaseServicePair pair = new(database, null);
986971
DatabaseServicePair resultingPair = null;

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,10 @@ internal bool AppDomainUnload(string appDomainKey)
252252
// For each decrement, subtract from count, and delete if we reach 0.
253253
lock (_appDomainKeyHash)
254254
{
255-
if (_appDomainKeyHash.ContainsKey(appDomainKey))
255+
if (_appDomainKeyHash.TryGetValue(appDomainKey, out int value))
256256
{
257257
// Do nothing if AppDomain did not call Start!
258258
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlConnectionContainer.AppDomainUnload|DEP> _appDomainKeyHash contained AppDomainKey: '{0}'.", appDomainKey);
259-
int value = _appDomainKeyHash[appDomainKey];
260259
SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.AppDomainUnload|DEP> _appDomainKeyHash for AppDomainKey: '{0}' count: '{1}'.", appDomainKey, value);
261260
Debug.Assert(value > 0, "Why is value 0 or less?");
262261

@@ -272,9 +271,9 @@ internal bool AppDomainUnload(string appDomainKey)
272271
Debug.Assert(0 == value, "We did not reach 0 at end of loop in AppDomainUnload!");
273272
Debug.Assert(!_appDomainKeyHash.ContainsKey(appDomainKey), "Key not removed after AppDomainUnload!");
274273

275-
if (_appDomainKeyHash.ContainsKey(appDomainKey))
274+
if (_appDomainKeyHash.TryGetValue(appDomainKey, out int remainingCount))
276275
{
277-
SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.AppDomainUnload|DEP|ERR> ERROR - after the Stop() loop, _appDomainKeyHash for AppDomainKey: '{0}' entry not removed from hash. Count: {1}'", appDomainKey, _appDomainKeyHash[appDomainKey]);
276+
SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.AppDomainUnload|DEP|ERR> ERROR - after the Stop() loop, _appDomainKeyHash for AppDomainKey: '{0}' entry not removed from hash. Count: {1}'", appDomainKey, remainingCount);
278277
}
279278
}
280279
else
@@ -510,10 +509,11 @@ internal void IncrementStartCount(string appDomainKey, out bool appDomainStart)
510509
// For each increment, add to count, and create entry if not present.
511510
lock (_appDomainKeyHash)
512511
{
513-
if (_appDomainKeyHash.ContainsKey(appDomainKey))
512+
if (_appDomainKeyHash.TryGetValue(appDomainKey, out int count))
514513
{
515-
_appDomainKeyHash[appDomainKey] = _appDomainKeyHash[appDomainKey] + 1;
516-
SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.IncrementStartCount|DEP> _appDomainKeyHash contained AppDomainKey: '{0}', incremented count: '{1}'.", appDomainKey, _appDomainKeyHash[appDomainKey]);
514+
count++;
515+
_appDomainKeyHash[appDomainKey] = count;
516+
SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.IncrementStartCount|DEP> _appDomainKeyHash contained AppDomainKey: '{0}', incremented count: '{1}'.", appDomainKey, count);
517517
}
518518
else
519519
{
@@ -1618,7 +1618,7 @@ private bool Start(
16181618
{
16191619
if (!_sqlDependencyPerAppDomainDispatchers.ContainsKey(appDomainKey))
16201620
{
1621-
_sqlDependencyPerAppDomainDispatchers[appDomainKey] = dispatcher;
1621+
_sqlDependencyPerAppDomainDispatchers.Add(appDomainKey, dispatcher);
16221622
}
16231623
}
16241624

@@ -1637,7 +1637,7 @@ private bool Start(
16371637
SqlConnectionContainer container = null;
16381638
lock (_connectionContainers)
16391639
{
1640-
if (!_connectionContainers.ContainsKey(hashHelper))
1640+
if (!_connectionContainers.TryGetValue(hashHelper, out container))
16411641
{
16421642
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependencyProcessDispatcher.Start|DEP> {0}, hashtable miss, creating new container.", ObjectID);
16431643
container = new SqlConnectionContainer(hashHelper, appDomainKey, useDefaults);
@@ -1647,7 +1647,6 @@ private bool Start(
16471647
}
16481648
else
16491649
{
1650-
container = _connectionContainers[hashHelper];
16511650
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependencyProcessDispatcher.Start|DEP> {0}, hashtable hit, container: {1}", ObjectID, container.ObjectID);
16521651
if (container.InErrorState)
16531652
{
@@ -1713,9 +1712,8 @@ internal bool Stop(
17131712

17141713
lock (_connectionContainers)
17151714
{
1716-
if (_connectionContainers.ContainsKey(hashHelper))
1715+
if (_connectionContainers.TryGetValue(hashHelper, out SqlConnectionContainer container))
17171716
{
1718-
SqlConnectionContainer container = _connectionContainers[hashHelper];
17191717
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependencyProcessDispatcher.Stop|DEP> {0}, hashtable hit, container: {1}", ObjectID, container.ObjectID);
17201718
server = container.Server; // Return server, database, and queue info for use by calling SqlDependency.
17211719
database = container.Database;

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,7 @@ internal SqlDependency LookupDependencyEntry(string id)
365365

366366
lock (_instanceLock)
367367
{
368-
if (_dependencyIdToDependencyHash.ContainsKey(id))
369-
{
370-
entry = _dependencyIdToDependencyHash[id];
371-
}
372-
else
368+
if (!_dependencyIdToDependencyHash.TryGetValue(id, out entry))
373369
{
374370
SqlClientEventSource.Log.TryNotificationTraceEvent("<sc.SqlDependencyPerAppDomainDispatcher.LookupDependencyEntry|DEP|ERR> ERROR - dependency ID mismatch - not throwing.");
375371
}

0 commit comments

Comments
 (0)