[wp-trac] [WordPress Trac] #58317: Refactor determine_locale() for performance
WordPress Trac
noreply at wordpress.org
Mon May 15 12:44:22 UTC 2023
#58317: Refactor determine_locale() for performance
-------------------------+-----------------------------
Reporter: Cybr | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version: trunk
Severity: minor | Keywords: has-patch
Focuses: performance |
-------------------------+-----------------------------
`determine_locale()` breaks various performance tropes. Albeit an obscure
function, I hope this refactor will be the first of many.
The first defense clause:
{{{#!php
<?PHP
if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) {
}}}
Could become:
{{{#!php
<?PHP
if ( $determined_locale && is_string( $determined_locale ) ) {
}}}
Here, I removed a function call and a NOT operation, resulting in exactly
the same code.
Determining the user locale:
{{{#!php
<?php
$determined_locale = get_locale();
if ( is_admin() ) {
$determined_locale = get_user_locale();
}
if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] &&
wp_is_json_request() ) {
$determined_locale = get_user_locale();
}
}}}
Could become:
{{{#!php
<?php
if (
is_admin()
|| ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] &&
wp_is_json_request() )
) {
$determined_locale = get_user_locale();
} else {
$determined_locale = get_locale();
}
}}}
Here I exchange a jump for an operator (first and second if-statements are
combined and exchanged with a logic operator). I also removed writing to
the variable twice in admin and insecure JSON requests.
Writing WP lang:
{{{#!php
<?php
$wp_lang = '';
if ( ! empty( $_GET['wp_lang'] ) ) {
$wp_lang = sanitize_text_field( $_GET['wp_lang'] );
} elseif ( ! empty( $_COOKIE['wp_lang'] ) ) {
$wp_lang = sanitize_text_field( $_COOKIE['wp_lang'] );
}
if ( ! empty( $wp_lang ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-
login.php' === $GLOBALS['pagenow'] ) {
$determined_locale = $wp_lang;
}
}}}
Could become:
{{{#!php
<?php
if ( isset( $GLOBALS['pagenow'] ) && 'wp-login.php' ===
$GLOBALS['pagenow'] ) {
if ( ! empty( $_GET['wp_lang'] ) ) {
$determined_locale = sanitize_text_field( $_GET['wp_lang']
) ?: $determined_locale;
} elseif ( ! empty( $_COOKIE['wp_lang'] ) ) {
$determined_locale = sanitize_text_field(
$_COOKIE['wp_lang'] ) ?: $determined_locale;
}
}
}}}
Here, instead of writing to `$wp_lang` twice, and then determining whether
we actually should use that value if the second attempt didn't happen, we
first determine the latter. Using the amazing power of the short ternary,
we spare a jump if the value amounts to nothing.
Still, the last two refactorizations could be combined, but I could not
think of a way to maintain readability while keeping in mind "DRY"
standards.
The refactorization makes the function about [https://3v4l.org/9PsqQ 1.5x
faster on the front-end], and about [https://3v4l.org/NNkoU 2.2x faster in
admin] — without changing any functionality.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/58317>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list