-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Use NativeMemory
in System.Data.Odbc
#85966
Conversation
Tagging subscribers to this area: @roji, @ajcvickers Issue DetailsContributes to #54297.
|
Related: #54468, cc: @reflectronic. |
aae5920
to
a99952d
Compare
Does this have any specific benefits? |
Use of |
var zeroes = new byte[length]; | ||
Marshal.Copy(zeroes, 0, ptr, length); | ||
#if !NET7_0_OR_GREATER | ||
new Span<byte>((void*)ptr, length).Clear(); |
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.
why not just Span.Clear for all TFMs ?
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.
Codegen is marginally better, see https://www.diffchecker.com/Gxzd78oe
More significantly - the #if
indicates we could just use NativeMemory.Clear
once .NET 6 is end-of-life.
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.
Codegen is marginally better, see https://www.diffchecker.com/Gxzd78oe
It seems that you compared codegen for constant length which is not the case here. Overall, Span.Clear should be better e.g. it's faster for small sizes as it won't pay price for the interop machinery so I assume it's better to just always use that instead of #if-else
. If it's for some reason slower it can be a good motivation for us to fix that in BCL/JIT
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.
Codegen is not for constant length, see https://csharp.godbolt.org/z/a157vhTT9
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.
The implementation for NativeMemory.Clear
actually uses SpanHelpers.ClearWithoutReferences(ref *(byte*)ptr, byteCount)
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.
Actually, since NativeMemory.Alloc
guarantees ptr
is pointer-aligned, and Length
is always IntPtr
aligned, we can use Unsafe.InitBlock
.
Test failures are unrelated and have been linked to existing issues. |
bf12034
to
b593c9b
Compare
{ | ||
Debug.Assert(initialSize % IntPtr.Size == 0, $"Expected aligned {nameof(initialSize)}."); |
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.
What guarantees this?
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.
DbBuffer
is is only instantiated from the derived class CNativeBuffer
.
The buffer size is either 4096 bytes, or a larger value calculated in CalcParameterBufferSize
.
runtime/src/libraries/System.Data.Odbc/src/System/Data/Odbc/OdbcParameterCollection.cs
Lines 112 to 128 in b593c9b
internal int CalcParameterBufferSize(OdbcCommand command) | |
{ | |
// Calculate the size of the buffer we need | |
int parameterBufferSize = 0; | |
for (int i = 0; i < Count; ++i) | |
{ | |
if (_rebindCollection) | |
{ | |
this[i].HasChanged = true; | |
} | |
this[i].PrepareForBind(command, (short)(i + 1), ref parameterBufferSize); | |
parameterBufferSize = (parameterBufferSize + (IntPtr.Size - 1)) & ~(IntPtr.Size - 1); // align buffer; | |
} | |
return parameterBufferSize; | |
} |
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.
Perhaps it makes sense to calculate aligned buffer size in the ctor instead.
Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it. |
Contributes to #54297.
Although target frameworks for
System.Data.Odbc
includenetstandard2.0
and$(NetFrameworkMinimum)
, these platforms are not supported (see #78550), therefore we can use the .NET 6NativeMemory
APIs.