Minor optimization -- factor a constant expression out of the inner-loop.

This commit is contained in:
Raymond Hettinger 2011-03-18 15:09:10 -07:00
parent f84f3c3d2d
commit 7e4c168385
1 changed files with 3 additions and 3 deletions

View File

@ -140,7 +140,7 @@ def lru_cache(maxsize=100):
tuple=tuple, sorted=sorted, len=len, KeyError=KeyError):
hits = misses = 0
kwd_mark = object() # separates positional and keyword args
kwd_mark = (object(),) # separates positional and keyword args
lock = Lock() # needed because ordereddicts aren't threadsafe
if maxsize is None:
@ -151,7 +151,7 @@ def lru_cache(maxsize=100):
nonlocal hits, misses
key = args
if kwds:
key += (kwd_mark,) + tuple(sorted(kwds.items()))
key += kwd_mark + tuple(sorted(kwds.items()))
try:
result = cache[key]
hits += 1
@ -170,7 +170,7 @@ def lru_cache(maxsize=100):
nonlocal hits, misses
key = args
if kwds:
key += (kwd_mark,) + tuple(sorted(kwds.items()))
key += kwd_mark + tuple(sorted(kwds.items()))
try:
with lock:
result = cache[key]