Skip to content

Conversation

@stephentoub
Copy link
Member

We still need to allocate an object for the closed-over state, but we can avoid the per-call delegate allocation.

We still need to allocate an object for the closed-over state, but we can avoid the per-call delegate allocation.
@stephentoub stephentoub added area-System.IO tenet-performance Performance related issue labels May 2, 2022
@stephentoub stephentoub added this to the 7.0.0 milestone May 2, 2022
@ghost
Copy link

ghost commented May 2, 2022

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

Issue Details

We still need to allocate an object for the closed-over state, but we can avoid the per-call delegate allocation.

Author: stephentoub
Assignees: -
Labels:

area-System.IO, tenet-performance

Milestone: 7.0.0

@ghost ghost assigned stephentoub May 2, 2022
Copy link
Member

@danmoseley danmoseley left a comment

Choose a reason for hiding this comment

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

Did we / should we do a pass for other places that could benefit from translating to a static lambda? Eg., at random

                RequestQueue<HttpConnection>.QueueItem queueItem = _http11RequestQueue.PeekNextRequestForConnectionAttempt();

                Task.Run(() => AddHttp11ConnectionAsync(queueItem));

@danmoseley
Copy link
Member

I actually don't have a good intuition for these things, and why the compiler or runtime can't do this transformation when possible.

@stephentoub
Copy link
Member Author

at random

#68750

int startTime = Environment.TickCount; // We need to measure time here, not in the lambda
return Task.Run(() => ConnectInternal(timeout, cancellationToken, startTime), cancellationToken);

return Task.Factory.StartNew(static state =>
Copy link
Member

Choose a reason for hiding this comment

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

Why Task.Factory.StartNew -- so you could pass DenyChildAttach?

Copy link
Member

Choose a reason for hiding this comment

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

I believe it's just because Task.Factory.StartNew accepts a state object, whereas Task.Run doesn't.

Copy link
Member Author

Choose a reason for hiding this comment

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

because Task.Factory.StartNew accepts a state object

Right

@danmoseley
Copy link
Member

danmoseley commented May 2, 2022

Ha, I hadn't seen that. Is Task.Run without 'static' the code smell?

eg

edit: never mind, there's clearly already a delegate allocated in that case. OK, I don't see significant other cases then.

@danmoseley danmoseley merged commit c31c514 into dotnet:main May 2, 2022
@stephentoub stephentoub deleted the npcsdelegate branch May 2, 2022 10:06
@ghost ghost locked as resolved and limited conversation to collaborators Jun 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.IO tenet-performance Performance related issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants