Uh oh!
There was an error while loading. Please reload this page.
Use NativeMemory in System.Data.Odbc - #85966
Conversation
ghost
commented
May 9, 2023
Tagging subscribers to this area: @roji, @ajcvickers Issue DetailsContributes to #54297.
|
Related: #54468, cc: @reflectronic. |
Wraith2
commented
May 9, 2023
Does this have any specific benefits? |
xtqqczze
commented
May 9, 2023
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.
why not just Span.Clear for all TFMs ?
There was a problem hiding this comment.
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.
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.
Codegen is not for constant length, see https://csharp.godbolt.org/z/a157vhTT9
There was a problem hiding this comment.
The implementation for NativeMemory.Clear actually uses SpanHelpers.ClearWithoutReferences(ref *(byte*)ptr, byteCount)
There was a problem hiding this comment.
Actually, since NativeMemory.Alloc guarantees ptr is pointer-aligned, and Length is always IntPtr aligned, we can use Unsafe.InitBlock.
xtqqczze
commented
May 9, 2023
Test failures are unrelated and have been linked to existing issues. |
| protected DbBuffer(int initialSize) : base(IntPtr.Zero, true) | ||
| protected unsafe DbBuffer(int initialSize) : base(IntPtr.Zero, true) | ||
| { | ||
| Debug.Assert(initialSize % IntPtr.Size == 0, $"Expected aligned {nameof(initialSize)}."); |
There was a problem hiding this comment.
DbBuffer is is only instantiated from the derived class CNativeBuffer.
The buffer size is either 4096 bytes, or a larger value calculated in CalcParameterBufferSize.
There was a problem hiding this comment.
Perhaps it makes sense to calculate aligned buffer size in the ctor instead.
ghost
commented
Oct 11, 2023
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.Odbcincludenetstandard2.0and$(NetFrameworkMinimum), these platforms are not supported (see #78550), therefore we can use the .NET 6NativeMemoryAPIs.