Page MenuHomePhabricator

Evaluate reducing db queries from Skin::getCategories
Open, Needs TriagePublic

Description

Background

In wikitext, you can place a page into one or more categories, e.g. [[Category:Fruits]]. When parsing an edit (Parser), these are extracted and stored along with the rest of the article content in ParserCache/ParserOutput where during a pageview, it can be retrieved at no overhead (i.e. already included the ParserOutput object) via ParserOutput::getCategoryNames.

The skin retrieves these and renders them at the bottom of the page, below the article content:

Screenshot catlinks.png (1,786×678 px, 168 KB) Screenshot catlinks-hidden.png (2,274×722 px, 236 KB)

Problem / Opportunity

This depends on two bits of information that are not cached, and are instead retrieved from the database on every pageview:

  • Page existence metadata for the category pages (i.e. red or blue).
  • Page property "hiddencat" for the category pages (i.e. normal or hidden cat).

I'm guessing that this was implemented this way, rather than behind the ParserCache, because otherwise when a category page is created or deleted, the information would be stale for upto 30 days in the ParserCache. That could be fixed by setting up LinksUpdate propagation (as we do for pagelinksand templatelinks), by either recording them as pagelinks, or by treating outgoing categorylinks the same (i.e. only recurse on creation or deletion to turn blue links red and vice versa).

Except that would not suffice, because the hiddencat status can technically change on every edit, so you'd either have to introduce a new way to detect changes from and to that state with dedicated change propagation, or, propagate on every category page edit which would be prohibitively expensive and profoundly wasteful, and so we don't it that way.

Instead, we fetch this live from the database on each pageview.

For logged-out users, pageviews are regularly CDN-cached for 14 days and we do nothing to invalidate that on category creations/edits/deletions. So the reality is that we do cache this information, and there's seemingly been no complaint or effort to change that in twenty years.

For logged-in users, this information is magically always up-to-date. This most visible when it comes to seeing the effect of your own actions. We have affordances throughout MediaWiki (e.g. ChronologyProtector, and UseDC=master cookies) to make sure users see the effect for their own actions, even if more broadly, for performance reasons, we don't want to instantly purge everything everywhere.

As part of T433637, we're trying to leverage the CDN cache for pageviews with ESI. If that succeeds, then this will implicitly become cached and stale the same way for logged-in users as well,

If that is okay with ESI, it should be oaky without ESI as well, so let's evaluate what we can to to avoid these DB queries on pageviews.

Status quo

@Krinkle wrote in task description:

[…]

  1. [0.002s] MediaWiki\Output\OutputPage::addCategoryLinksToLBAndGetResult SELECT … FROM page LEFT JOIN page_props ON (pp_propname = 'hiddencat' AND (pp_page = page_id)) WHERE ((page_namespace = 14 AND page_title IN (…,'Featured_articles',…,'1877_births','1951_deaths',…,'British_historians',…) ))
    • Who: OutputPage, on behalf of Skin.
    • Why: Show category links at the bottom of the page. Interestingly, there is no categorylinks table query, because that data is obtained from the ParserOutput from the ParserCache. Doing so saves a DB query but also ensure integrity and internal consistency with the rendered wikitext revision (i.e. you wouldn't want a category associated with a template like "citation needed", when it is no longer in the content or vice versa). This query is to help separate the "hidden" categories from the regular ones. This can change at any time through edits to category pages and rather than cache this in ParserCache and require refresh links to propagate on the entire site after (unprotected) Category edits, we instead query this at the last-minute.

It happens in three phases today.

  1. Parsing: Parser.php calls ParserOutput::addCategory() and the name is later stored via ParserCache.
  2. OutputPage (early):
    • retrieve ParserOutput from ParserCache
    • call OutputPage::addParserOutputMetadata > OutputPage::addCategoryLinks
    • This uses OutputPage::addCategoryLinksToLBAndGetResult to fetche page existence metadata (fed to LinkCache), and hiddencat page prop. This bypasses the PageProps service/cache, but it does combine the page and pageprop queries, so makes 1 query today, not 2.
    • It pre-generates an array with small chunks of HTML using LinkRenderer::makeLink <a href=.. title=.. class=..>..</a>, which makes use of the LinkCache page existence metadata.
    • It groups the HTML chunks by "normal" and "hidden".
  3. Skin (late): The skin eventually calls Skin::getCategories to get the HTML blob for the bottom of the page
    • This calls Skin::getCategoryLinks > OutputPage::getCategoryLinks to get the pre-generated and pre-grouped HTML chunks.
    • It wraps it in an unordered list. This currently does a second call to OutputPage::getCategoryLinks to know whether the list is empty. This is an ugly workaround the fact that its own business logic in Skin::getCategoryLinks failing to account for this or conveying it, even tough it clearly needs it.

Event Timeline

One thing we could do is include categories in Skin::preloadExistence, and ride along its LinkCache/LinkBatch lookup. I've checked in an Excimer UI profile that this runs after OutputPage::addParserOutputMetadata and before Skin::getCategories, which is exactly what we need. This would remain "live", and does not add caching or otherwise compromise anything we get today.

This would still leave the same query, but reduce it to the page_props table for "hiddencat", so not yet a net-reduction in DB roundtrips.

OutputPage::addCategoryLinks also calls LanguageConverter::findVariantLink on Language Converter wikis, to resolve category name in another variant. IMO this should be done during parsing, as the categorylink depends on the result, and would be confusing if it resolves to a different category name only at display time. However, the parsing part would need some changes before it can be removed from OutputPage (ref. T403122#12315605).