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

SqlBytes.Stream should consider overriding Span Read/Write methods #69921

Closed
eerhardt opened this issue May 27, 2022 · 4 comments · Fixed by #86674
Closed

SqlBytes.Stream should consider overriding Span Read/Write methods #69921

eerhardt opened this issue May 27, 2022 · 4 comments · Fixed by #86674
Labels
area-System.Data tenet-performance Performance related issue
Milestone

Comments

@eerhardt
Copy link
Member

eerhardt commented May 27, 2022

SqlBytes.Stream can return a custom Stream class StreamOnSqlBytes and returns it to external callers. Since .NET Core 2.1, System.IO.Stream has had Read and Write overloads that accept a Span to read to / write from. However, the base Stream implementation isn't optimized. To get the best performance when a caller is using the Span-based APIs, derived implementations of Stream are expected to override the Span-based APIs and perform the operation on the Spans. See the System.IO.Stream section of #22387 for more information.

We should consider overriding the Span Read/Write methods on StreamOnSqlBytes:

// The Read/Write/ReadByte/WriteByte simply delegates to SqlBytes
public override int Read(byte[] buffer, int offset, int count)
{
CheckIfStreamClosed();
ValidateBufferArguments(buffer, offset, count);
int iBytesRead = (int)_sb.Read(_lPosition, buffer, offset, count);
_lPosition += iBytesRead;
return iBytesRead;
}
public override void Write(byte[] buffer, int offset, int count)
{
CheckIfStreamClosed();
ValidateBufferArguments(buffer, offset, count);
_sb.Write(_lPosition, buffer, offset, count);
_lPosition += count;
}

That way callers using the Span-based APIs get better performance - an ArrayPool buffer doesn't need to be rented, and the data copied twice.

@eerhardt eerhardt added area-System.Data tenet-performance Performance related issue labels May 27, 2022
@ghost ghost added the untriaged New issue has not been triaged by the area owner label May 27, 2022
@ghost
Copy link

ghost commented May 27, 2022

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

Issue Details

SqlBytes.Stream can return a custom Stream class StreamOnSqlBytes and returns it to external callers. Since .NET Core 2.0, System.IO.Stream has had Read and Write overloads that accept a Span to read to / write from. However, the base Stream implementation isn't optimized. To get the best performance when a caller is using the Span-based APIs, derived implementations of Stream are expected to override the Span-based APIs and perform the operation on the Spans. See the System.IO.Stream section of #22387 for more information.

We should consider overriding the Span Read/Write methods on StreamOnSqlBytes:

// The Read/Write/ReadByte/WriteByte simply delegates to SqlBytes
public override int Read(byte[] buffer, int offset, int count)
{
CheckIfStreamClosed();
ValidateBufferArguments(buffer, offset, count);
int iBytesRead = (int)_sb.Read(_lPosition, buffer, offset, count);
_lPosition += iBytesRead;
return iBytesRead;
}
public override void Write(byte[] buffer, int offset, int count)
{
CheckIfStreamClosed();
ValidateBufferArguments(buffer, offset, count);
_sb.Write(_lPosition, buffer, offset, count);
_lPosition += count;
}

That way callers using the Span-based APIs get better performance - an ArrayPool buffer doesn't need to be rented, and the data copied twice.

Author: eerhardt
Assignees: -
Labels:

area-System.Data, tenet-performance

Milestone: -

@ajcvickers ajcvickers added this to the Future milestone Jul 6, 2022
@ajcvickers ajcvickers removed the untriaged New issue has not been triaged by the area owner label Jul 6, 2022
@hrrrrustic
Copy link
Contributor

hrrrrustic commented May 12, 2023

@eerhardt
This issue called should consider overriding, so some discussion required? Or it's fine for contributing implementation?

@eerhardt
Copy link
Member Author

I think a contribution would be welcome. I don't think there is discussion other than "dev time".

But I'd let the area owners confirm:
@ajcvickers @DavoudEshtehari @David-Engel @roji

@David-Engel
Copy link
Contributor

Looks all internal optimization to me, so I can't see any reason we wouldn't welcome a contribution here.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label May 23, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jan 29, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Feb 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Data tenet-performance Performance related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants