Skip to content

Commit 01f7039

Browse files
authored
Speed up site rendering by a factor of 4 (#1994)
The slow part of rendering was compiling and running regexes for symbol -> link replacement. Moving to string.replace instead of a regex is dramatically faster. I'm ditching the the tf.keras link replacement code, there's very few and we don't want to encourage that style of link anyway.
1 parent bb6afbc commit 01f7039

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

scripts/autogen.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ def make_md_source_for_entry(self, entry, path_stack, title_stack):
594594
for entry in children:
595595
self.make_md_source_for_entry(entry, path_stack[:], title_stack[:])
596596

597-
def make_map_of_symbol_names_to_api_urls(self):
597+
def make_symbol_to_link_map(self):
598598
def recursive_make_map(entry, current_url):
599599
current_url /= entry["path"]
600600
entry_map = {}
@@ -616,9 +616,12 @@ def recursive_make_map(entry, current_url):
616616
entry_map.update(recursive_make_map(child, current_url))
617617
return entry_map
618618

619-
self._map_of_symbol_names_to_api_urls = recursive_make_map(
620-
self.master, Path("")
621-
)
619+
urls = recursive_make_map(self.master, Path(""))
620+
self._symbol_to_link_map = {}
621+
for key, value in urls.items():
622+
symbol = f"`{key}`"
623+
link = f"[{symbol}]({value})"
624+
self._symbol_to_link_map[symbol] = link
622625

623626
def generate_examples_landing_page(self):
624627
"""Create the html file /examples/index.html.
@@ -740,7 +743,7 @@ def generate_examples_landing_page(self):
740743
)
741744

742745
def render_md_sources_to_html(self):
743-
self.make_map_of_symbol_names_to_api_urls()
746+
self.make_symbol_to_link_map()
744747
print("Rendering md sources to HTML")
745748
base_template = jinja2.Template(open(Path(self.theme_dir) / "base.html").read())
746749
docs_template = jinja2.Template(open(Path(self.theme_dir) / "docs.html").read())
@@ -886,12 +889,8 @@ def render_single_file(self, src_location, fname, nav):
886889
md_content = replace_links(md_content)
887890

888891
# Convert Keras symbols to links to the Keras docs
889-
for symbol, symbol_url in self._map_of_symbol_names_to_api_urls.items():
890-
md_content = re.sub(
891-
r"`((tf\.|)" + symbol + ")`",
892-
r"[`\1`](" + symbol_url + ")",
893-
md_content,
894-
)
892+
for symbol, link in self._symbol_to_link_map.items():
893+
md_content = md_content.replace(symbol, link)
895894

896895
# Convert TF symbols to links to tensorflow.org
897896
tmp_content = copy.copy(md_content)

0 commit comments

Comments
 (0)