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

Rewrite DbBuffer Read/Write methods with stackalloc #88395

Closed
wants to merge 6 commits into from

Conversation

Poppyto
Copy link
Contributor

@Poppyto Poppyto commented Jul 4, 2023

A memory optimisation for DbBuffer class, avoid GC allocation when possible

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

ghost commented Jul 4, 2023

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

Issue Details

A memory optimisation for DbBuffer class, avoid GC allocation when possible

Author: Poppyto
Assignees: -
Labels:

area-System.Data, community-contribution

Milestone: -

@xtqqczze
Copy link
Contributor

xtqqczze commented Jul 6, 2023

byte* addr = (byte*)DangerousGetHandle() + (uint)offset;
*addr = value;

@xtqqczze
Copy link
Contributor

xtqqczze commented Jul 6, 2023

On a code style note, it would be preferable to use the unsafe modifier on the ReadGuid, WriteGuid methods, instead of using unsafe blocks.


Validate(offset, sizeof(Guid));

Debug.Assert(0 == offset % /* alignof(Guid) */ sizeof(int), "invalid alignment");
Copy link
Contributor

@xtqqczze xtqqczze Jul 7, 2023

Choose a reason for hiding this comment

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

@roji, @ajcvickers Note for code owners, this change relaxes the alignment requirement from ADP.PtrSize. I believe this is acceptable, but it is still a change in behavior.

try
{
DangerousAddRef(ref mustRelease);
*((Guid*)DangerousGetHandle() + (uint)offset) = value;
Copy link
Contributor

Choose a reason for hiding this comment

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

@roji, @ajcvickers Note for code owners, please check whether this cast meets alignment requirements, or whether Unsafe.WriteUnaligned should be used instead. Notably, the value offset is only validated in the debug build.

Comment on lines +819 to +824
Span<short> spanBuffer = stackalloc short[3];
ReadInt16Array(offset, spanBuffer.Slice(0, 3));
return new DateTime(
unchecked((ushort)buffer[0]), // Year
unchecked((ushort)buffer[1]), // Month
unchecked((ushort)buffer[2])); // Day
unchecked((ushort)spanBuffer[0]), // Year
unchecked((ushort)spanBuffer[1]), // Month
unchecked((ushort)spanBuffer[2])); // Day
Copy link
Contributor

@xtqqczze xtqqczze Jul 7, 2023

Choose a reason for hiding this comment

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

This could also be rewritten as unsafe method:

[StructLayout(LayoutKind.Sequential)]
public unsafe readonly struct DateData
{
    public readonly ushort Year;
    public readonly ushort Month;
    public readonly ushort Day;
}
DateData data = *(DateData*)addr;
return new DateTime(data.Year, data.Month, data.Day);

@danmoseley
Copy link
Member

@Poppyto thanks for the contribution, sorry for the delay in responding.

@roji @ajcvickers do we want to accept this kind of change into Odbc? It does not have a readme indicating we only take necessary changes (like eg codedom has). If we aren't taking changes like this, we should create a readme; if we are, hopefuly we can review the change.

@stephentoub
Copy link
Member

@SamMonoRT, what would you like to see happen with this PR? Thanks.

@SamMonoRT
Copy link
Member

@SamMonoRT, what would you like to see happen with this PR? Thanks.

Following up

@SamMonoRT
Copy link
Member

We are not taking changes to System.Data.Odbc. We will add a ReadMe for the same. cc @roji

@SamMonoRT SamMonoRT closed this Apr 9, 2024
@github-actions github-actions bot locked and limited conversation to collaborators May 10, 2024
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.

5 participants