Repository navigation
Conversation
d3dfeb3 to
1d5156c
Compare
|
@benaadams, have you measured / examined the perf implications of these changes? The C# compiler doesn't generate a static method for the lambda, but rather an instance method: using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Running;
using System;
[MemoryDiagnoser]
public class Program
{
static void Main(string[] args) => BenchmarkSwitcher.FromAssemblies(new[] { typeof(Program).Assembly }).Run(args);
public static readonly Action<object> s_lambda = o => { };
public static readonly Action<object> s_static = Static;
private static void Static(object o) { }
[Benchmark] public void InvokeLambda() => s_lambda(null);
[Benchmark] public void InvokeStatic() => s_static(null);
}
I don't know whether it'll matter in these cases, but I'm pointing it out because they are fairly core to the system, and this change isn't purely diagnostic. |
|
Hmm... Looks like for the lambda it creates a ; public static readonly Action<object> s_lambda = o => { };
mov rcx,offset methodtable(System.Action`1[[System.Object, System.Private.CoreLib]])
call CORINFO_HELP_NEWSFAST
mov [rbp-8],rax
mov rcx,offset <diffable-addr>
mov edx,2
call CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE
mov rdx,offset <diffable-addr>
mov rdx,[rdx]
mov [rbp-18h],rdx
mov rdx,[rbp-18h]
mov r8,offset Program+<>c.<.cctor>b__8_0(System.Object)
mov rcx,[rbp-8]
call System.MulticastDelegate.CtorClosed(System.Object, IntPtr)
mov rcx,offset <diffable-addr>
mov rdx,[rbp-8]
call CORINFO_HELP_CHECKED_ASSIGN_REF
; public static readonly Action<object> s_static = Static;
mov rcx,offset methodtable(System.Action`1[[System.Object, System.Private.CoreLib]])
call CORINFO_HELP_NEWSFAST
mov [rbp-10h],rax
mov r8,offset Program.Static(System.Object)
mov rcx,[rbp-10h]
xor edx,edx
mov r9,offset <diffable-addr>
call System.MulticastDelegate.CtorOpened(System.Object, IntPtr, IntPtr)
mov rcx,offset <diffable-addr>
mov rdx,[rbp-10h]
call CORINFO_HELP_CHECKED_ASSIGN_REF
nop
lea rsp,[rbp]
pop rbp
ret🤔 |
Is this because static method delegates involve an extra The lambda delegate creation calls this #505 (comment) private void CtorClosed(object target, IntPtr methodPtr)
{
if (target == null)
ThrowNullThisInDelegateToInstance();
this._target = target;
this._methodPtr = methodPtr;
}Whereas the static delgate ctor calls this private void CtorOpened(object target, IntPtr methodPtr, IntPtr shuffleThunk)
{
this._target = this;
this._methodPtr = shuffleThunk;
this._methodPtrAux = methodPtr;
}// In the case of a static method passed to a delegate, this field stores
// whatever _methodPtr would have stored: and _methodPtr points to a
// small thunk which removes the "this" pointer before going on
// to _methodPtrAux. |
Yep. |
1e60a02 to
c5445c6
Compare
|
Looked at changing how static delegates were invoked; but that looked quite hard. So moved most to instance methods instead. |
c5445c6 to
8b86ce4
Compare
|
Updated summary Lambdas show up horribly in stack traces Changes <>c:<.cctor>b__4_0(Object):this
<>c:<ExecuteCallbackHandlers>b__44_0(Object):this
<>c:<.cctor>b__10_0(Object):this
<>c:<.cctor>b__8_0(Object):this
<>c:<WaitForCallbackToCompleteAsync>b__50_0(Object):this
<>c:<.cctor>b__23_0(Object):this
<>c:<.cctor>b__56_0(Object):this
<>c:<.cctor>b__6_0(QueueUserWorkItemCallback):this
<>c:<.cctor>b__26_0(Object):this
<>c:<.cctor>b__17_0(Object):this
<>c:<.cctor>b__274_0(Object):this
<>c:<.cctor>b__17_1(Action):this
<>c:.ctor():thisTo ValueTaskSourceAsTask:InvokeCompletion(Object):this
CancellationTokenSource:ExecuteNodeCallback(Object):this
ThreadPoolWorkQueue:InvokeAsyncStateMachineBox(Object):this
ValueTaskAwaiter:InvokeAction(Object):this
CancellationTokenSource:WhenCallbackCompletes(Object):this
Timer:InvokeCallback(Object):this
CancellationTokenSource:InvokeCallback(Object):this
CancellationTokenSource:LinkedTokenCancel(Object):this
QueueUserWorkItemCallback:InvokeCallback(QueueUserWorkItemCallback):this
ThreadPoolTaskScheduler:StartThread(Object)
SynchronizationContextTaskScheduler:PostCallback(Object)
CancellationToken:InvokeAction(Object):this
AwaitTaskContinuation:InvokeContextCallback(Object):this
Task:InvokeContextCallback(Object):this
AwaitTaskContinuation:InvokeAction(Action):thisThe IEnumerator |
|
Is this really worth it? Those frames in the stack traces aren't super pretty, but I don't see this providing any more useful information, and the other 99.99999% of lambdas in the world will continue to look the way they do in traces. This is adding a bunch of contortions to the code, and for what. |
I hear you; and you are right public class Program
{
async static Task Main() => await Task.Run(Background);
private static void Background() => throw new Exception();
}Changing the above from to Doesn't really add much, so should additionally add |
Assuming that would still require all of these code contortions that make the code IMO harder to maintain, I'd rather this PR just be closed. If you want to work with the C# team to design/enable propagating attributes from fields lambdas are stored into to the generated methods for those lambdas (or enabling lambdas to be attributed), then we could attribute the fields/lambdas accordingly. |
|
Thanks for understanding. |
Lambdas show up horribly in stack traces
Changes
To
The IEnumerator
FilterTasksFromWorkItemsgets renumbered so it shows up in regressions & improvements