Skip to content

remove duplicate dictionary lookups#4000

Open
SimonCropp wants to merge 1 commit intodotnet:mainfrom
SimonCropp:remove-duplicate-dictionary-lookups
Open

remove duplicate dictionary lookups#4000
SimonCropp wants to merge 1 commit intodotnet:mainfrom
SimonCropp:remove-duplicate-dictionary-lookups

Conversation

@SimonCropp
Copy link
Contributor

@SimonCropp SimonCropp commented Mar 5, 2026

No description provided.

Copilot AI review requested due to automatic review settings March 5, 2026 00:22
@SimonCropp SimonCropp requested a review from a team as a code owner March 5, 2026 00:22
@github-project-automation github-project-automation bot moved this to To triage in SqlClient Board Mar 5, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors several internal dictionary access patterns to avoid redundant ContainsKey + indexer lookups, primarily by switching to TryGetValue (and, for the DNS cache, direct indexer assignment) while preserving existing behavior in SqlDependency-related infrastructure.

Changes:

  • Replace double-lookups with TryGetValue in SqlDependency* code paths.
  • Simplify DNS cache insertion to a single overwrite assignment.
  • Update server enumerator parsing to use TryGetValue for instance detail extraction.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyUtils.cs Uses TryGetValue for dependency ID lookup under lock.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs Replaces repeated dictionary lookups with TryGetValue in app-domain/container tracking paths.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependency.cs Uses TryGetValue to avoid multiple lookups in server/user hash management and default options composition.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SQLFallbackDNSCache.cs Simplifies add/replace semantics to a single assignment into the ConcurrentDictionary.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/Sql/SqlDataSourceEnumeratorManagedHelper.netcore.cs Uses TryGetValue when populating DataRow fields from parsed instance details.

@paulmedynski
Copy link
Contributor

/azp run

@paulmedynski paulmedynski self-assigned this Mar 5, 2026
@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

Copy link
Contributor

@paulmedynski paulmedynski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A glorious set of changes! 🚀 Just a couple of comments.

_appDomainKeyHash[appDomainKey] = _appDomainKeyHash[appDomainKey] + 1;
SqlClientEventSource.Log.TryNotificationTraceEvent("SqlConnectionContainer.IncrementStartCount|DEP> _appDomainKeyHash contained AppDomainKey: '{0}', incremented count: '{1}'.", appDomainKey, _appDomainKeyHash[appDomainKey]);
count++;
_appDomainKeyHash[appDomainKey] = count;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can eliminate the increment here:

_appDomainKeyHash[appDomainKey] = count + 1;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would result in the incorrect count being logged below

if (_appDomainKeyHash.ContainsKey(appDomainKey))
if (_appDomainKeyHash.TryGetValue(appDomainKey, out int count))
{
_appDomainKeyHash[appDomainKey] = _appDomainKeyHash[appDomainKey] + 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow 3 identical lookups under the same lock 😄

if (!_sqlDependencyPerAppDomainDispatchers.ContainsKey(appDomainKey))
{
_sqlDependencyPerAppDomainDispatchers[appDomainKey] = dispatcher;
_sqlDependencyPerAppDomainDispatchers.Add(appDomainKey, dispatcher);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think TryAdd() would work here and eliminate the explicit check above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TryAdd doesnt exist in net classic

@github-project-automation github-project-automation bot moved this from To triage to In progress in SqlClient Board Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In progress

Development

Successfully merging this pull request may close these issues.

4 participants