Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Conversation

@benaadams
Copy link
Member

Move back to Default ExecutionContext when AsyncLocals are cleared

Early exit if setting value to null on DefaultContext

Return null for get if context is default without checking.

/cc @stephentoub @jkotas

@stephentoub
Copy link
Member

Can you share measurements?

{
ExecutionContext current = Thread.CurrentThread.ExecutionContext;
if (current == null)
ExecutionContext current = Thread.CurrentThread.ExecutionContext ?? ExecutionContext.Default;
Copy link
Member

Choose a reason for hiding this comment

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

This is always going to make things slower.

Copy link
Member

@stephentoub stephentoub Nov 21, 2016

Choose a reason for hiding this comment

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

Seems like the only case it could help is accessing AsyncLocal.Value when no values have been set, in which case it'll add the branch but save an interface method call.


if (newValue == null && newValues.Count == 0)
{
// Async locals cleared - set ExecutionContext back to Default to move back to fast-path
Copy link
Member

Choose a reason for hiding this comment

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

How likely is it that the async locals are ever cleared? It feels like a rare corner case.

Copy link
Member

@stephentoub stephentoub Nov 21, 2016

Choose a reason for hiding this comment

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

This would happen if all AsyncLocals in use were with T == reference types and all of them had their value set back to null (any T == value type in existence or any reference type not being set back to null would thwart this). I agree that does seem relatively rare.

if (newValue == null && newValues.Count == 0)
{
// Async locals cleared - set ExecutionContext back to Default to move back to fast-path
Thread.CurrentThread.ExecutionContext = ExecutionContext.Default;
Copy link
Member

Choose a reason for hiding this comment

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

ExecutionContext contains more than just the async locals. Do you also need to compare the rest of the state?

@danmoseley
Copy link
Member

@benaadams have you had a chance to progress on this one?

@benaadams
Copy link
Member Author

It needs a deep dive; will close and come back when have something with results 😄

@benaadams benaadams closed this Dec 8, 2016
@benaadams benaadams deleted the default-context branch March 27, 2018 05:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants