Skip to content

DOC: Speed up Sphinx build by using sphinxcontrib-globalsubs - #16162

Merged
pllim merged 10 commits into
astropy:mainfrom
namurphy:sphinx-globalsubs
Mar 8, 2024
Merged

pllim merged 10 commits into
astropy:mainfrom
namurphy:sphinx-globalsubs

Conversation

@namurphy

@namurphy namurphy commented Mar 5, 2024 •

Copy link
Copy Markdown
Contributor

Astropy presently uses docs/common_links.txt to store global substitutions that can be used throughout the documentation. The contents of this file are read in through docs/conf.py and then added to rst_prolog. Because rst_prolog gets added to the beginning of every reStructuredText file and docstring, a lengthy rst_prolog can add minutes to the documentation build (see #15149). 🐌

The sphinxcontrib-globalsubs extension allows us to create the global_substitutions dictionary in docs/conf.py to contain substitutions for use throughout the documentation, eliminating the need to prepend rst_prolog to files and docstrings.

A clean doc build prior to this PR locally took 7.2 min. After moving most of the common links to global_substitutions in docs/conf.py, a clean documentation build took 5.8 min. The doc build was shortened by 20%. 🎆 For these timings, I used make html -j 2 so that the documentation build was done on two processors. (These times do not include the creation of a Python environment.)

Because sphinxcontrib-globalsubs only works with substitutions, I had to convert some links in the docs to substitutions (i.e., SciPy_ → |SciPy|).

I still need to clean this up a bit & document this a bit better. There's still some content in docs/common_links.txt that will require a bit more work. I'd also like to define these substitutions in a separate file which can then get imported to docs/conf.py, but I had some trouble getting that to work, so I may do this in a follow-up PR.

Closes #15149. This was inspired by PlasmaPy/PlasmaPy#2277, PlasmaPy/PlasmaPy#2280, & PlasmaPy/PlasmaPy#2281. There's some related discussion on how to speed up the documentation build in CI in #16161.

@github-actions

github-actions Bot commented Mar 5, 2024

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@github-actions

github-actions Bot commented Mar 5, 2024

Copy link
Copy Markdown
Contributor

👋 Thank you for your draft pull request! Do you know that you can use [ci skip] or [skip ci] in your commit messages to skip running continuous integration tests until you are ready?

@pllim

pllim commented Mar 5, 2024

Copy link
Copy Markdown
Member

For the record, the last latest build took 1038 seconds or 17.3 minutes (https://readthedocs.org/projects/astropy/builds/23646203/).

@astrofrog

Copy link
Copy Markdown
Member

This is fantastic! I've always wondered how we could get rid of the rst_prolog so great to see this solution 😄

@namurphy

namurphy commented Mar 5, 2024

Copy link
Copy Markdown
Contributor Author

This is fantastic! I've always wondered how we could get rid of the rst_prolog so great to see this solution 😄

Another win for procrastination-driven development! 😹

@pllim

pllim commented Mar 5, 2024 •

Copy link
Copy Markdown
Member

I just checked, the RTD build for this PR took 926 seconds or 15.4 mins. That's 10% speed up?

https://readthedocs.org/projects/astropy/builds/23658770/

@namurphy namurphy changed the title DOC: Speed up documentation build by ∼20% by using sphinxcontrib-globalsubs DOC: Speed up Sphinx build by ∼20% by using sphinxcontrib-globalsubs Mar 5, 2024
@namurphy namurphy changed the title DOC: Speed up Sphinx build by ∼20% by using sphinxcontrib-globalsubs DOC: Speed up Sphinx build by using sphinxcontrib-globalsubs Mar 6, 2024
@namurphy

namurphy commented Mar 6, 2024 •

Copy link
Copy Markdown
Contributor Author

I just checked, the RTD build for this PR took 926 seconds or 15.4 mins. That's 10% speed up?

Thank you for checking this! The ∼20% improvement was on the sphinx-build step, and doesn't include the time needed to create the Python environment, etc.

It looks like the sphinx-build step itself was reduced from ∼710 s to ∼661 s on RTD. That's less of an improvement than I found locally, but I'll still take it!

@eerovaher eerovaher left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I can approve for coordinates.

@pllim

pllim commented Mar 6, 2024

Copy link
Copy Markdown
Member

@namurphy , when do you think this is ready to be taken out of draft? Thanks!

@namurphy

namurphy commented Mar 7, 2024

Copy link
Copy Markdown
Contributor Author

I'm still figuring out moving the remaining contents of common_links.txt over. The most recent RTD build gave the following errors several thousand times:

/home/docs/checkouts/readthedocs.org/user_builds/astropy/checkouts/16162/docs/<rst_epilog>:3: WARNING: py:obj reference target not found: global_substitutions
/home/docs/checkouts/readthedocs.org/user_builds/astropy/checkouts/16162/docs/<rst_epilog>:3: WARNING: py:obj reference target not found: docs/conf.py

I think...this can wait until tomorrow!

@namurphy

namurphy commented Mar 7, 2024

Copy link
Copy Markdown
Contributor Author

pre-commit.ci autofix

Comment thread pyproject.toml
"sphinx_design",
"Jinja2>=3.1.3",
"tomli; python_version < '3.11'",
"sphinxcontrib-globalsubs >= 0.1.1",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

One possible concern is that this is an early version of sphinxcontrib-globalsubs, which was released last year. But, we've been using it in PlasmaPy for a while now with no problems, and it wouldn't be that much work to go back to common_links.txt.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since this only affects doc build and RTD is green, probably okay.

@namurphy

namurphy commented Mar 7, 2024

Copy link
Copy Markdown
Contributor Author

Ready for review! The most recent build took 878 s, which is about 15% less than the ∼1038 s baseline.

@namurphy
namurphy marked this pull request as ready for review March 7, 2024 17:17
@namurphy
namurphy requested a review from nstarman March 7, 2024 17:17

@nstarman nstarman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Cosmo and Units LGTM. Thanks @namurphy for the significant speedup!

@taldcroft taldcroft left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fine for io.ascii and time.

@pllim pllim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Final timing:

  • This PR: Completed March 7, 2024. 4:20 p.m. Build took 878 seconds 🚀
  • main: Completed March 8, 2024. 2:46 a.m. Build took 1035 seconds

I think we have enough approvals. Let's speed things up. Thank you very much!

Comment thread docs/conf.py
"StructuredUnit": ":class:`~astropy.units.StructuredUnit`",
# Utils
"Masked": ":class:`~astropy.utils.masked.Masked`",
# Minimum versions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we lost the ability to be able to dynamically insert new minversion listing for dependencies (which tend to keep growing nowdays).

That said, docs/conf.py is easier to discover than docs/common_links.txt in general, plus the speed up. So the benefit of this PR outweighs the risk of forgetting to insert a new entry here.

<https://www.python.org/dev/peps/pep-0008/>`_. All of the PEP8 testing
so that they check Python source code for |PEP8|. All of the PEP8 testing
options require the `pytest-pep8 plugin
<https://pypi.org/project/pytest-pep8>`_, which must be installed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Side note: I never even installed pytest-pep8. The dev guide could probably use some serious clean-up but that is a battle for another day.

Comment thread pyproject.toml
"sphinx_design",
"Jinja2>=3.1.3",
"tomli; python_version < '3.11'",
"sphinxcontrib-globalsubs >= 0.1.1",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since this only affects doc build and RTD is green, probably okay.

@pllim
pllim merged commit a173fbf into astropy:main Mar 8, 2024
@namurphy
namurphy deleted the sphinx-globalsubs branch March 14, 2024 21:22
d-giles referenced this pull request in d-giles/astropy-abdufork Jul 26, 2024
* Apply sphinxcontrib-globalsubs to substitutions across the package

* Remove unused substitutions/links

* Add comments to conf.py

* Move over common_links.rst to conf.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Use sphinxcontrib-globalsubs to define reStructuredText substitutions currently in docs/common_links.txt

6 participants