You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Implement lazy-loading using source generators #10788
Would love this feature, mostly for performance reasons. Our code spends 45% CPU time in getter's of proxied objects (all in-memory). We could work around it, but it's quite a bit of work.
@sfiruch assuming you're accessing an actual database over the network, then the 45% CPU time is still likely completel neglible compared to the time spent waiting for the network response. If you're really seeing so much time spent in proxy code, this likely means your application is doing very little else (it's hard to imagine such an application that isn't a synthetic benchmark).
@sfiruch assuming you're accessing an actual database over the network, then the 45% CPU time is still likely completely neglible compared to the time spent waiting for the network response.
No, that's not the case. First, the database runs on the same machine and communicates using shared memory. Secondly, and more importantly, we fetch the data once (latency), and then do many computations using the objects. We actually measure 45% CPU time spent in dynamically generated getters in the proxy objects in practice.
If you're really seeing so much time spent in proxy code, this likely means your application is doing very little else (it's hard to imagine such an application that isn't a synthetic benchmark).
The application is doing a lot, and is a production application. The application computes prices for components and assemblies of large machines. It's doing the following thing:
Fetch a large tree structure from the database, once. This tree structure contains all information relevant to price things.
Compute the price of each node. To determine the price, we have to determine a lot of information recursively over all its children. The child computation depends on the node we determine the price for, so cannot easily be reused.
Thus, we end up with an O(n^2) algorithm running over a large tree data structure.
Workarounds
Duplicate the entity types and copy all the information, and perform computations over the copied types. Uninteresting workaround, because it's a lot of information, and would be a bit of work.
Automate the above using source generators.
Not use lazy-loading. Uninteresting, because we don't know upfront which subset of data we will require for every tree node. Also, IIRC lazy loading is a (per-context?) global setting.
Secondly, and more importantly, we fetch the data once (latency), and then do many computations using the objects. We actually measure 45% CPU time spent in dynamically generated getters in the proxy objects in practice.
Is this something you can reproduce in a standalone benchmark? It should be quite simple to do - a minimal scenario which uses lazy loading and performs some arbitrary computation, and which, when profiled, shows significant time spent inside the proxy objects.
If that's indeed the case, I think it could mean one of the following:
EF is somehow badly using lazy loading in some way, causing unreasonable CPU usage. This would be an EF problem rather than an inefficiency in the dynamic proxies, and we should fix it.
The Castle dynamic proxies themselves are somehow inefficient - that would need to be raised with them.
Everything is working well, but the work that actually needs to be done is enough to take up 45% CPU time in an application that otherwise does lots of other work; that simply seems really unlikely, given what the dynamic proxies actually do.
I hope you understand where I'm coming from here - it's just hard to imagine the proxies doing that much work, is all.
Also, IIRC lazy loading is a (per-context?) global setting.
Not really. Yes, if you want to use the dynamic proxies, you need to enable them globally, but you then opt into lazy loading on a per-navigation basis by making it virtual. That is, you choose which navigations actually do need to be lazy and which shouldn't.
Not use lazy-loading. Uninteresting, because we don't know upfront which subset of data we will require for every tree node
I'd recommend carefully thinking this through. Using lazy loading is generally discouraged and is the source of many issues. For one thing, since lazy loading is based on properties, all I/O is performed synchronously - but sync I/O is generally considered obsolete at this point, and new .NET APIs no longer provide sync I/O APIs (at which point you're mixing sync and async I/O, which is even more dangerous).
First, in many cases where it's not known upfront which subset of data is required, it's still possible to defer loading until you do know, and then use eager loading (with Include). Otherwise, you should be able to use explicit loading, which is another way of loading data late (once you know you need it), but in an explicit way rather than implicitly when a navigation is accessed. This can be done asynchronously, makes N+1 loading issues very obvious (because loading is explicit), and does not require any dynamic proxies.
To summarize, especially if you're performance-minded, I'd highly recommended looking into stopping the use of lazy loading. Otherwise, please see if you can put together a minimal repro that shows unreasonable time being taken by the proxies themselves.
Thanks for outlining your thoughts. I can't easily share the relevant code, as it's part of a large application. But here's the results from a recent profiling run where 25% CPU time is spent in the dynamic proxy code:
As I said before, this benchmark is CPU-bound, because all the data is cached locally, in the form of proxy objects. I think the CPU usage is this high, because fundamentally there are limits how good performance can get with the approach that Castle chose. Using source generators would have performance benefits, but it's obviously a bigger undertaking. I understand your suggestions regarding moving away from lazy loading. We started on this track, but had to leave it on after adding eager loading for several weeks without reaching 100%.
I just wanted to add a performance point from a production code. If you think a micro-benchmark would be helpful, I could create one - but I worry that you'd (rightly) think that my micro-benchmark is not representative of real-world workloads.
Can you post the expanded tree within Castle.DynamicProxy.AbstractionInvocation? That method itself only takes 3%, it would be good to know what actually takes the rest of the time underneath.
I see. It's just quite hard for me to believe that Castle.DynamicProxy is, well, that bad, is all.
I don't think it's bad per-se. If you call a method often enough, the overall impact will be measurable. The dynamic code does seem to do a lot more than just checking a flag.
Activity
[-]Implement Roslyn-based lazy-loading[/-][+]Implement lazy-loading using source generators[/+]sfiruch commentedon Oct 10, 2025
Would love this feature, mostly for performance reasons. Our code spends 45% CPU time in getter's of proxied objects (all in-memory). We could work around it, but it's quite a bit of work.
roji commentedon Oct 16, 2025
@sfiruch assuming you're accessing an actual database over the network, then the 45% CPU time is still likely completel neglible compared to the time spent waiting for the network response. If you're really seeing so much time spent in proxy code, this likely means your application is doing very little else (it's hard to imagine such an application that isn't a synthetic benchmark).
sfiruch commentedon Oct 20, 2025
No, that's not the case. First, the database runs on the same machine and communicates using shared memory. Secondly, and more importantly, we fetch the data once (latency), and then do many computations using the objects. We actually measure 45% CPU time spent in dynamically generated getters in the proxy objects in practice.
The application is doing a lot, and is a production application. The application computes prices for components and assemblies of large machines. It's doing the following thing:
Thus, we end up with an O(n^2) algorithm running over a large tree data structure.
Workarounds
roji commentedon Oct 20, 2025
Is this something you can reproduce in a standalone benchmark? It should be quite simple to do - a minimal scenario which uses lazy loading and performs some arbitrary computation, and which, when profiled, shows significant time spent inside the proxy objects.
If that's indeed the case, I think it could mean one of the following:
I hope you understand where I'm coming from here - it's just hard to imagine the proxies doing that much work, is all.
Not really. Yes, if you want to use the dynamic proxies, you need to enable them globally, but you then opt into lazy loading on a per-navigation basis by making it virtual. That is, you choose which navigations actually do need to be lazy and which shouldn't.
I'd recommend carefully thinking this through. Using lazy loading is generally discouraged and is the source of many issues. For one thing, since lazy loading is based on properties, all I/O is performed synchronously - but sync I/O is generally considered obsolete at this point, and new .NET APIs no longer provide sync I/O APIs (at which point you're mixing sync and async I/O, which is even more dangerous).
First, in many cases where it's not known upfront which subset of data is required, it's still possible to defer loading until you do know, and then use eager loading (with Include). Otherwise, you should be able to use explicit loading, which is another way of loading data late (once you know you need it), but in an explicit way rather than implicitly when a navigation is accessed. This can be done asynchronously, makes N+1 loading issues very obvious (because loading is explicit), and does not require any dynamic proxies.
To summarize, especially if you're performance-minded, I'd highly recommended looking into stopping the use of lazy loading. Otherwise, please see if you can put together a minimal repro that shows unreasonable time being taken by the proxies themselves.
sfiruch commentedon Oct 24, 2025
Hey Shay
Thanks for outlining your thoughts. I can't easily share the relevant code, as it's part of a large application. But here's the results from a recent profiling run where 25% CPU time is spent in the dynamic proxy code:

As I said before, this benchmark is CPU-bound, because all the data is cached locally, in the form of proxy objects. I think the CPU usage is this high, because fundamentally there are limits how good performance can get with the approach that Castle chose. Using source generators would have performance benefits, but it's obviously a bigger undertaking. I understand your suggestions regarding moving away from lazy loading. We started on this track, but had to leave it on after adding eager loading for several weeks without reaching 100%.
I just wanted to add a performance point from a production code. If you think a micro-benchmark would be helpful, I could create one - but I worry that you'd (rightly) think that my micro-benchmark is not representative of real-world workloads.
roji commentedon Oct 25, 2025
Can you post the expanded tree within Castle.DynamicProxy.AbstractionInvocation? That method itself only takes 3%, it would be good to know what actually takes the rest of the time underneath.
sfiruch commentedon Oct 27, 2025
Sure. It's not that interesting, as Castle generates the code dynamically:

roji commentedon Oct 27, 2025
I see. It's just quite hard for me to believe that Castle.DynamicProxy is, well, that bad, is all.
sfiruch commentedon Oct 27, 2025
I don't think it's bad per-se. If you call a method often enough, the overall impact will be measurable. The dynamic code does seem to do a lot more than just checking a flag.