git » blitiri » commit 2910dc5

Defer docutils imports

author Alberto Bertogli
2022-08-20 22:40:32 UTC
committer Alberto Bertogli
2022-08-20 22:40:32 UTC
parent 162db6602a5dbde94498b9eddeb4e1f3acb10f73

Defer docutils imports

Importing docutils is ~30% of our startup cost, which is very
significant.

This patch defers the imports of docutils objects to when they're
needed, to speed things up.

blitiri.cgi +4 -2

diff --git a/blitiri.cgi b/blitiri.cgi
index 3c2cf52..7c01da1 100755
--- a/blitiri.cgi
+++ b/blitiri.cgi
@@ -95,8 +95,6 @@ import sys
 import time
 import urllib.request, urllib.parse, urllib.error
 import zlib
-from docutils.core import publish_parts
-from docutils.utils import SystemMessage
 
 # Before importing the config, add our cwd to the Python path
 sys.path.append(os.getcwd())
@@ -523,6 +521,8 @@ def cached(f):
 # helper functions
 @cached
 def rst_to_html(rst, secure=True):
+    # Moving these here speeds up execution significantly on cache hit.
+    from docutils.core import publish_parts
     settings = {
         "input_encoding": encoding,
         "output_encoding": "utf8",
@@ -536,6 +536,8 @@ def rst_to_html(rst, secure=True):
 
 
 def validate_rst(rst, secure=True):
+    # Moving these here speeds up execution significantly on cache hit.
+    from docutils.utils import SystemMessage
     try:
         rst_to_html(rst, secure)
         return None