Skip to content

Conversation

@edwardneal
Copy link
Contributor

Description

.NET 5 introduced an overload of decimal.GetBits which could write data to a stack-allocated Span<int> rather than allocating and returning a new array every time. This PR modifies TdsParser and SqlParameter to use this new overload in all cases.

We come very close to being able to eliminate the TdsParserStateObject._decimalBits field, but this is used when reading decimals and improving that would require changes to SqlBuffer. I'd like to fix that at the same time as I add test coverage for that scenario.

Issues

None.

Testing

Existing tests pass, although I'd appreciate a CI run.

@edwardneal edwardneal requested a review from a team as a code owner October 29, 2025 19:06
@apoorvdeshmukh
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

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 optimizes decimal bit extraction operations by using stack-allocated Span<int> instead of heap-allocated arrays in .NET Core and .NET 5+ environments. The changes reduce heap allocations and improve performance when processing decimal values.

  • Replaces decimal.GetBits() array allocations with stack-allocated spans in .NET targets
  • Applies the optimization consistently across multiple methods handling decimal/numeric SQL types
  • Maintains backward compatibility with .NET Framework using conditional compilation

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
TdsParser.cs Optimizes decimal bit extraction in 8 methods (WriteSqlVariantValue, WriteSqlVariantDataRowValue, WriteSqlMoney, SerializeCurrency, WriteCurrency, AdjustDecimalScale, SerializeDecimal, WriteDecimal) by using stack-allocated spans for .NET targets
SqlParameter.cs Optimizes decimal scale extraction in ValueScaleCore method using stack-allocated span for .NET targets


// write the sign (note that COM and SQL are opposite)
if (0x80000000 == (stateObj._decimalBits[3] & 0x80000000))
if ((decimalBits[3] & 0x80000000) == 0x80000000)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would use stateObj._decimalBits here and below instead of decimalBits.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would be valid in netfx, but not in netcore - we'll have written the decimal bits to decimalBits, but read values from stateObj._decimalBits.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I was assuming we would revert to always setting _decimalBits per my comment above.

@paulmedynski paulmedynski self-assigned this Oct 30, 2025
@codecov
Copy link

codecov bot commented Oct 30, 2025

Codecov Report

❌ Patch coverage is 76.47059% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.44%. Comparing base (0a39567) to head (2cd4d77).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
...qlClient/src/Microsoft/Data/SqlClient/TdsParser.cs 73.33% 8 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (0a39567) and HEAD (2cd4d77). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (0a39567) HEAD (2cd4d77)
addons 1 0
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3732      +/-   ##
==========================================
- Coverage   76.83%   69.44%   -7.39%     
==========================================
  Files         272      266       -6     
  Lines       45363    45070     -293     
==========================================
- Hits        34854    31299    -3555     
- Misses      10509    13771    +3262     
Flag Coverage Δ
addons ?
netcore 70.22% <76.92%> (-6.41%) ⬇️
netfx 69.69% <87.50%> (-6.62%) ⬇️

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.

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.

Looks great - nice to get rid of _decimalBits.

@paulmedynski
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants