-
Notifications
You must be signed in to change notification settings - Fork 311
Merge | Netfx SqlTypeWorkarounds #3448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 rewrites the SQL type conversion workarounds for .NET Framework (netfx) to improve performance and code clarity while replacing dynamic IL method creation with reflection‐based factories and adding comprehensive unit tests.
- Rewritten conversion methods for SqlBinary, SqlDecimal, SqlGuid, and SqlMoney using direct memory access and unsafe code paths.
- Updated call sites (e.g. in TdsParser, SqlBuffer, and ValueUtilsSmi) to use the new conversion methods.
- Updated project and package references to include System.ValueTuple and removed legacy netfx implementations.
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
tests/UnitTests/Microsoft/Data/SqlTypes/SqlTypeWorkaroundsTests.cs | Added unit tests to validate the new conversion method implementations. |
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlTypes/SqlTypeWorkarounds.cs | Rewritten conversion methods using reflection-based factories and unsafe memory access. |
src/Microsoft.Data.SqlClient/SqlBuffer.cs | Replaced deprecated SqlMoneyCtor calls with LongToSqlMoney conversion. |
src/Microsoft.Data.SqlClient/Server/ValueUtilsSmi.cs | Updated SqlMoney conversion calls to use new methods. |
src/Microsoft.Data.SqlClient/TdsParser.cs | Updated invocations for SqlBinary, SqlGuid, and SqlDecimal conversions; enhanced readability with explicit braces. |
csproj and Directory.Packages.props | Added System.ValueTuple dependency and updated related project dependencies. |
Comments suppressed due to low confidence (1)
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlTypes/SqlTypeWorkarounds.cs:115
- Consider adding dedicated unit tests to simulate scenarios where reflection fails in CreateSqlToInternalRepresentationFactory and CreateSqlMoneyToLongFactory. This will ensure that the fallback logic is correctly exercised and behaves as expected.
private static unsafe Func<SqlDecimal, (uint, uint, uint, uint)> CreateSqlToInternalRepresentationFactory()
|
||
#endregion | ||
|
||
private static unsafe Func<TValue, TInstance> CreateFactory<TInstance, TValue, TIgnored>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider refactoring the CreateFactory method to reduce its complexity and improve readability. Splitting its logic or adding more detailed inline comments could ease future maintenance of the unsafe pointer arithmetic and reflection handling.
Copilot uses AI. Check for mistakes.
@benrr101 - This is a sensitive hot path. I'd perf check your changes. For reference, check out #1647 (comment). You might be able to reuse the BDN project on that comment. |
It's good to get the trailing file merged, but I'm not sure switching to using pointers is a real simplification. The dynamic method is well-documented and although it's unusual, it doesn't raise questions about the managed/unmanaged memory layout of SqlDecimal, whether the value needs to be pinned or any performance issues which might arise from copying structs if they're passed by value. I was planning to use a DynamicMethod when simplifying the UDT serialization so might be slightly biased, but I think keeping the initial approach of emitting a single IL method might be simpler here. The method we emit isn't much more than a chain of field accesses. |
@David-Engel As per your request, here's comparison of perf for old vs new, fast vs fallback. As you can see, almost all of them are faster or are negligibly slower (ie <1%). Though the question is whether this is compliant or not (I'll run a test to see what it says). [Numbers removed b/c they're out of date now] @edwardneal I'd have to hard disagree on using dynamic methods. Unless there's a huge performance difference between not using them and using them (like 20%+) I think readability and maintainability outweigh it. I think for most cases (at least for our case), engineers are going to be more familiar with pointers than IL opcodes. And in this case, removing the dynamic method actually improves perf by 11% (though some of that might be due to removing checks and some layers of indirection). Though this did lead me down a trail to learn more about marshaling and blittable types. As near as I can figure, for structs, as long as they aren't defined as using |
Update, some more information has come to light about considerations for these code paths, and I need to reimplement a couple of them. As such, I'm going to mark this as draft until it's ready. Updated perf numbers will be provided when ready. And as a heads up to @edwardneal the DynamicMethod will go away, but so will accessing member variables. |
Replace SqlMoneyToLong code to use ToInternalRepresentation
Alrighty, updates are ready for review. @David-Engel as per your request, here's the perf numbers:
|
Thanks for checking @benrr101; I've just done the same, and agree - the managed and unmanaged layouts are going to be the same (although that's pretty much academic now.) The removal of DynamicMethod is probably the reason why SqlDecimal_WriteTdsValueFast has regressed (and it'd be good to see the benchmark's memory allocation figures) but if you're happy with the performance/complexity tradeoff then everything else looks good to me. |
{ | ||
// The logic of this method is that there are special internal methods that can create | ||
// Sql* types without the need for copying. These methods are internal to System.Data, | ||
// so we cannot access them, even they are so much faster. To get around this, we |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
even though they
5.3 % slower on decimal? Is that OK - or am I misreading the numbers? |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #3448 +/- ##
===========================================
+ Coverage 63.30% 92.58% +29.27%
===========================================
Files 280 6 -274
Lines 62386 310 -62076
===========================================
- Hits 39493 287 -39206
+ Misses 22893 23 -22870
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlTypes/SqlTypeWorkarounds.cs
Show resolved
Hide resolved
// Documentation for internal method: | ||
// https://learn.microsoft.com/en-us/dotnet/framework/additional-apis/system.data.sqltypes.sqlmoney.tosqlinternalrepresentation | ||
|
||
MethodInfo method = typeof(SqlMoney).GetMethod( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Invoking a statically cached method would be faster than redoing Reflection lookup. Something like static readonly MethodInfo s_toSqlInternal = typeof(SqlMoney).GetMethod(...);
Description
This PR is less of a merge and more of a total rewrite 😅 . There are a handful of methods that are used in the code to convert between internal representations of a handful of Sql* types (SqlBinary, SqlDecimal, SqlGuid, and SqlMoney). In netcore, we can use built-in methods to get at these internal representations, but netfx will not have access to them. There are equivalent conversion methods available, but they almost always make copies of the data. Thus, we have these methods that directly access the data or internal conversion method for the best performance when reading these types.
The original implementations of these were clunky and confusing, and although it'd be easy to copy-paste them into the common project file, I wanted to make sure we knew what was going on (and whether they were being used). Thus, with a bit of AI magic, I rewrote these conversion methods. Now, I'm sure no one will trust that I did it right - I mean, I don't even trust myself! So, this is my first PR where I'm introducing true unit tests! These were checked against the behavior of the original implementations, so it can be safely assumed that the new implementations do things correctly. I have also added comments to the methods to give a better understanding of how they work, for future people.
Also, say goodbye to dynamic IL method creation!
Issues
#1261
Testing
Added unit tests for the rewritten methods!