Explain Codes LogoExplain Codes Logo

What is the maximum recursion depth, and how to increase it?

python
recursion-depth
performance
best-practices
Nikita BarsukovbyNikita Barsukov·Oct 8, 2024
TLDR

The default Python recursion limit is 1000, set to prevent the possibility of a stack overflow from infinite recursion. To modify it, use sys.setrecursionlimit(limit). However, tread with caution; high limits might possibly result in system crashes.

import sys # Just doubling it up sys.setrecursionlimit(2000) # Pushing limits to 2000

Take note, Python doesn't enjoy deep recursion due to the absence of tail recursion optimization.

Plate check: What's the current recursion depth?

Before diving headfirst into the pool, check how deep it is. Use sys.getrecursionlimit() to pull up the current limit:

import sys current_limit = sys.getrecursionlimit() print(f"Current recursion limit is like staircase with {current_limit} steps. Up we go!")

Iterative Solutions: A Loop...Hole?

Given Python's large stack frames, an iterative solution might just be your safety cushion. This completely bypasses the recursion limit and offers better memory efficiency. Use loops, stacks and queues to roll your own "call stack."

Beyond just sys.setrecursionlimit()

Temporary limit boosts: Context Managers

Need a quick increase in recursion limit for a particular block of code? Context managers to the rescue! This allows raising the limit for a specific block and then restore the old values once done:

from contextlib import contextmanager import sys @contextmanager def limiter_goes_brrr(new_limit): old_limit = sys.getrecursionlimit() sys.setrecursionlimit(new_limit) try: yield finally: # Business as usual sys.setrecursionlimit(old_limit) with limiter_goes_brrr(2000): # Go deep or go home! But only here. pass

System Limits: The bigger picture

The shell command ulimit displays the current stack size limit, usually about 8MB. Run ulimit -s to check the current limit and ulimit -s new_size to set a new limit. Obviously, size does matter here.

Echoing system and Python limits

While pushing limits, keep an eye on the system's RAM. A sky-high setrlimit might sap up the RAM, causing broader system issues. It is often wiser to adjust both Python's limit and the OS stack size with careful thought.

Recursive function redesign to the rescue

Sometimes, it's the design that matters. Redesigning a recursive function to minimise the recursion depth can be a lifesaver. Flatten your functions, use helper functions or simply wave goodbye to recursion and move to an iterative approach.