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
98281cef
Commit
98281cef
authored
Mar 09, 2013
by
Terry Jan Reedy
Browse files
Options
Browse Files
Download
Plain Diff
Merge heads
parents
adecf3f6
d8d6010d
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
7 additions
and
9 deletions
+7
-9
Lib/functools.py
Lib/functools.py
+7
-9
No files found.
Lib/functools.py
View file @
98281cef
...
@@ -222,7 +222,7 @@ def lru_cache(maxsize=128, typed=False):
...
@@ -222,7 +222,7 @@ def lru_cache(maxsize=128, typed=False):
def
decorating_function
(
user_function
):
def
decorating_function
(
user_function
):
cache
=
{}
cache
=
{}
hits
=
misses
=
currsize
=
0
hits
=
misses
=
0
full
=
False
full
=
False
cache_get
=
cache
.
get
# bound method to lookup a key or return None
cache_get
=
cache
.
get
# bound method to lookup a key or return None
lock
=
RLock
()
# because linkedlist updates aren't threadsafe
lock
=
RLock
()
# because linkedlist updates aren't threadsafe
...
@@ -242,7 +242,7 @@ def lru_cache(maxsize=128, typed=False):
...
@@ -242,7 +242,7 @@ def lru_cache(maxsize=128, typed=False):
def
wrapper
(
*
args
,
**
kwds
):
def
wrapper
(
*
args
,
**
kwds
):
# Simple caching without ordering or size limit
# Simple caching without ordering or size limit
nonlocal
hits
,
misses
,
currsize
nonlocal
hits
,
misses
key
=
make_key
(
args
,
kwds
,
typed
)
key
=
make_key
(
args
,
kwds
,
typed
)
result
=
cache_get
(
key
,
sentinel
)
result
=
cache_get
(
key
,
sentinel
)
if
result
is
not
sentinel
:
if
result
is
not
sentinel
:
...
@@ -251,14 +251,13 @@ def lru_cache(maxsize=128, typed=False):
...
@@ -251,14 +251,13 @@ def lru_cache(maxsize=128, typed=False):
result
=
user_function
(
*
args
,
**
kwds
)
result
=
user_function
(
*
args
,
**
kwds
)
cache
[
key
]
=
result
cache
[
key
]
=
result
misses
+=
1
misses
+=
1
currsize
+=
1
return
result
return
result
else
:
else
:
def
wrapper
(
*
args
,
**
kwds
):
def
wrapper
(
*
args
,
**
kwds
):
# Size limited caching that tracks accesses by recency
# Size limited caching that tracks accesses by recency
nonlocal
root
,
hits
,
misses
,
currsize
,
full
nonlocal
root
,
hits
,
misses
,
full
key
=
make_key
(
args
,
kwds
,
typed
)
key
=
make_key
(
args
,
kwds
,
typed
)
with
lock
:
with
lock
:
link
=
cache_get
(
key
)
link
=
cache_get
(
key
)
...
@@ -307,23 +306,22 @@ def lru_cache(maxsize=128, typed=False):
...
@@ -307,23 +306,22 @@ def lru_cache(maxsize=128, typed=False):
last
=
root
[
PREV
]
last
=
root
[
PREV
]
link
=
[
last
,
root
,
key
,
result
]
link
=
[
last
,
root
,
key
,
result
]
last
[
NEXT
]
=
root
[
PREV
]
=
cache
[
key
]
=
link
last
[
NEXT
]
=
root
[
PREV
]
=
cache
[
key
]
=
link
currsize
+=
1
full
=
(
len
(
cache
)
>=
maxsize
)
full
=
(
currsize
>=
maxsize
)
misses
+=
1
misses
+=
1
return
result
return
result
def
cache_info
():
def
cache_info
():
"""Report cache statistics"""
"""Report cache statistics"""
with
lock
:
with
lock
:
return
_CacheInfo
(
hits
,
misses
,
maxsize
,
currsize
)
return
_CacheInfo
(
hits
,
misses
,
maxsize
,
len
(
cache
)
)
def
cache_clear
():
def
cache_clear
():
"""Clear the cache and cache statistics"""
"""Clear the cache and cache statistics"""
nonlocal
hits
,
misses
,
currsize
,
full
nonlocal
hits
,
misses
,
full
with
lock
:
with
lock
:
cache
.
clear
()
cache
.
clear
()
root
[:]
=
[
root
,
root
,
None
,
None
]
root
[:]
=
[
root
,
root
,
None
,
None
]
hits
=
misses
=
currsize
=
0
hits
=
misses
=
0
full
=
False
full
=
False
wrapper
.
cache_info
=
cache_info
wrapper
.
cache_info
=
cache_info
...
...
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