Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to make the lru_cache type specific #57436

Closed
rhettinger opened this issue Oct 19, 2011 · 8 comments
Closed

Option to make the lru_cache type specific #57436

rhettinger opened this issue Oct 19, 2011 · 8 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@rhettinger
Copy link
Contributor

BPO 13227
Nosy @rhettinger, @ncoghlan, @vstinner, @ezio-melotti
Files
  • typed_lru.diff: Add type differentiation to the cache entries
  • typed_lru2.diff: Patch with tests and application to re module
  • typed_lru3.diff: Updated patch with docs
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/rhettinger'
    closed_at = <Date 2011-10-20.15:59:28.817>
    created_at = <Date 2011-10-19.22:06:36.315>
    labels = ['type-feature', 'library']
    title = 'Option to make the lru_cache type specific'
    updated_at = <Date 2011-10-20.15:59:28.815>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2011-10-20.15:59:28.815>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2011-10-20.15:59:28.817>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2011-10-19.22:06:36.315>
    creator = 'rhettinger'
    dependencies = []
    files = ['23473', '23475', '23480']
    hgrepos = []
    issue_num = 13227
    keywords = ['patch']
    message_count = 8.0
    messages = ['145976', '145985', '145990', '145994', '146007', '146008', '146022', '146023']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'ncoghlan', 'vstinner', 'ezio.melotti', 'python-dev']
    pr_nums = []
    priority = 'low'
    resolution = 'fixed'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue13227'
    versions = ['Python 3.3']

    @rhettinger
    Copy link
    Contributor Author

    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.

    @rhettinger rhettinger self-assigned this Oct 19, 2011
    @rhettinger rhettinger added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Oct 19, 2011
    @rhettinger
    Copy link
    Contributor Author

    Updated patch to include tests and an application to the re module.

    @vstinner
    Copy link
    Member

    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).

    @ncoghlan
    Copy link
    Contributor

    Features looks good, but the current patch doesn't handle keyword arguments correctly (more details in the Reitveld review).

    @rhettinger
    Copy link
    Contributor Author

    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

    @ncoghlan
    Copy link
    Contributor

    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)

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 20, 2011

    New changeset cba503a2c468 by Raymond Hettinger in branch 'default':
    bpo-13227: Option to make the lru_cache() type specific (suggested by Andrew Koenig).
    http://hg.python.org/cpython/rev/cba503a2c468

    @rhettinger
    Copy link
    Contributor Author

    Victor, Ezio, and Nick, thanks for the review.
    Andrew, thanks for the feature request.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants