Proposal to optimize recursive calls

  • In more than one places throughout the entire Django code, we are using recursive function calls
  • As every recursive calls takes up more space and time, We can optimize them with iterative functions.

I would like to know more about the relevance of those inefficient recursive calls and create a ticket to optimize them if consensus arrives.

One example : Link

Hi Akash,

Any proposed optimisations would need to have some profiling done to prove that it’s worth doing otherwise it introduces the risk of regression to otherwise perfectly working code.

There’s an old adage: If it ain’t broke don’t fix it :slight_smile:

David

1 Like

Indeed we need profiling data to guide optimization efforts. For a minimal example see my recent ticket optimizing a single function: #34681 (Optimize memcache_key_warnings()) – Django

In the _save_parents case I doubt there are any gains to be had. The “recursion” depth is only going to be a few levels deep, as it depends on the model inheritance hierarchy. And function calls are quite cheap from Python 3.11 onwards due to the “lazy frames” optimziation.

2 Likes

Thank you for the insights David and Adam. Got it.