Skip to content
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

Closed
wants to merge 4 commits into from

Conversation

xtqqczze
Copy link
Contributor

@xtqqczze xtqqczze commented May 9, 2023

Contributes to #54297.

Although target frameworks for System.Data.Odbc include netstandard2.0 and $(NetFrameworkMinimum), these platforms are not supported (see #78550), therefore we can use the .NET 6 NativeMemory APIs.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label May 9, 2023
@ghost
Copy link

ghost commented May 9, 2023

Tagging subscribers to this area: @roji, @ajcvickers
See info in area-owners.md if you want to be subscribed.

Issue Details

Contributes to #54297.

Author: xtqqczze
Assignees: -
Labels:

area-System.Data, community-contribution

Milestone: -

@xtqqczze
Copy link
Contributor Author

xtqqczze commented May 9, 2023

Related: #54468, cc: @reflectronic.

@Wraith2
Copy link
Contributor

Wraith2 commented May 9, 2023

Does this have any specific benefits?

@xtqqczze
Copy link
Contributor Author

xtqqczze commented May 9, 2023

Does this have any specific benefits?

Use of LocalAlloc is deprecated and NativeMemory.Alloc is faster.

var zeroes = new byte[length];
Marshal.Copy(zeroes, 0, ptr, length);
#if !NET7_0_OR_GREATER
new Span<byte>((void*)ptr, length).Clear();
Copy link
Member

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 ?

Copy link
Contributor Author

@xtqqczze xtqqczze May 9, 2023

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.

Copy link
Member

@EgorBo EgorBo May 9, 2023

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

Copy link
Contributor Author

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

Copy link
Contributor Author

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)

Copy link
Contributor Author

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.

@xtqqczze
Copy link
Contributor Author

xtqqczze commented May 9, 2023

Test failures are unrelated and have been linked to existing issues.

{
Debug.Assert(initialSize % IntPtr.Size == 0, $"Expected aligned {nameof(initialSize)}.");
Copy link
Member

Choose a reason for hiding this comment

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

What guarantees this?

Copy link
Contributor Author

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.

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;
}

Copy link
Contributor Author

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.

rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this pull request Jun 2, 2023
rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this pull request Jun 6, 2023
rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this pull request Jun 6, 2023
rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this pull request Jun 7, 2023
rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this pull request Jun 9, 2023
rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this pull request Jun 13, 2023
rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this pull request Jun 15, 2023
@ghost
Copy link

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.

@ghost ghost locked as resolved and limited conversation to collaborators Nov 10, 2023
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Data community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants