classification
Title: Option to make the lru_cache type specific
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: ezio.melotti, haypo, ncoghlan, python-dev, rhettinger
Priority: low Keywords: patch

Created on 2011-10-19 22:06 by rhettinger, last changed 2011-10-20 15:59 by rhettinger. This issue is now closed.

Files
File name Uploaded Description Edit
typed_lru.diff rhettinger, 2011-10-19 22:06 Add type differentiation to the cache entries review
typed_lru2.diff rhettinger, 2011-10-19 22:54 Patch with tests and application to re module review
typed_lru3.diff rhettinger, 2011-10-20 04:30 Updated patch with docs review
Messages (8)
msg145976 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-10-19 22:06
Based on a comment from Andrew Koenig, it is suggested that the lru_cache() offer an option to become type specific so that equal objects of different typed don't get cached to the same entry.

Here's an example:

@lru_cache(typed=True)
def square(x):
    print('squaring', x)
    return x * x

for x in [3, 3.0, 3, 3.0]:
    print(square(x))

Without type specificity, all four calls will return 9 rather than 9. 9.0, 9, 9.0.

First-pass patch attached.
msg145985 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-10-19 22:54
Updated patch to include tests and an application to the re module.
msg145990 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2011-10-19 23:04
The patch doesn't update the doc. You may also test type(square(3)). Except of these nits, the patch looks good.

The typed option is an important feature, and your patch contains a good example of its usage (the re module).
msg145994 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2011-10-20 00:36
Features looks good, but the current patch doesn't handle keyword arguments correctly (more details in the Reitveld review).
msg146007 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-10-20 04:30
Nick, Ezio, and Victor, thanks for looking at the code.  
Here's an updated the patch:
* optimization for fast globals() are now keyword-only arguments
* now types the keyword values, not the key/values tuples
* expanded tests
* added the doc update
msg146008 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2011-10-20 04:45
Looks good to me (although it took me longer than it should have to figure out why you didn't need to store the keyword argument names a second time)
msg146022 - (view) Author: Roundup Robot (python-dev) Date: 2011-10-20 15:58
New changeset cba503a2c468 by Raymond Hettinger in branch 'default':
Issue 13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
http://hg.python.org/cpython/rev/cba503a2c468
msg146023 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-10-20 15:59
Victor, Ezio, and Nick, thanks for the review.
Andrew, thanks for the feature request.
History
Date User Action Args
2011-10-20 15:59:28rhettingersetstatus: open -> closed
resolution: fixed
messages: + msg146023
2011-10-20 15:58:05python-devsetnosy: + python-dev
messages: + msg146022
2011-10-20 04:45:50ncoghlansetmessages: + msg146008
2011-10-20 04:30:01rhettingersetfiles: + typed_lru3.diff

messages: + msg146007
2011-10-20 00:58:17ezio.melottisetnosy: + ezio.melotti

stage: patch review
2011-10-20 00:36:04ncoghlansetnosy: + ncoghlan
messages: + msg145994
2011-10-19 23:04:09hayposetnosy: + haypo
messages: + msg145990
2011-10-19 22:54:29rhettingersetfiles: + typed_lru2.diff

messages: + msg145985
2011-10-19 22:06:36rhettingercreate