Skip to content

Commit 52a5861

Browse files
rhettingerGadgetSteve
authored andcommitted
Add comment to explain the implications of not sorting keywords (python#3331)
In Python 3.6, sorted() was removed from _make_key() for the lru_cache and instead rely on guaranteed keyword argument order preservation. This makes keyword argument handling faster but it also causes multiple callers with a different keyword argument order to be cached as separate items. Depending on your point of view, this is either a performance regression (increased number of cache misses) or a performance enhancement (faster computation of keys).
1 parent ee85765 commit 52a5861

1 file changed

Lines changed: 4 additions & 0 deletions

File tree

Lib/functools.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,10 @@ def _make_key(args, kwds, typed,
432432
saves space and improves lookup speed.
433433
434434
"""
435+
# All of code below relies on kwds preserving the order input by the user.
436+
# Formerly, we sorted() the kwds before looping. The new way is *much*
437+
# faster; however, it means that f(x=1, y=2) will now be treated as a
438+
# distinct call from f(y=2, x=1) which will be cached separately.
435439
key = args
436440
if kwds:
437441
key += kwd_mark

0 commit comments

Comments
 (0)