Concepts 🚀 Project 🧮 Labs
📚 Learn / Algorithms & Data Structures / Caching & memoization

Caching & memoization

Store expensive results so you don't recompute. functools.lru_cache is the easy button.

Code example

from functools import lru_cache

@lru_cache(maxsize=128)
def fib(n):
    if n < 2: return n
    return fib(n-1) + fib(n-2)

# first call: slow (computes everything)
fib(50)
# second call: instant (cached)
fib(50)

How it works

Caching stores the result of an expensive function so future calls with the same arguments return instantly. functools.lru_cache is a decorator that does this automatically — least-recently-used eviction when the cache fills. The notebook could cache package recommendation results per session so re-running a cell with the same imports doesn't re-parse. The trade-off: caching uses memory, and it's only a win if the same inputs recur. Cache invalidation (knowing when the cached result is stale) is famously hard.

🔍 Where this is used in OpenBenchML Not heavily used in OpenBenchML yet — most routes hit the DB on every request. Could be added for hot paths like the leaderboard.