Skip to content

Declare sphinxext.redirect_from parallel_read_safe #19686

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

Merged
merged 1 commit into from
Apr 29, 2021

Conversation

timhoffm
Copy link
Member

@timhoffm timhoffm commented Mar 11, 2021

PR Summary

The new redirect_from plugin prevented sphinx from running in parallel and changed the doc build performance from awfully slow to insanely slow.

This change improves the performances again to awfully slow 😄 . It's only relevant for people who arebuilding the docs in parallel (make html -j4).

@anntzer
Copy link
Contributor

anntzer commented Mar 11, 2021

But is is actually parallel_read_safe as is? Should we not somehow merge the redirects dicts populated by each parallel reader?

@timhoffm
Copy link
Member Author

You're right. I was only thinking about threas-safety, but these run on different processes. Of the top of my head I'm not clear how to do the communication. OTOH, I don't need all the redirect pages in my local docs, so I could as well add a switch that off. CI is single-process anyway.

@timhoffm timhoffm marked this pull request as draft March 12, 2021 00:41
@anntzer
Copy link
Contributor

anntzer commented Mar 12, 2021

I think env-merge-info (https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx-core-events) is where to merge the different readers' info (which likely means redirects need to be stored on the env, not the directive).

@anntzer
Copy link
Contributor

anntzer commented Mar 12, 2021

Untested, but I think you need something like

diff --git i/doc/sphinxext/redirect_from.py w/doc/sphinxext/redirect_from.py
index d8aa487b0..f480ced3b 100644
--- i/doc/sphinxext/redirect_from.py
+++ w/doc/sphinxext/redirect_from.py
@@ -48,12 +48,14 @@ HTML_TEMPLATE = """<html>
 def setup(app):
     RedirectFrom.app = app
     app.add_directive("redirect-from", RedirectFrom)
+    app.connect("env-before-read-docs", _setup_redirects)
+    app.connect("env-merge-info", _merge_redirects)
     app.connect("build-finished", _generate_redirects)
+    return {"parallel_read_safe": True}
 
 
 class RedirectFrom(Directive):
     required_arguments = 1
-    redirects = {}
 
     def run(self):
         redirected_doc, = self.arguments
@@ -61,20 +63,32 @@ class RedirectFrom(Directive):
         builder = self.app.builder
         current_doc = env.path2doc(self.state.document.current_source)
         redirected_reldoc, _ = env.relfn2path(redirected_doc, current_doc)
-        if redirected_reldoc in self.redirects:
+        if redirected_reldoc in env._redirects:
             raise ValueError(
                 f"{redirected_reldoc} is already noted as redirecting to "
-                f"{self.redirects[redirected_reldoc]}")
-        self.redirects[redirected_reldoc] = builder.get_relative_uri(
+                f"{env._redirects[redirected_reldoc]}")
+        env._redirects[redirected_reldoc] = builder.get_relative_uri(
             redirected_reldoc, current_doc)
         return []
 
 
+def _setup_redirects(app, env, docnames):
+    if not hasattr(env, "_redirects"):
+        env._redirects = {}
+
+
+def _merge_redirects(app, env, docnames, other):
+    for src, dst in other.items():
+        if src in env._redirects:
+            raise ValueError(f"{src} is already noted as redirecting to {dst}")
+        env._redirects[src] = dst
+
+
 def _generate_redirects(app, exception):
     builder = app.builder
     if builder.name != "html" or exception:
         return
-    for k, v in RedirectFrom.redirects.items():
+    for k, v in app.env._redirects.items():
         p = Path(app.outdir, k + builder.out_suffix)
         if p.is_file():
             logger.warning(f'A redirect-from directive is trying to create '

@timhoffm timhoffm force-pushed the redirect-parallel branch from 646053a to bf93529 Compare March 14, 2021 23:31
Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

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

This seems fine to me, but I didn't test. Did you test w multiple processes?

@timhoffm
Copy link
Member Author

It runs, but I still have to check, that it produces all the correct redirects.

@timhoffm timhoffm mentioned this pull request Apr 1, 2021
@timhoffm timhoffm force-pushed the redirect-parallel branch from bf93529 to 2900fda Compare April 24, 2021 22:37
@timhoffm
Copy link
Member Author

The above proposal adds a private attribute to the sphinx env, which feels very wrong. While that's used in the sphinx example documentation, it's not recommended.

New approach: Use a domain as data store for the redirects mapping.
That is how the sphinx todo extension handles data and was suggested
sphinx-doc/sphinx#9003 (comment)

@timhoffm timhoffm marked this pull request as ready for review April 25, 2021 19:13
This uses a domain as data store for the redirects sphinx plugin.
That is how the sphinx todo extension handles data and was suggested in
sphinx-doc/sphinx#9003 (comment)

Limit documenting special members to __call__

Closes matplotlib#20080.

Follow up to matplotlib#17151.
@timhoffm timhoffm force-pushed the redirect-parallel branch from 2900fda to 4d248b2 Compare April 28, 2021 20:50
@QuLogic QuLogic added this to the v3.4.2 milestone Apr 29, 2021
@QuLogic QuLogic merged commit 0ac5c29 into matplotlib:master Apr 29, 2021
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Apr 29, 2021
QuLogic added a commit that referenced this pull request Apr 29, 2021
…686-on-v3.4.x

Backport PR #19686 on branch v3.4.x (Declare sphinxext.redirect_from parallel_read_safe)
@timhoffm timhoffm deleted the redirect-parallel branch April 29, 2021 05:48
@QuLogic
Copy link
Member

QuLogic commented May 8, 2021

When building 3.4.2 docs, no redirects were produced, so there might some bug here.

@jklymak
Copy link
Member

jklymak commented Dec 7, 2021

As pointed out above and in #21876, this doesn't actually produce redirects...

@timhoffm
Copy link
Member Author

timhoffm commented Dec 7, 2021

Ok, let's revert this for now. Local built times have already increased to a level that I'm mostly not trying to build locally anymore :(. And CI doesn't use parallel builds AFAIK. @jklymak feel free to revert, I don't have Repo access right now.

The local build time needs special investigation. Probably one can just deactivate redirects there.

@jklymak
Copy link
Member

jklymak commented Dec 7, 2021

(OT: For what ever reason parallel builds are far slower for me than non-parallel!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants