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

Instance dictionaries should be created eagerly #88984

Closed
markshannon opened this issue Aug 4, 2021 · 3 comments
Closed

Instance dictionaries should be created eagerly #88984

markshannon opened this issue Aug 4, 2021 · 3 comments
Assignees
Labels
3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage

Comments

@markshannon
Copy link
Member

BPO 44821
Nosy @terryjreedy, @markshannon
PRs
  • bpo-44821: Eagerly assign __dict__ for new objects. #27589
  • 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/markshannon'
    closed_at = <Date 2021-08-09.14:57:19.689>
    created_at = <Date 2021-08-04.07:57:16.295>
    labels = ['interpreter-core', '3.11', 'performance']
    title = 'Instance dictionaries should be created eagerly'
    updated_at = <Date 2021-08-09.14:57:19.689>
    user = 'https://github.com/markshannon'

    bugs.python.org fields:

    activity = <Date 2021-08-09.14:57:19.689>
    actor = 'Mark.Shannon'
    assignee = 'Mark.Shannon'
    closed = True
    closed_date = <Date 2021-08-09.14:57:19.689>
    closer = 'Mark.Shannon'
    components = ['Interpreter Core']
    creation = <Date 2021-08-04.07:57:16.295>
    creator = 'Mark.Shannon'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44821
    keywords = ['patch']
    message_count = 3.0
    messages = ['398864', '398908', '399148']
    nosy_count = 2.0
    nosy_names = ['terry.reedy', 'Mark.Shannon']
    pr_nums = ['27589']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue44821'
    versions = ['Python 3.11']

    @markshannon
    Copy link
    Member Author

    Currently, instance dictionaries (dict attribute) are created lazily when the first attribute is set.

    This is bad for performance for a number of reasons:

    1. It causes additional checks on every attribute access.
    2. It causes allocation of the object and its dict to be temporarily separated, most likely leading to increased physical separation and worse cache behavior.
    3. It has a large impact on specialization, as the first SET_ATTR for an object has to behave differently.

    Creating a __dict__ lazily does not save a significant amount of memory.
    If an object has a __dict__ slot, then it will end up with a dictionary before it dies in almost all cases.

    Many objects, e.g. ints, floats, don't have a dictionary. They are unaffected.

    Plain python objects that have no instance attributes are extremely rare, unless they have __slots__, in which case they don't have a dictionary anyway.

    The remaining case is subclasses of builtin types that do not add extra attributes, but these are rare, and the overhead of an empty dictionary is only 64 bytes (on a 64 bit machine).

    @markshannon markshannon added the 3.11 only security fixes label Aug 4, 2021
    @markshannon markshannon self-assigned this Aug 4, 2021
    @markshannon markshannon added interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage labels Aug 4, 2021
    @markshannon
    Copy link
    Member Author

    New changeset cee67fa by Mark Shannon in branch 'main':
    bpo-44821: Eagerly assign __dict__ for new objects. (GH-27589)
    cee67fa

    @terryjreedy
    Copy link
    Member

    Close this?

    @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
    3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants