Skip to content

Commit 5503709

Browse files
authored
Add comment to explain the implications of not sorting keywords (#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 759e30e commit 5503709

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

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)