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

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

codecov bot commented Oct 30, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (0a39567) to head (4fdfed7).
⚠️ Report is 18 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #3732       +/-   ##
==========================================
- Coverage   76.83%       0   -76.84%     
==========================================
  Files         272       0      -272     
  Lines       45363       0    -45363     
==========================================
- Hits        34854       0    -34854     
+ Misses      10509       0    -10509     
Flag Coverage Δ
addons ?
netcore ?
netfx ?

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).

@benrr101 benrr101 merged commit e833446 into dotnet:main Nov 17, 2025
248 checks passed
@edwardneal edwardneal deleted the perf/netcore-decimal-getbits branch November 17, 2025 17:30
@mdaigle mdaigle added this to the 7.0.0-preview3 milestone Nov 17, 2025
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.

5 participants