Skip to content

Pickling objects by name without __module__ is suboptimal #122459

Closed

Description

@serhiy-storchaka

When the object is pickled by name (classes, functions, and other enum-like objects) has no __module__ attribute or it is None, the code searches the object in all imported modules: tries to resolve its name relatively to the module and check that the result is the same object.

Whether the module was get from the __module__ attribute or found in sys.modules, the code checks it again:
Then it checks again: tries to resolve its name relatively to the module and check that the result is the same object.

So in some the work is repeated twice. The following PR makes it only be performed once. It also contains few other minor optimizations and simplifications.

Linked PRs

Activity

  1. added a commit that references this issue on Jul 30, 2024
  2. serhiy-storchaka commented on Aug 3, 2024

    @serhiy-storchaka
    MemberAuthor

    I did not expect significant effect from this change, but it make pickling a function without __module__ with the Python implementation twice faster:

    $ ./python -m timeit -s "import sys; sys.modules['_pickle'] = None; import pickle; global func" -s "def func(): pass" -s "func.__module__ = None" -- "pickle.dumps(func)"
    
    Before: 2000 loops, best of 5: 192 usec per loop
    After:  2000 loops, best of 5: 92.3 usec per loop
    

    This affects also pickling Ellipsis and NotImplemented singletons:

    $ ./python -m timeit -s "import sys; sys.modules['_pickle'] = None; import pickle" -- "pickle.dumps(...)"
    
    Before: 20000 loops, best of 5: 14.1 usec per loop
    After:  20000 loops, best of 5: 10.6 usec per loop
    

    The main purpose is of course cleaning up the code.

  3. added a commit that references this issue on Aug 5, 2024
  4. added a commit that references this issue on Aug 7, 2024
  5. added a commit that references this issue on Aug 22, 2024
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

    3.14bugs and security fixesperformancePerformance or resource usage

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions