Stop parsing a whole page to read its breadcrumb, and work out the shown lists once per render

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-20 15:52:34 -07:00
1 parent 3bd6da1b19
commit d1e74f3e76
3 files changed
+35 -1

No files matched your search

+23
View File
@@ -15,6 +15,7 @@ A list with a `noun` also has a page for each state or province where at least
MIN_SIZE of its trails are, and fewer than MAX_SHARE of that place's (olt/placefacets.py).
A list belongs to a `group` (GROUPS), the heading it sits under on /lists/.
"""
import contextlib
import re
from dataclasses import dataclass
from typing import Callable, Optional
@@ -271,13 +272,35 @@ def is_shown(n, total, least=0):
return n >= max(MIN_SIZE, least) and n <= MAX_SHARE * total
_SHOWN_MEMO = None # while a site is being rendered: {id(records): (records, shown)}
@contextlib.contextmanager
def remembering_shown():
"""Within this block shown() works each list of records out once. A site render asks
for it from every state, place and list page; the records do not change meanwhile."""
global _SHOWN_MEMO
outer, _SHOWN_MEMO = _SHOWN_MEMO, {}
try:
yield
finally:
_SHOWN_MEMO = outer
def shown(records):
"""[(list, its trails)] for every list that passes is_shown, in LISTS order."""
if _SHOWN_MEMO is not None:
held = _SHOWN_MEMO.get(id(records))
if held is not None and held[0] is records:
return list(held[1])
out = []
for lst in LISTS:
recs = members(lst, records)
if is_shown(len(recs), len(records), lst.min_size):
out.append((lst, recs))
if _SHOWN_MEMO is not None:
_SHOWN_MEMO[id(records)] = (records, out)
return list(out)
return out
+5
View File
@@ -90,6 +90,11 @@ def not_found_page(site):
def render_site(records, out_dir, repo_root, site=None):
"""Write every HTML page for the given records. Returns the number of pages written."""
with browse.remembering_shown():
return _render_site(records, out_dir, repo_root, site)
def _render_site(records, out_dir, repo_root, site=None):
site = site or site_settings.load()
copy_assets(out_dir)
sketch.use(out_dir) # the maps' static drawings are read from the track files written here
+7 -1
View File
@@ -186,7 +186,13 @@ class _Crumbs(HTMLParser):
def visible_breadcrumbs(body):
"""[(text, href)] of the breadcrumb a page body shows; the last item has no href."""
parser = _Crumbs()
parser.feed(str(body))
text = str(body)
# The breadcrumb is near the top of a body: feed it in pieces and stop once its nav has
# closed, rather than parsing a whole page of rows.
for i in range(0, len(text), 8192):
parser.feed(text[i:i + 8192])
if parser._done:
break
return parser.items