From d1e74f3e76cbc9b5618412434acda517be5a8767 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Fri, 20 Nov 2026 15:52:34 -0700 Subject: [PATCH] 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 --- web/olt/browse.py | 23 +++++++++++++++++++++++ web/olt/pages.py | 5 +++++ web/olt/seo.py | 8 +++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/web/olt/browse.py b/web/olt/browse.py index c8e08e3..d25d3d8 100644 --- a/web/olt/browse.py +++ b/web/olt/browse.py @@ -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 diff --git a/web/olt/pages.py b/web/olt/pages.py index dbd4241..6ef51de 100644 --- a/web/olt/pages.py +++ b/web/olt/pages.py @@ -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 diff --git a/web/olt/seo.py b/web/olt/seo.py index f61744b..b2f6aae 100644 --- a/web/olt/seo.py +++ b/web/olt/seo.py @@ -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