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

Add faster TocTree._toctree_copy method #10988

Merged
merged 4 commits into from Jan 2, 2023
Merged

Conversation

hofmandl1
Copy link
Contributor

This was pair-programmed with @ax-lothas

As in the standalone html builder the navigation is flattened out for every single html page, the code needs to create a specialized toctree for every html page.

Previously this was done by deep-copying the complete navigation toctree and then stripping out the parts not needed on the particular page.

With this change the code only (deep)-copies the needed parts of the toctree avoiding unnecessary copying+throwing-away

The performance improvements seems to be smaller for smaller page counts and get bigger the more pages are involved:

+----------------------------------+-----------+-----------+-------------------+
|         830 Source Files         |   5.3.0   |  5.3.0mod |       Ratio       |
+----------------------------------+-----------+-----------+-------------------+
|          SingleThreaded          |  49.137 s |  43.517 s | 0.885625903087287 |
| Parallel (auto, 16 logical CPUs) |  13.334 s |  12.849 s | 0.963626818659067 |
+----------------------------------+-----------+-----------+-------------------+

+----------------------------------+-----------+-----------+-------------------+
|        6166 Source Files         |   5.3.0   |  5.3.0mod |       Ratio       |
+----------------------------------+-----------+-----------+-------------------+
|          SingleThreaded          | 907.497 s | 521.945 s | 0.575147906825036 |
| Parallel (auto, 16 logical CPUs) | 195.400 s | 150.139 s | 0.768367451381781 |
+----------------------------------+-----------+-----------+-------------------+

Relates

This was pair-programmed with @ax-lothas

As in the standalone html builder the navigation is flattened out
for every single html page, the code needs to create a specialized toctree
for every html page.

Previously this was done by deep-copying the complete navigation toctree
and then stripping out the parts not needed on the particular page.

With this change the code only (deep)-copies the needed parts of the toctree
avoiding unnecessary copying+throwing-away

The performance improvements seems to be smaller for smaller page counts and
get bigger the more pages are involved:

+----------------------------------+-----------+-----------+-------------------+
|         830 Source Files         |   5.3.0   |  5.3.0mod |       Ratio       |
+----------------------------------+-----------+-----------+-------------------+
|          SingleThreaded          |  49.137 s |  43.517 s | 0.885625903087287 |
| Parallel (auto, 16 logical CPUs) |  13.334 s |  12.849 s | 0.963626818659067 |
+----------------------------------+-----------+-----------+-------------------+

+----------------------------------+-----------+-----------+-------------------+
|        6166 Source Files         |   5.3.0   |  5.3.0mod |       Ratio       |
+----------------------------------+-----------+-----------+-------------------+
|          SingleThreaded          | 907.497 s | 521.945 s | 0.575147906825036 |
| Parallel (auto, 16 logical CPUs) | 195.400 s | 150.139 s | 0.768367451381781 |
+----------------------------------+-----------+-----------+-------------------+
def _toctree_copy(self, node: ET, depth: int, maxdepth: int, collapse: bool = False) -> ET:
"""Utility: deep-copy a TOC but omit things with the semantics of ._toctree_prune.

!!! MUST be kept consistent with ._toctree_prune !!!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by 'consistent' here?

A

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While _toctree_prune deletes unneeded stuff from a (previously done) deep copy of the toctree, _toctree_copy does the deep copy itself but only copies the parts, that _toctree_prune would not delete. So assuming, that _toctree_prune would be adjusted in order to e.g. not delete a particular node type, then _toctree_copy would also need to be adjusted to copy that particular node type.

TBH we didn't fully understand the different node types involved here. It would probably be best, to completely get rid of _toctree_prune in order not to have this consistency problem any more (maybe it is not that big of a problem if there aren't any adjustments to that code done in the foreseeable future). We tried getting rid of _toctree_prune but found that it is used in another place and didn't feel confident enough to do the necessary changes.

@AA-Turner AA-Turner changed the title Speed up navigation generation for "-M html" builder Add faster TocTree._toctree_copy method Jan 2, 2023
@AA-Turner AA-Turner merged commit b26b9ba into sphinx-doc:master Jan 2, 2023
@AA-Turner
Copy link
Member

Thanks!

A

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants