Edit request 6 April 2025 - Performance improvement

edit

Hello, you may be interested by the code used by this template on frwiki: [1].

When the template is used without "lang" parameter, I figured out a way to use "code + nowiki" instead of "syntaxhighlight". The result is visually identical, but we avoid the expensive syntaxhighlight call.

(Refs a previous, failed attempt: Template talk:Code/Archive 1#Template-protected edit request on 4 March 2024.)

Od1n (talk) 18:03, 6 April 2025 (UTC)Reply

I have pushed (pushed back, actually) the suggested change to the sandbox. Note that, as it happens, there is a difference in behavior with HTML entities. See third table in the previous discussion. Od1n (talk) 01:19, 7 April 2025 (UTC)Reply
I note that this edit removes the |id= parameter on the grounds that it's unused. That may be the case; but it was added on the grounds of harmonisation. I think that it should not be removed. --Redrose64 🌹 (talk) 16:56, 7 April 2025 (UTC)Reply
Some relevant examples from the previous discussion have been copied to the testcases page. I see differences in rendering between the live template and the sandbox in the second "Markup behaviour" table. Are those differences intended? – Jonesey95 (talk) 18:37, 8 April 2025 (UTC)Reply
  • No opinion regarding the removal of the |id= parameter. I just synchronized with the production template, where it has been removed. I lean toward keeping it removed, but that’s a secondary consideration.
  • I noticed the discrepancy with HTML entities only after posting the suggested change. On one hand, the change would make the template significantly faster (eliminating the expensive <syntaxhighlight> call on all simple uses), but on the other hand, it introduces that discrepancy with HTML entities. Thoughts?
Od1n (talk) 20:58, 10 April 2025 (UTC)Reply
What's the use case for this? Are there actually a significant number of pages running into the expensive parser function limit because of calls to this template with lang=text? Otherwise WP:PERF applies and it's not worth the extra complexity. * Pppery * it has begun... 20:17, 22 April 2025 (UTC)Reply
Are there any pages that have lang=text? I for one have never seen any in the wild. 𝕁𝕄𝔽 (talk) 20:40, 22 April 2025 (UTC)Reply
"Text" is the default, so any pages that don't specify a language would trigger this, which is probably fairly common. But even given that there are only 6 pages using this template with too many expensive parser function calls so I still don't think it's worth it. * Pppery * it has begun... 23:26, 24 April 2025 (UTC)Reply
  Not done for now: please establish a consensus for this alteration before using the {{Edit template-protected}} template. jlwoodwa (talk) 01:35, 27 April 2025 (UTC)Reply
A great solution for this issue would be upstream, in the extension code: when no syntax highlighting is applied, there is no reason for the tag to be expensive.
When using lang="text", we should ensure that the tag execution is fast, and does not increment the expensive function count. For instance, I thought of an early return in SyntaxHighlight.php:
//if ( $lexer === null ) {
if ( $lexer === null || $lexer === 'text' ) {
	// When syntax highlighting is disabled..
	$status->value = self::plainCodeWrap( $code, $isInline );
	return $status;
}
Od1n (talk) 20:36, 19 May 2025 (UTC)Reply
One caveat with the above change: it would break the "line numbers" feature on plain text, i.e. <syntaxhighlight lang="text" line>. So my proposed change requires further refinement.
By the way, I noticed a related bug that would impact this change too: when using <syntaxhighlight line> (erroneously omitting the lang attribute), the lines placeholder appears but does not contain line numbers.
Od1n (talk) 20:52, 19 May 2025 (UTC)Reply