Skip to content

New shared-keys dictionary implementation #58111

Closed

Description

@markshannon
BPO 13903
Nosy @loewis, @rhettinger, @terryjreedy, @gpshead, @jcea, @pitrou, @vstinner, @pjenvey, @benjaminp, @markshannon, @JimJJewett
Files
  • 691ce331f955.diff: Patch to rev. 06a6fed0da56
  • 49b7e7e4a27c.diff
  • 257e16e71654.diff: patch to revision 2a142141e5fd
  • 372d0bca85ae.diff: Patch to rev. 9fcb2676696c
  • 73423916a242.diff: Patch to rev. 73423916a242
  • str_subclass.patch
  • gc_tracking.patch: Patch
  • make_split_table_error.patch
  • cached_keys.patch
  • dkdebug.patch
  • insertdict.patch
  • 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 = None
    closed_at = <Date 2012-05-02.17:09:34.992>
    created_at = <Date 2012-01-29.14:26:46.386>
    labels = ['interpreter-core', 'performance']
    title = 'New shared-keys dictionary implementation'
    updated_at = <Date 2012-05-12.21:52:57.593>
    user = 'https://github.com/markshannon'

    bugs.python.org fields:

    activity = <Date 2012-05-12.21:52:57.593>
    actor = 'python-dev'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-05-02.17:09:34.992>
    closer = 'benjamin.peterson'
    components = ['Interpreter Core']
    creation = <Date 2012-01-29.14:26:46.386>
    creator = 'Mark.Shannon'
    dependencies = []
    files = ['24510', '24670', '24766', '25096', '25313', '25318', '25339', '25340', '25381', '25385', '25422']
    hgrepos = ['112']
    issue_num = 13903
    keywords = ['patch']
    message_count = 43.0
    messages = ['152229', '152253', '152293', '152345', '152351', '152366', '152373', '152380', '152419', '152863', '152885', '154637', '154645', '155246', '155248', '156096', '157365', '157466', '157477', '157482', '157500', '157649', '158051', '158192', '158723', '159018', '159028', '159031', '159032', '159049', '159055', '159139', '159143', '159161', '159172', '159188', '159478', '159484', '159485', '159489', '159683', '159698', '160496']
    nosy_count = 14.0
    nosy_names = ['loewis', 'rhettinger', 'terry.reedy', 'gregory.p.smith', 'jcea', 'pitrou', 'vstinner', 'pjenvey', 'benjamin.peterson', 'Yury.Selivanov', 'Mark.Shannon', 'python-dev', 'jcon', 'Jim.Jewett']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue13903'
    versions = ['Python 3.3']

    Activity

    1. markshannon commented on Jan 29, 2012

      @markshannon
      MemberAuthor

      The proposed dictionary implementation allows sharing of keys & hashes between dictionaries. This leads to substantial memory savings for object-oriented programs. For non-OO programs the impact is negligible.

    2. added
      interpreter-core
      performance
      on Jan 29, 2012
    3. terryjreedy commented on Jan 29, 2012

      @terryjreedy
      Member

      In the initial comment, 'Dummy' to 'Deleted' here but only here:

      • Holds an active (key, value) pair. Active can transition to Dummy
        + Holds an active (key, value) pair. Active can transition to Deleted

      Im Lib/test/test_pprint.py
      def test_set_reprs(self): ...
      # Consequently, this test is fragile and ...
      + # XXX So why include this "test" in the first place?
      Raymond, I believe you added this 44927 and revised for 3.x in 45067.
      I imagine it will also be a problem with randomized hashes. Should it be removed or somehow revised?

    4. vstinner commented on Jan 30, 2012

      @vstinner
      Member

      I see two unrelated parts in your patch:

      • change dictionary structure in memory
      • change many constants linked to optimization: PyDICT_MAXFREELIST: 80->40, 2/3->5/8, etc.

      You may open a new issue for the second part, except if I am wrong and you need to change constants for the first part?

      (I don't understand why you changed constants and how you chose new values.)

    5. rhettinger commented on Jan 30, 2012

      @rhettinger
      Contributor

      This would likely require a PEP before having a chance of being considered for inclusion.

    6. markshannon commented on Jan 30, 2012

      @markshannon
      MemberAuthor

      Does this really need a PEP?
      There is no new language feature and no change to any API.
      It is just saving some memory.

      The only possible issue is changing dict repr() ordering,
      but bpo-13703 will do that anyway.

    7. self-assigned this
      on Jan 31, 2012
    8. rhettinger commented on Jan 31, 2012

      @rhettinger
      Contributor

      Changing dictionaries is a big deal. You're changing many pieces at once (not a good idea) including changing tunable parameters that are well-studied (I spent a month testing whether 5/8 was better idea that 2/3 for resizing or when the ideal small dict size was 4, 8, or 16). You're changing the meaning of the fields in dictobject.h which will likely break any code that relied on those.

      The ideas may be good ones but they warrant a good deal of thought. Dicts weren't just slapped together -- the current code is the product to two decades of tweaking by engineers who devoted significant time to the task. It would be easy to unknowingly undo some of their work.

    9. markshannon commented on Jan 31, 2012

      @markshannon
      MemberAuthor

      Raymond Hettinger wrote:

      Raymond Hettinger <raymond.hettinger@gmail.com> added the comment:

      Changing dictionaries is a big deal. You're changing many pieces at once (not a good idea) including changing tunable parameters that are well-studied (I spent a month testing whether 5/8 was better idea that 2/3 for resizing or when the ideal small dict size was 4, 8, or 16). You're changing the meaning of the fields in dictobject.h which will likely break any code that relied on those.

      The ideas may be good ones but they warrant a good deal of thought. Dicts weren't just slapped together -- the current code is the product to two decades of tweaking by engineers who devoted significant time to the task. It would be easy to unknowingly undo some of their work.

      OK.
      I'll write a PEP.

      By the way, I'm trying not changing the tunable parameters for the
      unshared-keys case, just the shared-keys case. Of course, they do
      interact with each other.

    10. pitrou commented on Jan 31, 2012

      @pitrou
      Member

      As Victor I think the tunables could be changed separately, unless they truely get in the way of the shared-keys optimization.

      By the way, there will need a "protection" for the case where instances are used as bags of key/value pairs (dict-alikes with attribute access). Perhaps bound the size of the keys array to 100 entries and then switch into unshared mode.

    11. gpshead commented on Feb 1, 2012

      @gpshead
      Member

      FYI - I strongly support this type of work to reduce memory use of the Python interpreter! :)

      Also, yes, constant changing should be separate from this change but are worth occasionally re-measuring and justifying as common computer architectures have changed since the original decisions were made.

    12. pitrou commented on Feb 8, 2012

      @pitrou
      Member

      Looking at your latest patch, I worry about "any deletion
      +(including pop & popitem) causes a split table to become a combined table". Why wouldn't you use a dummy pointer (such as ((PyObject *) 1)) to signal deleted slots?

    13. markshannon commented on Feb 8, 2012

      @markshannon
      MemberAuthor

      Antoine Pitrou wrote:

      Antoine Pitrou <pitrou@free.fr> added the comment:

      Looking at your latest patch, I worry about "any deletion
      +(including pop & popitem) causes a split table to become a combined table". Why wouldn't you use a dummy pointer (such as ((PyObject *) 1)) to signal deleted slots?

      In fact here is no need for a dummy pointer.
      When deleting from a split-table, the value can just be set to NULL,
      the resulting key-NULL pair is legal in a split-table.
      Your suggestion doesn't make the code any more complex, so I've included it.

      In practice, it will very rare that a deletion occurs in a split table
      (since they are only used for attribute dictionaries).

    14. jimjjewett commented on Feb 29, 2012

      jimjjewettmannequin
      Mannequin

      As of Feb 28, 2012, the PEP mentions an additional optimization of storing the values in an array indexed by (effectively) key insertion order, rather than key position. ("Alternative Implementation")

      It states that this would reduce memory usage for the values array by 1/3. 1/3 is a worst-case measurement; average is 1/2. (At savings of less than 1/3, the keys would resize, to initial savings of 2/3. And yes, that means in practice, the average savings would be greater than half, because the frequency of dicts of size N decreases with N.)

      It states that the keys table would need an additional "values_size" field, but in the absence of dummies, this is just ma_used.

      Note that this would also simplify resizing, as the values arrays would not have to be re-ordered, and would not have to be modified at all unless/until that particular instance received a value for a position beyond its current size.

    15. 21 remaining items

    16. python-dev commented on Apr 23, 2012

      python-devmannequin
      Mannequin

      New changeset 34b6998efd2c by Benjamin Peterson in branch 'default':
      fix instance dicts with str subclasses (bpo-13903)
      http://hg.python.org/cpython/rev/34b6998efd2c

    17. markshannon commented on Apr 24, 2012

      @markshannon
      MemberAuthor

      Failing to maintain GC tracking in setdefault and copy (for split-tables)

      Patch attached

    18. python-dev commented on Apr 24, 2012

      python-devmannequin
      Mannequin

      New changeset 507a6703d6a3 by Benjamin Peterson in branch 'default':
      fix dict gc tracking (bpo-13903)
      http://hg.python.org/cpython/rev/507a6703d6a3

    19. markshannon commented on Apr 24, 2012

      @markshannon
      MemberAuthor

      Failed to differentiate between failure and error in make_split_table().

      Patch attached

    20. python-dev commented on Apr 24, 2012

      python-devmannequin
      Mannequin

      New changeset b044e0568be2 by Martin v. Loewis in branch 'default':
      Account for shared keys in type's __sizeof__ (bpo-13903).
      http://hg.python.org/cpython/rev/b044e0568be2

    21. python-dev commented on Apr 24, 2012

      python-devmannequin
      Mannequin

      New changeset 5d5b72a71898 by Benjamin Peterson in branch 'default':
      distiguish between refusing to creating shared keys and error (bpo-13903)
      http://hg.python.org/cpython/rev/5d5b72a71898

    22. markshannon commented on Apr 27, 2012

      @markshannon
      MemberAuthor

      Decref cached-keys when type is deallocated.

      Patch attached.

    23. python-dev commented on Apr 27, 2012

      python-devmannequin
      Mannequin

      New changeset a3beae842f13 by Benjamin Peterson in branch 'default':
      decref cached keys on type deallocation (bpo-13903)
      http://hg.python.org/cpython/rev/a3beae842f13

    24. pitrou commented on Apr 27, 2012

      @pitrou
      Member

      New changeset a3beae842f13 by Benjamin Peterson in branch 'default':
      decref cached keys on type deallocation (bpo-13903)
      http://hg.python.org/cpython/rev/a3beae842f13

      Is there any way to detect / avoid leaks on this separate refcounting
      scheme?

    25. pitrou commented on Apr 27, 2012

      @pitrou
      Member

      This patch integrates the dictkeys' refcounting into the refcount checking framework. Seems to work ok, but it would be better if someone more acquainted with the code could confirm it.

    26. markshannon commented on Apr 30, 2012

      @markshannon
      MemberAuthor

      Change insertdict to follow normal (non-stealing) ref-counting behaviour which fixes possible leakage.

      Patch attached.

    27. python-dev commented on Apr 30, 2012

      python-devmannequin
      Mannequin

      New changeset c5fd332e5857 by Benjamin Peterson in branch 'default':
      change insertdict to not steal references (bpo-13903)
      http://hg.python.org/cpython/rev/c5fd332e5857

    28. python-dev commented on May 12, 2012

      python-devmannequin
      Mannequin

      New changeset 10e8b97d0fd7 by Antoine Pitrou in branch 'default':
      Make the reference counting of dictkeys objects participate in refleak hunting
      http://hg.python.org/cpython/rev/10e8b97d0fd7

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

    No one assigned

      Labels

      interpreter-coreperformance

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions