AndrewRqy commited on
Commit ·
500c05f
1
Parent(s): 079fead
Performance sweep: gr.skip diff + memoize hot HTML helpers per click transition
Browse files429 unmodified lines4304314324334344354364379 unmodified lines447448449450451452453454455456457458195 unmodified lines6546556566576586596606616626636646656666676686696706716724 unmodified lines67767867968068168268368468568668768868937 unmodified lines72772872973073173273310 unmodified lines7447457467477487497 unmodified lines7577587597607617622211 unmodified lines297429752976297729782979298029816 unmodified lines29882989299029912992299329942995299629972998809 unmodified lines380838093810381138123813381451 unmodified lines386638673868386938703871429 unmodified lines"""Render the left page: wizard's text for the current inscribe step,with hero/village placeholders substituted, the step marker in thecorner, and the step-themed pixel sigil tucked into the bottom-innercorner."""raw = _wizard_text_for(state.lang or "en", state.inscribe_step)raw = (raw.replace("{hero_name}", state.hero_name or "Tobin").replace("{village_name}", state.village_name or "the Hollow"))9 unmodified lines+ _sigil_svg(state.inscribe_step)+ "</div>")# Wrap in a lang-tagged div so the CSS drop-cap rule (which mangles# Chinese characters into chunky pixel-block initials) can be# disabled selectively for `lang="zh"`. The page-counter marker goes# AFTER the body so it flows below the text instead of absolute-# positioned over it.return f"<div lang='{lang}' class='grimoire-text-body'>{body}</div>{marker}{sigil}"def _grimoire_right_sigil_html(state: GameState) -> str:195 unmodified linessealed oracles passing between them. Sets the whole game's framing —'YOU are the wizard.'If the wizard_desk_scene Klein texture exists, it's used as a backdropbehind the wizard/hero figures (warm candle + parchment + quill imagerythat doubles down on the inscribe-phase atmosphere).Falls back to a plain text caption if the sprites haven't beengenerated yet (e.g. on a fresh checkout before Klein has run)."""# Theme-aware sprite lookup. Prefer theme-specific files# (e.g. wizard_space_cowboy.png), fall back to the canonical fantasy# sprites if the theme-specific file isn't on disk yet.from oracles.themes import get_theme as _get_themetheme_key_for_sprites = (getattr(state, "theme", "fantasy") if state else "fantasy") or "fantasy"wizard_uri = (_sprite_data_uri_app(f"wizard_{theme_key_for_sprites}")or _sprite_data_uri_app("wizard")4 unmodified lines)desk_uri = _sprite_data_uri_app("wizard_desk_scene")if not wizard_uri or not hero_uri:return ("<div class='oracle-inscribe-figs' style='text-align:center;""padding:14px;font-style:italic;color:var(--w-mute,#b4a890)'>""(sprites not yet generated — run ""<code>modal run oracles_app/modal_backend/modal_klein_oracles.py</code>)""</div>")bg_style = ""if desk_uri:# Layer the wizard-desk texture behind the figures at low opacity37 unmodified lineschampion_label = _md_safe(role_raw).upper()else:champion_label = _md_safe(_t("figure_apprentice", lang))return (f"<div class='oracle-inscribe-figs' style=\"{bg_style}\">"f"<div class='oracle-fig'>"f"<img src='{wizard_uri}' alt='mentor'/>"10 unmodified lines"</div>""</div>")# ---------------------------------------------------------------------------7 unmodified lines_THEME_STYLE_CACHE: dict = {}_INSCRIBE_DECORATION_CACHE: dict = {}_LOCALIZED_CHROME_CACHE: dict = {}def _banner_html(state: Optional[GameState] = None) -> str:2211 unmodified linesdef _badge_html(state: GameState, trial_substate: str = TRIAL_STATE_AWAIT) -> str:"""Small status chip shown under the banner — the title itself is inthe parallax banner so this is just a one-line state indicator."""lang = _tlang(state)if state.mode == "inscribe":label = _t("badge_inscribing", lang)elif state.mode == "send_off":6 unmodified lineslabel = _t("badge_return", lang)else:label = _t("badge_done", lang)return ("<div style='margin: 14px 0 4px 0;'>"f"<span class='oracle-badge'>{label}</span>""</div>")def _sealed_list_html(state: GameState) -> str:809 unmodified lines(insc_v, send_v, trial_v, await_v, reveal_v,epi_v, epi_end_v, epi_sum_v) = _visibility_for(state, trial_substate)rg = _grimoire_right_visibility(state) # 9 visibility updatesreturn (# 0..17: existing slots (unchanged)state,trial_substate,51 unmodified lines*_localized_chrome_updates(state),)# ---------------------------------------------------------------------------# Event handlers429 unmodified lines4304314324334344354364374384394404414424434444454464474484494504519 unmodified lines461462463464465466467468469195 unmodified lines6656666676686696706716726736746756766776786796806816824 unmodified lines68768868969069169269369469569669769869970070137 unmodified lines73974074174274374474510 unmodified lines7567577587597607617627637 unmodified lines7717727737747757767777782211 unmodified lines2990299129922993299429952996299729982999300030013002300330046 unmodified lines3011301230133014301530163017301830193020302130223023809 unmodified lines383338343835383638373838383951 unmodified lines38913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919429 unmodified lines"""Render the left page: wizard's text for the current inscribe step,with hero/village placeholders substituted, the step marker in thecorner, and the step-themed pixel sigil tucked into the bottom-innercorner.Cached by (lang, inscribe_step, hero_name, village_name). All fourdetermine the output exactly; nothing else does. On grimoire spreadtransitions, only ``inscribe_step`` changes, so the cache stays warm."""lang_code = state.lang or "en"cache_key = (lang_code,state.inscribe_step,state.hero_name or "Tobin",state.village_name or "the Hollow",)if cache_key in _GRIMOIRE_LEFT_CACHE:return _GRIMOIRE_LEFT_CACHE[cache_key]raw = _wizard_text_for(lang_code, state.inscribe_step)raw = (raw.replace("{hero_name}", state.hero_name or "Tobin").replace("{village_name}", state.village_name or "the Hollow"))9 unmodified lines+ _sigil_svg(state.inscribe_step)+ "</div>")result = f"<div lang='{lang}' class='grimoire-text-body'>{body}</div>{marker}{sigil}"_GRIMOIRE_LEFT_CACHE[cache_key] = resultreturn resultdef _grimoire_right_sigil_html(state: GameState) -> str:195 unmodified linessealed oracles passing between them. Sets the whole game's framing —'YOU are the wizard.'Cached by (theme, lang) — those are the only fields the rendered HTMLdepends on. Called on every click; caching saves a few hundred μs ofstring building per transition."""theme_key_for_sprites = (getattr(state, "theme", "fantasy") if state else "fantasy") or "fantasy"lang_for_cache = _tlang(state)cache_key = (theme_key_for_sprites, lang_for_cache)if cache_key in _INSCRIBE_DECORATION_CACHE:return _INSCRIBE_DECORATION_CACHE[cache_key]from oracles.themes import get_theme as _get_themewizard_uri = (_sprite_data_uri_app(f"wizard_{theme_key_for_sprites}")or _sprite_data_uri_app("wizard")4 unmodified lines)desk_uri = _sprite_data_uri_app("wizard_desk_scene")if not wizard_uri or not hero_uri:fallback = ("<div class='oracle-inscribe-figs' style='text-align:center;""padding:14px;font-style:italic;color:var(--w-mute,#b4a890)'>""(sprites not yet generated — run ""<code>modal run oracles_app/modal_backend/modal_klein_oracles.py</code>)""</div>")_INSCRIBE_DECORATION_CACHE[cache_key] = fallbackreturn fallbackbg_style = ""if desk_uri:# Layer the wizard-desk texture behind the figures at low opacity37 unmodified lineschampion_label = _md_safe(role_raw).upper()else:champion_label = _md_safe(_t("figure_apprentice", lang))result = (f"<div class='oracle-inscribe-figs' style=\"{bg_style}\">"f"<div class='oracle-fig'>"f"<img src='{wizard_uri}' alt='mentor'/>"10 unmodified lines"</div>""</div>")_INSCRIBE_DECORATION_CACHE[cache_key] = resultreturn result# ---------------------------------------------------------------------------7 unmodified lines_THEME_STYLE_CACHE: dict = {}_INSCRIBE_DECORATION_CACHE: dict = {}_LOCALIZED_CHROME_CACHE: dict = {}_GRIMOIRE_LEFT_CACHE: dict = {}_BADGE_CACHE: dict = {}def _banner_html(state: Optional[GameState] = None) -> str:2211 unmodified linesdef _badge_html(state: GameState, trial_substate: str = TRIAL_STATE_AWAIT) -> str:"""Small status chip shown under the banner — the title itself is inthe parallax banner so this is just a one-line state indicator.Cached by (lang, mode, current_trial). On a grimoire-spread clicknone of those change, so the cache hits every time."""lang = _tlang(state)cache_key = (lang, state.mode, state.current_trial)if cache_key in _BADGE_CACHE:return _BADGE_CACHE[cache_key]if state.mode == "inscribe":label = _t("badge_inscribing", lang)elif state.mode == "send_off":6 unmodified lineslabel = _t("badge_return", lang)else:label = _t("badge_done", lang)result = ("<div style='margin: 14px 0 4px 0;'>"f"<span class='oracle-badge'>{label}</span>""</div>")_BADGE_CACHE[cache_key] = resultreturn resultdef _sealed_list_html(state: GameState) -> str:809 unmodified lines(insc_v, send_v, trial_v, await_v, reveal_v,epi_v, epi_end_v, epi_sum_v) = _visibility_for(state, trial_substate)rg = _grimoire_right_visibility(state) # 9 visibility updatesnew_bundle = (# 0..17: existing slots (unchanged)state,trial_substate,51 unmodified lines*_localized_chrome_updates(state),)# Diff against the last bundle sent for this session and replace# unchanged elements with gr.skip(). The state is mutable and the same# reference flows through every handler, so we cache the previous# bundle directly on it. Slots 0 (state) and 1 (trial_substate) are# always returned in full — gr.State components track their values# by identity, not diff.last = getattr(state, "_last_bundle", None)state._last_bundle = list(new_bundle)if last is None or len(last) != len(new_bundle):return new_bundlediffed = list(new_bundle[:2])for i in range(2, len(new_bundle)):new_val = new_bundle[i]old_val = last[i]try:if new_val == old_val:diffed.append(gr.skip())continueexcept Exception:passdiffed.append(new_val)return tuple(diffed)# ---------------------------------------------------------------------------# Event handlers
app.py+71-23