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
7e0c581c
Commit
7e0c581c
authored
Mar 17, 2012
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Low overhead path for maxsize==0
parent
0863d710
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
2 deletions
+13
-2
Lib/functools.py
Lib/functools.py
+13
-2
No files found.
Lib/functools.py
View file @
7e0c581c
...
@@ -170,15 +170,16 @@ def lru_cache(maxsize=100, typed=False):
...
@@ -170,15 +170,16 @@ def lru_cache(maxsize=100, typed=False):
cache
=
dict
()
cache
=
dict
()
hits
=
misses
=
0
hits
=
misses
=
0
kwd_mark
=
(
object
(),)
# separate positional and keyword args
cache_get
=
cache
.
get
# bound method to lookup key or return None
cache_get
=
cache
.
get
# bound method to lookup key or return None
_len
=
len
# localize the global len() function
_len
=
len
# localize the global len() function
kwd_mark
=
(
object
(),)
# separate positional and keyword args
lock
=
Lock
()
# because linkedlist updates aren't threadsafe
lock
=
Lock
()
# because linkedlist updates aren't threadsafe
root
=
[]
# root of the circular doubly linked list
root
=
[]
# root of the circular doubly linked list
root
[:]
=
[
root
,
root
,
None
,
None
]
# initialize by pointing to self
root
[:]
=
[
root
,
root
,
None
,
None
]
# initialize by pointing to self
PREV
,
NEXT
,
KEY
,
RESULT
=
0
,
1
,
2
,
3
# names for the link fields
PREV
,
NEXT
,
KEY
,
RESULT
=
0
,
1
,
2
,
3
# names for the link fields
def
make_key
(
args
,
kwds
,
typed
,
tuple
=
tuple
,
sorted
=
sorted
,
type
=
type
):
def
make_key
(
args
,
kwds
,
typed
,
tuple
=
tuple
,
sorted
=
sorted
,
type
=
type
):
# helper function to build a cache key from positional and keyword args
key
=
args
key
=
args
if
kwds
:
if
kwds
:
sorted_items
=
tuple
(
sorted
(
kwds
.
items
()))
sorted_items
=
tuple
(
sorted
(
kwds
.
items
()))
...
@@ -189,7 +190,17 @@ def lru_cache(maxsize=100, typed=False):
...
@@ -189,7 +190,17 @@ def lru_cache(maxsize=100, typed=False):
key
+=
tuple
(
type
(
v
)
for
k
,
v
in
sorted_items
)
key
+=
tuple
(
type
(
v
)
for
k
,
v
in
sorted_items
)
return
key
return
key
if
maxsize
is
None
:
if
maxsize
==
0
:
@
wraps
(
user_function
)
def
wrapper
(
*
args
,
**
kwds
):
# no caching, just do a statistics update after a successful call
nonlocal
misses
result
=
user_function
(
*
args
,
**
kwds
)
misses
+=
1
return
result
elif
maxsize
is
None
:
@
wraps
(
user_function
)
@
wraps
(
user_function
)
def
wrapper
(
*
args
,
**
kwds
):
def
wrapper
(
*
args
,
**
kwds
):
...
...
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