Page MenuHomePhabricator

GenderCache could avoid one SQL request sometimes
Open, Needs TriagePublic

Description

Steps to replicate the issue:

  • Install MediaWiki master (e.g. current master 1.39.0-alpha) with basic configuration (no extension, one skin),
  • Activate $wgDebugToolbar = true; and $wgDebugDumpSql = true;
  • Open the Main page and check the SQL requests:

1/ First as unlogged user:

There is a SQL request

SELECT user_name,up_value FROM user LEFT JOIN user_properties ON ((user_id = up_user) AND up_property = 'gender') WHERE user_name = '127.0.0.1'

⇒ This request could be avoided since the gender of an anonymous user is always the default value (and if I’m not mistaken, it is also the case for temporary users).

2/ Second as logged-in user:

There is a SQL request

SELECT user_name,up_value FROM user LEFT JOIN user_properties ON ((user_id = up_user) AND up_property = 'gender') WHERE user_name = 'MyUsername'

But this request comes after a previous one more general

SELECT up_property,up_value FROM user_properties WHERE up_user = 1

⇒ The result is already known and accessible through the UserOptionsLookup service, and in this case the SQL request could be avoided.


What should have happened instead?:

In the case GenderCache only searches the gender of the current user (which always occur to create a link to their userpage, in the skin subsystem), this SQL request could be avoided.

I agree the marginal time is useless if GenderCache does a request for multiple users, but there are a lot of pages where there is no links to userpages (of other users), so I guess the case is quite common.

I have no idea how much performance gain we can gain, but I think it is always beneficial to reduce the number of SQL requests for already-requested and easy-to-obtain informations.

Event Timeline

There is another task quite near of this one, but centered on LinkCache T297914.

In my observations, I assumed the properties of the current user are always loaded, but I’m not completely sure of this in all types of requests.

  • An option is to prove this by checking all code paths.
  • Another option would be to opportunistically ask UserOptionManager::getOption( $wgUser, 'gender' ) in the sense that, if it has the information it’s great, else it doesn’t matter and the information will be retrieved by GenderCache possibly with other requested users. I propose this opportunistic solution because I think (but I might be mistaken) that it should be prefered the SQL request in GenderCache instead of the SQL request of UserOptionManager (at this point we don’t need all user options, only the gender). The downside is that UserOptionManager currently does not implement such opportunistic behaviour.
Seb35 changed the subtype of this task from "Bug Report" to "Task".Aug 21 2022, 7:42 PM

I submit my current proposition as a WIP because it might be sub-optimal if the options of the current user are not loaded (and possibly detrimental to the performance in this specific case).

Change 824888 had a related patch set uploaded (by Seb35; author: Seb35):

[mediawiki/core@master] Load the gender of current user without dedicated SQL request

https://gerrit.wikimedia.org/r/824888

See also T267054 / https://gerrit.wikimedia.org/r/c/mediawiki/core/+/689533 where the check for ips was removed and every user results in database request