From b6ee8f09ad2c0bc9e83394e2427de861eb1e2a72 Mon Sep 17 00:00:00 2001 From: Lukasz Kasprzak Date: Mon, 24 Aug 2026 16:35:01 +0200 Subject: perf(readings): stack calendar layers and load the book table once per Days call mobile.Days (dlectio's 7-day calendar view) called readings.Load once per date. Load's offlineLoad re-ran caldata.Stack (parses the embedded calendar INI, plus any user calendar layer) and bible.LoadBookTable (parses the embedded books.ini, plus any user override) on every call, even though neither depends on the date -- only on cfg.Selection().Form and cfg.Use, which mobile.Days holds fixed across its whole loop. Verified before changing anything: isolated benchmarks put Stack+ LoadBookTable at ~1.1ms/call (OF) and ~1.6ms/call (EF) -- real INI parsing and, for a user override, file I/O -- and for the OF form that redundant work was ~67% of a 7-day view's total time. Load gains a Prepared value (the layer stack + book table) and a Prepare/LoadWith pair: Prepare builds a Prepared once, LoadWith reuses it across dates. Load itself is unchanged, still calling Prepare on every invocation -- every existing caller keeps its current behaviour untouched. mobile.Days now Prepares once and loops LoadWith; mobile.Day picks up the same fix for its own two same-cfg Load calls. Benchmarked (interleaved before/after pairs, to control for machine thermal drift): the OF 7-day view (BenchmarkDaysWeek) drops from ~11.5ms to ~6.4ms/op (allocs 53359 -> 16347, -69%), an EF 7-day view (BenchmarkDays7EF) from ~74ms to ~66-73ms/op (allocs 299204 -> 253815, -15%; the EF form's per-day calendar computation dominates its total cost far more than OF's does, so the fix's share of the win is smaller there), and a single Day call (BenchmarkDay1) from ~4.3ms to ~2.9ms/op (allocs 15357 -> 9192, -40%). Output identity verified separately (not part of this diff): a 288-case sweep of mobile.Day/mobile.Days across both forms, both UI languages, all four corpora, a leap day, the Sacred Triduum, a Requiem day, and windows spanning Christmas/Pentecost/Assumption/All Souls produced byte-identical (SHA-256-equal) JSON before and after this change. New TestLoadWithAgreesWithLoad asserts LoadWith(Prepare(cfg), cfg, opts) equals Load(cfg, opts) for every case, so the fast path cannot silently drift from the slow one. go test ./... and make ci (both build tags, oracle/differential suite included) are green. --- mobile/mobile.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'mobile/mobile.go') diff --git a/mobile/mobile.go b/mobile/mobile.go index 7e4c109..bc380d2 100644 --- a/mobile/mobile.go +++ b/mobile/mobile.go @@ -78,7 +78,12 @@ func Day(date, form, version, lang string) string { // Day identity (name/season/colour) in the interface language. cfgID := config.Config{UILanguage: lang, Lectionary: lect, All: true} - if _, info, err := readings.Load(cfgID, readings.Options{Date: date, All: true}); err == nil { + // cfgID and cfgR below differ only in UILanguage/ReadingVersion, never in + // Lectionary or Use -- the two inputs Prepare's cost depends on -- so one + // Prepare (keyed off cfgID; either config would do) serves both Load + // calls. See readings.Prepared's doc comment. + p := readings.Prepare(cfgID) + if _, info, err := readings.LoadWith(p, cfgID, readings.Options{Date: date, All: true}); err == nil { out.Name = info.Name out.Season = info.Season out.Colour = info.Colour @@ -92,7 +97,7 @@ func Day(date, form, version, lang string) string { // language localizes the structural labels (Heading) and citation dialect; // the corpus (version) alone decides the scripture text. cfgR := config.Config{UILanguage: lang, Lectionary: lect, ReadingVersion: version, All: true} - secs, _, err := readings.Load(cfgR, readings.Options{Date: date, All: true}) + secs, _, err := readings.LoadWith(p, cfgR, readings.Options{Date: date, All: true}) if err != nil { if out.Error == "" { out.Error = err.Error() @@ -166,12 +171,17 @@ func Days(start string, count int, form, lang string) string { lect := lectByForm(form) ui := i18n.Get(lang) cfg := config.Config{UILanguage: lang, Lectionary: lect, All: true} + // cfg is identical for every date in the loop below, so the layer stack + // and book table it implies are too -- Prepare once here rather than + // letting each readings.Load call re-stack/re-parse them. See + // readings.Prepared's doc comment. + p := readings.Prepare(cfg) out := make([]daySummary, 0, count) for i := 0; i < count; i++ { date := d.AddDate(0, 0, i).Format("2006-01-02") row := daySummary{Date: date, Parts: []summaryPart{}} - secs, info, err := readings.Load(cfg, readings.Options{Date: date, All: true}) + secs, info, err := readings.LoadWith(p, cfg, readings.Options{Date: date, All: true}) if err != nil { row.Error = err.Error() } else { -- cgit v1.3