Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
41eb79a0
Commit
41eb79a0
authored
Mar 30, 2012
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
No need to create and destroy links when updating a fixed-sized circular queue.
parent
3288e948
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
11 deletions
+16
-11
Lib/functools.py
Lib/functools.py
+16
-11
No files found.
Lib/functools.py
View file @
41eb79a0
...
...
@@ -219,7 +219,7 @@ def lru_cache(maxsize=100, typed=False):
def
wrapper
(
*
args
,
**
kwds
):
# size limited caching that tracks accesses by recency
nonlocal
hits
,
misses
nonlocal
root
,
hits
,
misses
key
=
make_key
(
args
,
kwds
,
typed
)
if
kwds
or
typed
else
args
with
lock
:
link
=
cache_get
(
key
)
...
...
@@ -236,16 +236,21 @@ def lru_cache(maxsize=100, typed=False):
return
result
result
=
user_function
(
*
args
,
**
kwds
)
with
lock
:
# put result in a new link at the front of the list
last
=
root
[
PREV
]
link
=
[
last
,
root
,
key
,
result
]
cache
[
key
]
=
last
[
NEXT
]
=
root
[
PREV
]
=
link
if
_len
(
cache
)
>
maxsize
:
# purge the least recently used cache entry
old_prev
,
old_next
,
old_key
,
old_result
=
root
[
NEXT
]
root
[
NEXT
]
=
old_next
old_next
[
PREV
]
=
root
del
cache
[
old_key
]
if
_len
(
cache
)
<
maxsize
:
# put result in a new link at the front of the list
last
=
root
[
PREV
]
link
=
[
last
,
root
,
key
,
result
]
cache
[
key
]
=
last
[
NEXT
]
=
root
[
PREV
]
=
link
else
:
# use root to store the new key and result
root
[
KEY
]
=
key
root
[
RESULT
]
=
result
cache
[
key
]
=
root
# empty the oldest link and make it the new root
root
=
root
[
NEXT
]
del
cache
[
root
[
KEY
]]
root
[
KEY
]
=
None
root
[
RESULT
]
=
None
misses
+=
1
return
result
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment