-
Notifications
You must be signed in to change notification settings - Fork 52
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
AsyncSchemaRegistryDeserializer creates new delegate for each invocation of any deserializer #314
Comments
Hmm, I would guess that this is because inner Given this, we might have to revisit how we build expression trees. |
I'm still trying to understand what .NET is doing under the hood there. I don't think it's an issue of compilation per se - in the sense that the nested lamda is compiled fine I think. As for delegate creation, my understanding was that non-capturing lambdas are cached, but that doesn't seem to be happening for expressions (though I'd need to double check if the language specs say they can be cached, or must be cached). The simplest example I could condense it down to so far is the following:
Interestingly, I don't observe the same behaviour when creating an equivalent function/invocation structure directly in code outside of the expression system:
Even more interestingly, FastExpressionCompiler seems to be doing a better job here. If I compile any of my samples with that, delegates do seem to be cached fine - though I may have to run this through for more complex examples. |
After a bit more testing - FastExpressionCompiler isn't able to compile more complex lambda expressions / throws in those cases, so unfortunately doesn't seem to be a great workaround. Thinking about this a little further, I suspect that in the current way expression trees are created, re-creating delegates might be unavoidable in some cases. Not in the simple examples above, but at least once you have types whose properties are other complex types (whether self-referential or not) - if I understand the current generators right, their lambda would reference variables from the outer scope to access their nested types' deserializers. I think that would inevitably make the lambda capture the outer parameter, and capturing lambdas are never cached. Though hard to verify at the moment given even the simple, non-capturing versions aren't cached in the default compilation path. I think a few options in increasing order of madness that come to mind at the moment are:
Maybe the first option isn't so bad after all.. :-) |
Thanks for the detailed writeup! A few additional thoughts:
Of the options you laid out, something like that is probably the most pragmatic. (A more sophisticated variation might be to walk the type graph and only do the circular reference trick for types that appear in a cycle.)
This is more or less where we're at too. I'm going to leave this as a bug since it's an unintended consequence of how the serdes are built, but it's unlikely that we'll be able to prioritize working on it. |
I was going through a memory dump and noticed unusually high allocations coming from calls to
AsyncSchemaRegistryDeserializer<T>.DeserializeAsync
on a process running on Win10 under .NET8.The allocated types were of type
Delegate
, with their call stack goingDeserializeAsync
->System.Reflection.Emit.DynamicMethod.CreateDelegate(Type, Object)
->Delegate.CreateDelegatenoSecurityCheck
.It appears that every call to
DeserializeAsync
leads to a new delegate being created for every single deserialization call (!!).For example, set breakpoint in Delegate.CoreCLR.cs ->
CreateDelegateNoSecurityCheck
, and run something likeCreateDelegateNoSecurityCheck
will be hit (at least?) three times. It's not clear to me yet why that is happeningMore self-contained example to reproduce this:
The text was updated successfully, but these errors were encountered: