Skip to content

Commit

Permalink
Speed up site rendering by a factor of 4 (#1994)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mattdangerw authored Nov 26, 2024
1 parent bb6afbc commit 01f7039
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions scripts/autogen.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ def make_md_source_for_entry(self, entry, path_stack, title_stack):
for entry in children:
self.make_md_source_for_entry(entry, path_stack[:], title_stack[:])

def make_map_of_symbol_names_to_api_urls(self):
def make_symbol_to_link_map(self):
def recursive_make_map(entry, current_url):
current_url /= entry["path"]
entry_map = {}
Expand All @@ -616,9 +616,12 @@ def recursive_make_map(entry, current_url):
entry_map.update(recursive_make_map(child, current_url))
return entry_map

self._map_of_symbol_names_to_api_urls = recursive_make_map(
self.master, Path("")
)
urls = recursive_make_map(self.master, Path(""))
self._symbol_to_link_map = {}
for key, value in urls.items():
symbol = f"`{key}`"
link = f"[{symbol}]({value})"
self._symbol_to_link_map[symbol] = link

def generate_examples_landing_page(self):
"""Create the html file /examples/index.html.
Expand Down Expand Up @@ -740,7 +743,7 @@ def generate_examples_landing_page(self):
)

def render_md_sources_to_html(self):
self.make_map_of_symbol_names_to_api_urls()
self.make_symbol_to_link_map()
print("Rendering md sources to HTML")
base_template = jinja2.Template(open(Path(self.theme_dir) / "base.html").read())
docs_template = jinja2.Template(open(Path(self.theme_dir) / "docs.html").read())
Expand Down Expand Up @@ -886,12 +889,8 @@ def render_single_file(self, src_location, fname, nav):
md_content = replace_links(md_content)

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

# Convert TF symbols to links to tensorflow.org
tmp_content = copy.copy(md_content)
Expand Down

0 comments on commit 01f7039

Please sign in to comment.