Description
For example, ./python -m pydoc test.test_enum
takes 32 seconds. It is 20 seconds in 3.12, 15 seconds in 3.11 and only 1.6 seconds in 3.10. Well, perhaps test.test_enum
was grown, but the main culprit is bpo-35113. And further changes like gh-106727 only added to it.
For every class without a docstring pydoc
tries to find its comments by calling inspect.getcomments()
which calls inspect.findsource()
which reads and parses the module source, then traverse it and find classes with the specific qualname. For large modules with many classes it has quadratic complexity.
I tried to optimize the AST traversing code, and get 18 seconds on main. It still has quadratic complexity. Further optimization will require introducing a cache and finding positions of all classes in one pass.
But it all would be much simpler and faster if simply save the value of co_firstlineno
of the code object executed during class creation in the file dict (as __firstlineno__
for example).