Skip to content

better perf by avoiding redundant casts#4018

Open
SimonCropp wants to merge 2 commits intodotnet:mainfrom
SimonCropp:better-perf-by-avoiding-cast-duplication
Open

better perf by avoiding redundant casts#4018
SimonCropp wants to merge 2 commits intodotnet:mainfrom
SimonCropp:better-perf-by-avoiding-cast-duplication

Conversation

@SimonCropp
Copy link
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: To triage

Development

Successfully merging this pull request may close these issues.

2 participants