Skip to content

Don't cache the templates that call one using the current word - #2848

Merged
BoboTiG merged 3 commits into
reader-dict:mainfrom
monosans:fix/cache-template-callers
Nov 22, 2026
Merged

BoboTiG merged 3 commits into
reader-dict:mainfrom
monosans:fix/cache-template-callers

Conversation

@monosans

Copy link
Copy Markdown
Contributor

Two builds of the same RU dump gave different synonyms for a dozen words. On "Малороссия", "Уркаина", "Незалежная" and the like, the list from {{шаблон:синонимы:Украина}} dropped a different word each time: whichever page that worker rendered first. So "Уркаина" listed itself, and "Малороссия" lost "Украина" and got "син.: ;" instead.

That template only contains {{синонимы|...}} calls. синонимы redirects to ссылки с пометой, which hides the current word (hide={{PAGENAME}}) and is uncacheable. Two gaps let the caller be cached anyway:

  • set_cache_exclusions() only looks at a template's own body (PAGENAME, #invoke of an excluded module) and at direct redirects, so a template that calls an uncacheable template stays cacheable
  • expand() checks wikitext.startswith("{{" + name), so a call written with the namespace ({{шаблон:...}}, {{Template:...}}) never matches an exclusion

The fix:

  • after the existing steps, follow the template calls and redirects backwards from every uncacheable template, at any depth, and mark the callers uncacheable. This replaces the redirect query, which was the one-level case of it
  • exclusions are a set of template names without the namespace, and expand() looks up the name of the called template in it

The catch is speed. In RU the uncacheable templates go from 4084 to 30901 out of 44976, because widely used ones like термин (and so хим., истор....) read PAGENAME, if only for a category sort key. On my machine with 12 workers, --render went from 11:00 to 17:01. In exchange the output no longer depends on the order the words are rendered in: of the 4.3M entries 79 change (14 distinct articles), all of them now correct.

Testing

  • Host: Windows 11, Docker Desktop 4.100.0, image python:3.13-slim with the requirements of this branch, RU dump 2026-11-01
  • Rendered "Уркаина", "Малороссия", "Незалежная" and "Украина" in one process, in both orders: before, all four got the list of the first word; now each one only lacks its own word
  • Full RU build: синонимы:Украина, синонимы and ссылки с пометой are uncacheable, no render errors, and the StarDict output differs from the previous build only on those 79 entries
  • ruff format --check, ruff check and mypy pass

@monosans
monosans force-pushed the fix/cache-template-callers branch from 93eb083 to c72c4c9 Compare November 22, 2026 14:46
@BoboTiG

BoboTiG commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Good catch!

About speed, there is not much we can do.

I thought about altering all templates & modules before checking for their cacheable state in order to remove all category handling (since we never use them and we remove them later in the process anyway).

But, looking at https://ru.wiktionary.org/w/index.php?title=%D0%A8%D0%B0%D0%B1%D0%BB%D0%BE%D0%BD:%D1%82%D0%B5%D1%80%D0%BC%D0%B8%D0%BD&action=edit it won't be an easy task, and I am afraid we might introduce errors.

I always wanted to get rid of categories at the root, maybe do you have a better idea?

Comment thread wikidict/context.py Resolved
A template that only calls an uncacheable template was cached, and so
was a call written with its namespace (`{{Template:foo}}`). The first
word rendered then decided the output for all the others: in RU the
synonyms of "Украина" lost the word of whichever page came first.

Follow the template calls and redirects at any depth, and compare the
template names without the namespace.
A module requiring an uncacheable module, or expanding an uncacheable
template, is now uncacheable as well, and so are the templates invoking it.
A direct `{{#invoke:...}}` of an uncacheable module is not cached either.

Since `#invoke` calls are part of the graph now, the temporary table of
excluded modules is no longer needed.
@monosans
monosans force-pushed the fix/cache-template-callers branch from c72c4c9 to a2d9ee2 Compare November 22, 2026 17:39
When looking for templates & modules using the current word, skip:

- the docs of a template: comments, `<noinclude>`, and what is outside
  `<onlyinclude>`, as they are not transcluded
- category links, as they are dropped from the output anyway, while their
  sort key is often `{{PAGENAME}}`
- `mw.title.getCurrentTitle().nsText` and `.namespace` in modules, as
  words are always in the main namespace

In RU, the uncacheable templates go from 36k to 12k out of 45k.
@BoboTiG
BoboTiG merged commit f0bf952 into reader-dict:main Nov 22, 2026
1 check passed
@monosans

monosans commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for merging! About the categories, I went with a lighter version of your idea in d109737: the templates themselves stay untouched, only the cacheable check ignores what never reaches the output. That means category links (even with {{PAGENAME}} or {{#if:...}} inside), comments, <noinclude> docs, and in modules the getCurrentTitle().nsText reads, since words are always in the main namespace. In RU that brought the uncacheable templates down from 36k to 12k out of 45k.

I did a full RU build of main after the merge: --render took 9:05 with 12 workers, so a bit faster than before the PR (11:00). Apart from the formula newlines below, the output is the same as my earlier build of this PR, where 31k templates were uncacheable, so skipping those parts didn't change anything visible.

While comparing, I noticed the last commit of #2847 (the trailing newline after a formula) arrived after the merge, so it's in #2849 now.

@monosans
monosans deleted the fix/cache-template-callers branch November 23, 2026 00:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants