Repository navigation
fix(document): memoize per-language USFM book-name lookup in localize_non_usfm_book_names - #407
Merged
linearcombination merged 1 commit intoNov 17, 2026
Conversation
PurpleGuitar
approved these changes
Nov 17, 2026
Cache per-language USFM book-name lookup in localize_non_usfm_book_names book_codes_for_lang_from_usfm_only(lang_code) re-reads and re-parses every USFM file for that language's Bible-text repo. It was being called once per TN/TNC/TQ book (O(books requested x usfm books)) even though its result depends only on lang_code, which is identical across all books in a request. Cache it per lang_code so it's computed once per language instead of once per book, with no change to the localized output.
linearcombination
force-pushed
the
memoize-book-name-lookup
branch
from
November 17, 2026 22:22
18505b2 to
14b0c94
Compare
linearcombination
merged commit Nov 17, 2026
e372f70
into
WycliffeAssociates:doc-dev.walink.org
24 of 25 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #363
Intent
Use case: a user selects only the TN (translation notes) resource, for all books in both OT and NT, for one language, requesting DOCX output.
Approved follow-up: implement the fix for the investigation's dominant finding. When generating a DOCX for this request shape, the code localizes each of the 66 requested TN books' name headers by calling document_generator.localize_non_usfm_book_names -> resource_lookup.book_codes_for_lang_from_usfm_only(lang_code) once per book. That function's result depends only on lang_code, which is identical across all 66 calls, yet it is recomputed from scratch every time: each call re-reads and re-parses every USFM file for that language's Bible-text repo (for pt-br, 66 files/6.0MB), an O(book-count x usfm-book-count) cost instead of the O(usfm-book-count) the work actually needs. Fix it so this per-language result is computed once per request instead of once per book (e.g. memoize the lookup keyed on lang_code, or hoist the call out of the per-book loop in localize_non_usfm_book_names), with no change to the localized output.
What Changed
backend/doc/domain/document_generator.py,localize_non_usfm_book_namesnow caches the result ofresource_lookup.book_codes_for_lang_from_usfm_only(lang_code)in a localusfm_names_by_langdict keyed bylang_code, computing it once per distinct language per request instead of once per book.tests/unit/test_document_generator.pygains coverage asserting the lookup is invoked at most once per language across multiple books, alongside existing localization-output assertions.Risk Assessment
✅ Low: The change is a small, request-scoped memoization of a pure per-language lookup (keyed correctly by lang_code, handles falsy-but-cacheable empty results, no cross-request global state), produces no behavioral change to localized output, exactly matches the authorized fix, and is backed by two new tests that assert real call-count bounding and correct localized output rather than source-text matching.
Testing
test
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
✅ **Review** - passed
✅ No issues found.
✅ **Test** - passed
✅ No issues found.
test✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.