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

SafeFileHandle.ThreadPoolValueTaskSource made ligher by one field #113435

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Scooletz
Copy link

@Scooletz Scooletz commented Mar 12, 2025

This PR combines gather/scatter fields in SafeFileHandle.ThreadPoolValueTaskSource into one. This saves one reference field in TPVTS.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 12, 2025
@Scooletz
Copy link
Author

This is my first PR to the mighty runtime 😅

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

This is a performance optimization. Do you have any benchmark that shows this makes a difference?

Debug.Assert(_readScatterBuffers != null);
result = RandomAccess.ReadScatterAtOffset(_fileHandle, _readScatterBuffers, _fileOffset);
Debug.Assert(_buffers != null);
result = RandomAccess.ReadScatterAtOffset(_fileHandle, Unsafe.As<IReadOnlyList<Memory<byte>>>(_buffers), _fileOffset);
Copy link
Member

Choose a reason for hiding this comment

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

It is not ok to use Unsafe.As just because of you can. In fact, we are working on eliminating Unsafe.As and similar uses to make the runtime core more secure by construction.

Copy link
Author

Choose a reason for hiding this comment

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

Clearly, one cannot use it just like that. The reasoning was that the setting of the field is made only in one of two scatter/gather methods and this is aligned with setting the _operation. In my opinion it's similar to the case in ValueTask which also casts As according to the "type" held in another filed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe you could make a regular cast; the always-true type check will be dwarfed by the cost of the subsequent I/O operation.

Copy link
Author

Choose a reason for hiding this comment

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

Replaced by the regular

@jkotas jkotas added the tenet-performance Performance related issue label Mar 12, 2025
@Scooletz
Copy link
Author

This is a performance optimization. Do you have any benchmark that shows this makes a difference?

Let me spin some tomorrow and share what it does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.IO community-contribution Indicates that the PR has been added by a community member tenet-performance Performance related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants