Skip to content

Implement lazy-loading using source generators #10788

Description

@ajcvickers

This feature would use Roslyn to re-write entity classes as part of compilation to inject lazy-loading behaviors without the limitations of proxies.

Activity

  1. added this to the Backlog milestone on Jan 27, 2018
  2. changed the title [-]Implement Roslyn-based lazy-loading[/-] [+]Implement lazy-loading using source generators[/+] on Oct 20, 2022
  3. added theissue type on Jun 15, 2025
  4. sfiruch commented on Oct 10, 2025

    @sfiruch

    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.

  5. roji commented on Oct 16, 2025

    @roji
    Member

    @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).

  6. sfiruch commented on Oct 20, 2025

    @sfiruch

    @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:

    1. Fetch a large tree structure from the database, once. This tree structure contains all information relevant to price things.
    2. 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.
  7. roji commented on Oct 20, 2025

    @roji
    Member

    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:

    1. 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.
    2. The Castle dynamic proxies themselves are somehow inefficient - that would need to be raised with them.
    3. 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.

  8. sfiruch commented on Oct 24, 2025

    @sfiruch

    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:
    Image

    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.

  9. roji commented on Oct 25, 2025

    @roji
    Member

    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.

  10. sfiruch commented on Oct 27, 2025

    @sfiruch

    Sure. It's not that interesting, as Castle generates the code dynamically:
    Image

  11. roji commented on Oct 27, 2025

    @roji
    Member

    I see. It's just quite hard for me to believe that Castle.DynamicProxy is, well, that bad, is all.

  12. sfiruch commented on Oct 27, 2025

    @sfiruch

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions