Skip to content

better perf by avoiding redundant casts#4018

Merged
mdaigle merged 2 commits intodotnet:mainfrom
SimonCropp:better-perf-by-avoiding-cast-duplication
Apr 16, 2026
Merged

better perf by avoiding redundant casts#4018
mdaigle merged 2 commits intodotnet:mainfrom
SimonCropp:better-perf-by-avoiding-cast-duplication

Conversation

@SimonCropp
Copy link
Copy Markdown
Contributor

No description provided.

@SimonCropp SimonCropp requested a review from a team as a code owner March 7, 2026 11:13
Copilot AI review requested due to automatic review settings March 7, 2026 11:13
@github-project-automation github-project-automation bot moved this to To triage in SqlClient Board Mar 7, 2026
Copy link
Copy Markdown
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

This PR performs small, targeted micro-optimizations across SqlClient hot paths by replacing repeated is checks + explicit casts with C# pattern matching (is T t) to avoid redundant casts/unboxing and simplify code.

Changes:

  • Replace if (x is T) { (T)x ... } patterns with if (x is T t) { t ... } in multiple locations.
  • Simplify a couple of ConvertTo(...) TypeConverter implementations by combining type checks and casts.
  • Minor readability cleanup in exception handling and bulk-copy XML handling.

Reviewed changes

Copilot reviewed 6 out of 6 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/TdsParser.cs Uses pattern-matching locals to avoid repeated casts/unboxing while writing/serializing values (RPC params, bulk copy, and serialization paths).
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlUtil.cs Simplifies SqlException detection/cast when building error arrays.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlParameter.cs TypeConverter ConvertTo now pattern-matches SqlParameter once before calling instance descriptor conversion.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionStringBuilder.cs TypeConverter ConvertTo now pattern-matches SqlConnectionStringBuilder before instance descriptor conversion.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommand.cs Avoids duplicate cast by binding SqlString via pattern matching in ANSI size computation logic.
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlBulkCopy.cs Avoids duplicate cast when converting XmlReader to XmlDataFeed for XML/JSON bulk copy values.

@SimonCropp SimonCropp changed the title better perf by avoiding cast duplication better perf by avoiding redundant casts Mar 7, 2026
@paulmedynski paulmedynski added the Code Health 💊 Issues/PRs that are targeted to source code quality improvements. label Mar 9, 2026
@paulmedynski paulmedynski modified the milestones: 7.0.1, 7.1.0-preview1 Mar 9, 2026
@benrr101 benrr101 moved this from To triage to In review in SqlClient Board Mar 17, 2026
@paulmedynski paulmedynski self-assigned this Apr 7, 2026
Copy link
Copy Markdown
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.

Looks great!

@paulmedynski
Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 2 pipeline(s).

@mdaigle mdaigle enabled auto-merge (squash) April 16, 2026 21:22
@mdaigle mdaigle modified the milestone: 7.1.0-preview1 Apr 16, 2026
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 16, 2026

Codecov Report

❌ Patch coverage is 29.41176% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.12%. Comparing base (569ada7) to head (9d7e7d7).
⚠️ Report is 74 commits behind head on main.

Files with missing lines Patch % Lines
...qlClient/src/Microsoft/Data/SqlClient/TdsParser.cs 0.00% 10 Missing ⚠️
...lient/src/Microsoft/Data/SqlClient/SqlParameter.cs 50.00% 2 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (569ada7) and HEAD (9d7e7d7). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (569ada7) HEAD (9d7e7d7)
CI-SqlClient 1 0
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4018      +/-   ##
==========================================
- Coverage   72.36%   66.12%   -6.25%     
==========================================
  Files         287      271      -16     
  Lines       43149    65716   +22567     
==========================================
+ Hits        31223    43452   +12229     
- Misses      11926    22264   +10338     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 66.12% <29.41%> (?)

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.

@mdaigle mdaigle merged commit 4df2aae into dotnet:main Apr 16, 2026
299 of 300 checks passed
@github-project-automation github-project-automation bot moved this from In review to Done in SqlClient Board Apr 16, 2026
@SimonCropp SimonCropp deleted the better-perf-by-avoiding-cast-duplication branch April 17, 2026 12:54
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.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants