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

Question about using some code piece in Server.cs #169

Open
TheVeryStarlk opened this issue Mar 3, 2024 · 2 comments
Open

Question about using some code piece in Server.cs #169

TheVeryStarlk opened this issue Mar 3, 2024 · 2 comments

Comments

@TheVeryStarlk
Copy link

TheVeryStarlk commented Mar 3, 2024

for each connection a running listener instance gets it calls an execute connection method and does not await it, and that method's first line is an awaited task yield method. So my question is, what is the reason behind doing this instead of just calling Task.Run and passing the connection delegate's method.

@davidfowl
Copy link
Owner

Avoids closure allocations. Using Task.Yield when there's no sync context will re-queue the existing async state machine to the thread pool.

There's 0 allocations when you do it this way. When you use Task.Run there's a new Task allocation, a delegate allocation and a closure allocation (because there's state that would be captured here).

@TheVeryStarlk
Copy link
Author

Makes sense. Thank you for this response. I asked this because I had similar code where I used Task.Run, I will refactor it to how it is done in Bedrock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants