Thanks to visit codestin.com
Credit goes to github.com

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

Conversation

benaadams
Copy link
Member

As per https://github.com/dotnet/coreclr/issues/3147#issuecomment-183158977

No stored ExecutionContext when context is default

/cc @stephentoub something like this?

@@ -53,7 +53,7 @@ internal void Undo()

public sealed class ExecutionContext : IDisposable
{
private static readonly ExecutionContext Default = new ExecutionContext();
internal static readonly ExecutionContext Default = new ExecutionContext();
Copy link
Member

Choose a reason for hiding this comment

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

This can stay private and you can instead use IsDefaultFTContext
Edit: Nevermind, it would need to be internal to be used explicitly as an arg to ExecutionContext.Run.

@stephentoub
Copy link
Member

cc: @ericeil

QueueUserWorkItemCallback tpcallBack = new QueueUserWorkItemCallback(callBack, state, compressStack, ref stackMark);
ExecutionContext context = compressStack && !ExecutionContext.IsFlowSuppressed() ?
ExecutionContext.Capture(ref stackMark, ExecutionContext.CaptureOptions.IgnoreSyncCtx | ExecutionContext.CaptureOptions.OptimizeDefaultCase) :
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.

@ericeil should comment on whether for this version of the code base it needs to be this complicated, or whether for CoreCLR it could be simplified, since we know that many of these things are nops.

@benaadams benaadams force-pushed the QueueUserWorkItemCallback branch from 3645744 to afe2dc4 Compare February 12, 2016 14:34
@@ -1168,11 +1168,7 @@ internal interface IThreadPoolWorkItem

internal sealed class QueueUserWorkItemCallback : IThreadPoolWorkItem
Copy link

Choose a reason for hiding this comment

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

Please rename to QueueUserWorkItemCallbackNullContext

@ericeil
Copy link

ericeil commented Feb 12, 2016

A couple of general thoughts on this:

  1. The work queues are exposed to debuggers, and I'm not sure how much they depend on the exact details of the workitem types. It's possible that this change will also require work in the debuggers to understand the new types. @noahfalk may know more.
  2. This change substantially increases the number of types that implement IThreadPoolWorkItem. This may impact the performance of the interface dispatch at the callsites for these methods, since they're now more polymorphic (requiring more interface dispatch cache entries). I don't know the current design of interface dispatch on CoreCLR, but there are typically performance "cliffs" as callsites become more and more polymorphic. This may not end up mattering, or be sufficiently offset by the GC perf gains from smaller (on average) work item objects, but it's something to watch out for.

Overall, though, I think this is a great change to try, with potential to help substantially in many scenarios.

@ericeil
Copy link

ericeil commented Feb 12, 2016

cc: @kouvel

@noahfalk
Copy link
Member

Thanks Eric! At the ICorDebug interfaces our interaction with the threadpool is identifying which threads are in the pool so no worries there. The SOS/DAC interfaces expose data from ThreadpoolMgr native class, but I am guessing this change wouldn't affect it. The relevant source is debug\daccess\request.cpp ClrDataAccess::GetThreadpoolData(struct DacpThreadpoolData *threadpoolData).

Higher up the stack its possible that Visual Studio might introspect on those types, but a cursory source search didn't find anything. I'll send an email to the VS debugger guys and ask them to take a peak.

@benaadams benaadams force-pushed the QueueUserWorkItemCallback branch from 16e65e5 to b939185 Compare February 12, 2016 21:57
@benaadams
Copy link
Member Author

This change substantially increases the number of types that implement IThreadPoolWorkItem...

Altered to follow @stephentoub's suggestion of having the null context use the same as non default context, and do the null check in execute. So there is just one new type for the default context.

Both null and non-default context now use the original type which is more or less the same other than removing the constructor that does the EC capture.

@noahfalk
Copy link
Member

Following up, my coworker over in VS debugger wrote me back and said he thinks this is fine from the VS perspective, they don't introspect into these types.

// call directly if it is an unsafe call OR EC flow is suppressed
WaitCallback cb = callback;
callback = null;
cb(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 did you swap the polarity of the if/else?

@stephentoub
Copy link
Member

Thanks, @noahfalk, for following up.

benaadams added a commit to benaadams/corert that referenced this pull request Feb 14, 2016
@stephentoub
Copy link
Member

LGTM (once squashed).

Does this have the expected impact on your benchmark?

@benaadams benaadams force-pushed the QueueUserWorkItemCallback branch from 672e93a to 9418bc4 Compare February 14, 2016 20:13
@benaadams
Copy link
Member Author

Checking now

@benaadams
Copy link
Member Author

Its using the right object
QueueUserCallbackDefaultContext

Have to redo it as a release build to measure the allocations for same scenario correctly.

@benaadams
Copy link
Member Author

Yep, behaves as suggested allocated bytes go from ~41MB to ~33MB

Before
Before-QueueUserWorkItemCallback

After
After-QueueUserWorkItemCallback

@ericeil
Copy link

ericeil commented Feb 15, 2016

Looks great. Thanks!

ericeil added a commit that referenced this pull request Feb 15, 2016
@ericeil ericeil merged commit 2ec6a40 into dotnet:master Feb 15, 2016
@benaadams benaadams deleted the QueueUserWorkItemCallback branch February 15, 2016 17:55
@benaadams benaadams restored the QueueUserWorkItemCallback branch February 15, 2016 17:56
benaadams added a commit to benaadams/corert that referenced this pull request Feb 16, 2016
@sajayantony
Copy link

Very cool 👍

@sajayantony
Copy link

@geoffkizer we should get some updated numbers soon.

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.

6 participants