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