Skip to content

Conversation

benrr101
Copy link
Contributor

Description

While working on another chunk of code, I had another breakthrough on provably deleting SMI code. There's a tiny class InOutOfProcHelper that contains a single property InProc that always returns false. Pulling on this thread revealed that the entirety of SmiContextFactory and SmiContext could be deleted.

The process I used for safely deleting these was to basically take the hard coded values and propagate them down to lower levels. At each step of propagating, I delete branches of the code became unreachable, and keep pushing it further down. Each commit builds on its own, so it's possible to step through the changes. Trying to explain each step would probably be impossible...

Removing SmiContext from ValueUtilsSmi became a bit tricky since there are circular references that pass an SmiContext between each other. However, knowing that SmiContext should always be null, I was able to work it out by passing null into the calls to the methods, then making the argument have a null default, then eliminating nulls from the calls to the methods, then removing the "internal" usages of the argument since logically they could only be null.

There are a couple annotated spots where the code must've never been called because it will literally always throw.

Testing

These branches should not have been reached previously, so CI should be sufficient validate nothing broke.

benrr101 added 17 commits June 11, 2025 18:00
…e that false everywhere it was being used to determine what other branches of code can be deleted
…exceptions they will always throw b/c _smiLink will always be null
…sages of the property

It can continue to be propagated if we find out the version is always the same...
…nd propagate that forward, allowing us to remove MetadataUtilsSmi.IsValidForSmiVersion and a block of code in SqlDataRecord
…, since it is always passed to itself and is always null
… propagating null to all usages of it

Also removing SqlInternalConnectionSmi.SmiConnection since it's never used.
@benrr101 benrr101 added this to the 7.0-preview1 milestone Jun 12, 2025
@Copilot Copilot AI review requested due to automatic review settings June 12, 2025 04:17
@benrr101 benrr101 requested a review from a team as a code owner June 12, 2025 04:17
@benrr101 benrr101 added the Code Health 💊 Issues/PRs that are targeted to source code quality improvements. label Jun 12, 2025
Copy link
Contributor

@Copilot 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

This pull request removes dead SMI code and cleans up related internal APIs by eliminating unused SMI context and versioning logic. Key changes include:

  • Removing the InOutOfProcHelper and SmiContext-related branches and entire SmiContextFactory/SmiContext files.
  • Updating SMI utility methods (ValueUtilsSmi and related classes) to no longer require a context parameter.
  • Changing Debug.Assert/Debug.Fail calls and adding TODO markers to signal areas needing further investigation.

Reviewed Changes

Copilot reviewed 17 out of 17 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/SqlUtil.cs Added a TODO comment to verify method usage.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlDependency.cs Removed obsolete InProc SMI checks.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi*.cs Updated method signatures and XML documentation to remove context parameters.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlDataRecord*.cs Removed branches using SMI context and simplified legacy logic.
src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionSmi.cs Removed SMI context usage and updated getters to throw as expected.
src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs Replaced Debug.Assert with Debug.Fail for negotiated SMI version checks.
Other files Removed obsolete SMI API files and updated references accordingly.
Comments suppressed due to low confidence (4)

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlUtil.cs:2437

  • [nitpick] Consider expanding this TODO comment with specific criteria or conditions under which these methods should be reviewed or removed to improve clarity for future maintenance.
// @TODO: Check these methods for usage

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs:467

  • [nitpick] Consider replacing Debug.Fail with a more descriptive logging mechanism or a custom exception so that future debugging efforts clearly understand the expected behavior when this condition is encountered.
Debug.Fail("NegotiatedSmiVersion will always throw (SmiContextFactory.Instance.NegotiatedSmiVersion >= SmiContextFactory.Sql2005Version)");

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlInternalConnectionSmi.cs:32

  • [nitpick] Add a comment next to this getter to explain why it is expected to always throw, which would help maintainers understand the rationale behind omitting SMI context handling.
get => throw SQL.ContextUnavailableOutOfProc();

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SmiContextFactory.netfx.cs:1

  • Ensure that all references to SmiContextFactory have been updated and removed to avoid linkage errors or unexpected behavior in the absence of this obsolete API.
// Entire file removed

@edwardneal
Copy link
Contributor

There are a few types which are only ever used from these code paths too.

  • SqlStreamChars
  • SqlClientWrapperSmiStream
  • SqlClientWrapperSmiStreamChars

Besides this, SqlSequentialStreamSmi and SqlSequentialTextReaderSmi will also disappear shortly, once SqlDataReaderSmi is removed.

@benrr101
Copy link
Contributor Author

@edwardneal Very good catch!! Let's get that deleted code counter over 1000!

@benrr101 benrr101 added the Common Project 🚮 Things that relate to the common project project label Jun 12, 2025
Copy link

codecov bot commented Jun 12, 2025

Codecov Report

Attention: Patch coverage is 33.85827% with 84 lines in your changes missing coverage. Please review.

Project coverage is 65.80%. Comparing base (e649a7c) to head (b214f14).
Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
...x/src/Microsoft/Data/SqlClient/SqlDataReaderSmi.cs 0.00% 27 Missing ⚠️
...osoft/Data/SqlClient/Server/SqlDataRecord.netfx.cs 56.41% 17 Missing ⚠️
...c/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs 53.84% 12 Missing ⚠️
...t/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs 0.00% 10 Missing ⚠️
...crosoft/Data/SqlClient/SqlInternalConnectionSmi.cs 0.00% 7 Missing ⚠️
...nt/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs 0.00% 4 Missing ⚠️
...osoft/Data/SqlClient/Server/ValueUtilsSmi.netfx.cs 0.00% 4 Missing ⚠️
...oft/Data/SqlClient/Server/SqlDataRecord.netcore.cs 50.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3414      +/-   ##
==========================================
- Coverage   68.33%   65.80%   -2.53%     
==========================================
  Files         301      294       -7     
  Lines       65631    65210     -421     
==========================================
- Hits        44849    42913    -1936     
- Misses      20782    22297    +1515     
Flag Coverage Δ
addons 92.58% <ø> (ø)
netcore 68.66% <54.54%> (-3.95%) ⬇️
netfx 67.36% <33.05%> (+0.36%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@benrr101 benrr101 merged commit 9be9a5a into main Jun 18, 2025
251 checks passed
@benrr101 benrr101 deleted the dev/russellben/cleanup/smi-dead-code2 branch June 18, 2025 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code Health 💊 Issues/PRs that are targeted to source code quality improvements. Common Project 🚮 Things that relate to the common project project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants