Skip to content

Option to make the lru_cache type specific #57436

Description

@rhettinger
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']

    Activity

    1. rhettinger commented on Oct 19, 2011

      @rhettinger
      ContributorAuthor

      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.

    2. self-assigned this
      on Oct 19, 2011
    3. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Oct 19, 2011
    4. rhettinger commented on Oct 19, 2011

      @rhettinger
      ContributorAuthor

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

    5. vstinner commented on Oct 19, 2011

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

    6. ncoghlan commented on Oct 20, 2011

      @ncoghlan
      Contributor

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

    7. rhettinger commented on Oct 20, 2011

      @rhettinger
      ContributorAuthor

      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
    8. ncoghlan commented on Oct 20, 2011

      @ncoghlan
      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)

    9. python-dev commented on Oct 20, 2011

      python-devmannequin
      Mannequin

      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

    10. rhettinger commented on Oct 20, 2011

      @rhettinger
      ContributorAuthor

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

    11. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions