Ensuring functool lru_cache caching key works by normalizing arg & kwargs

I noticed based on the constructed arg/kwargs, the key used in lru_cache might not work perfectly.

I can think of 2 solution,

  1. disallow passing arg in typing interface (not runtime tho).
  2. Using inspect to normalize arg & kwargs

Here’s example of solution 2:

I understand why 1 might not best solution, but why isn’t 2 implemented? Is there any reason or could this be an improvement?

My guess is performance?

1 Like

I would guess so too. inspect.signature and its routines are fairly expensive as they are pure Python, while lru_cache is coded in C.

not only that:

foo(a=1, b=2)
foo(b=2, a=1)

Are 2 different caches. And it is specifically noted that it is due to performance: cpython/Lib/functools.py at 16cd6cc86b3ba20074ae3eefb61aeb24ee1544f7 · python/cpython · GitHub

There are many external libraries that do “caching” and “dispatching” more thoroughly. Python only provides very basic versions for these, but they are very performant.