WordPress.org

Make WordPress Core

Ticket #37997: trac-37997-improve-just-in-time-performance.patch

File trac-37997-improve-just-in-time-performance.patch, 4.3 KB (added by jrf, 3 years ago)

Proof of concept for caching of paths for just in time language loading

  • src/wp-includes/class-wp-locale-switcher.php

    From 39f91adcd53c761137ae0cfbdba2a29b4a947187 Mon Sep 17 00:00:00 2001
    Date: Sat, 12 Nov 2016 01:55:57 +0100
    Subject: [PATCH] Proof of concept for improving the performance of the
     `_load_textdomain_just_in_time()` function.
    
    ---
     src/wp-includes/class-wp-locale-switcher.php |  3 ++
     src/wp-includes/l10n.php                     | 67 +++++++++++++++++++++++-----
     2 files changed, 60 insertions(+), 10 deletions(-)
    
    diff --git a/src/wp-includes/class-wp-locale-switcher.php b/src/wp-includes/class-wp-locale-switcher.php
    index 3d288c8..4ccffab 100644
    a b class WP_Locale_Switcher { 
    228228         * @param string $locale The locale to change to.
    229229         */
    230230        private function change_locale( $locale ) {
     231                // Reset translation availability information.
     232                _get_path_to_translation( null, true );
     233
    231234                $this->load_translations( $locale );
    232235
    233236                $GLOBALS['wp_locale'] = new WP_Locale();
  • src/wp-includes/l10n.php

    diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php
    index 39346c4..cbd7985 100644
    a b function load_child_theme_textdomain( $domain, $path = false ) { 
    827827 * the translation file from `wp-content/languages`, removing the need
    828828 * to call load_plugin_texdomain() or load_theme_texdomain().
    829829 *
    830  * Holds a cached list of available .mo files to improve performance.
    831  *
    832830 * @since 4.6.0
    833831 * @access private
    834832 *
    function load_child_theme_textdomain( $domain, $path = false ) { 
    841839function _load_textdomain_just_in_time( $domain ) {
    842840        global $l10n_unloaded;
    843841
    844         $l10n_unloaded = (array) $l10n_unloaded;
    845 
    846         static $cached_mofiles = null;
     842        $l10n_unloaded    = (array) $l10n_unloaded;
     843        $translation_path = _get_path_to_translation( $domain );
    847844
    848845        // Short-circuit if domain is 'default' which is reserved for core.
    849         if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) ) {
     846        if ( 'default' === $domain || isset( $l10n_unloaded[ $domain ] ) || false === $translation_path ) {
    850847                return false;
    851848        }
    852849
     850        return load_textdomain( $domain, $translation_path );
     851}
     852
     853/**
     854 * Get the path to a translation file in the `wp-content/languages` directory.
     855 *
     856 * Caches the retrieved results internally.
     857 *
     858 * @since 4.7.0
     859 * @access private
     860 *
     861 * @see _load_textdomain_just_in_time()
     862 *
     863 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     864 * @param bool   $reset  Whether to reset the internal cache. Used by the switch to locale functionality.
     865 * @return string|false The path to the translation file or false if no translation file was found.
     866 */
     867function _get_path_to_translation( $domain, $reset = false ) {
     868        static $available_translations = array();
     869
     870        if ( true === $reset ) {
     871                $available_translations = array();
     872        }
     873
     874        if ( ! isset( $available_translations[ $domain ] ) ) {
     875                $available_translations[ $domain ] = _get_path_to_glotpress_translation( $domain );
     876        }
     877
     878        return $available_translations[ $domain ];
     879}
     880
     881/**
     882 * Get the path to a translation file in the languages directory for loading
     883 * a textdomain just in time.
     884 *
     885 * Holds a cached list of available .mo files to improve performance.
     886 *
     887 * @since 4.7.0
     888 * @access private
     889 *
     890 * @see _get_path_to_translation()
     891 *
     892 * @param string $domain Text domain. Unique identifier for retrieving translated strings.
     893 * @return string|false The path to the translation file or false if no translation file was found.
     894 */
     895function _get_path_to_glotpress_translation( $domain ) {
     896        static $cached_mofiles = null;
     897
    853898        if ( null === $cached_mofiles ) {
    854899                $cached_mofiles = array();
    855900
    function _load_textdomain_just_in_time( $domain ) { 
    868913        $locale = is_admin() ? get_user_locale() : get_locale();
    869914        $mofile = "{$domain}-{$locale}.mo";
    870915
    871         if ( in_array( WP_LANG_DIR . '/plugins/' . $mofile, $cached_mofiles ) ) {
    872                 return load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile );
     916        $path = WP_LANG_DIR . '/plugins/' . $mofile;
     917        if ( in_array( $path, $cached_mofiles ) ) {
     918                return $path;
    873919        }
    874920
    875         if ( in_array( WP_LANG_DIR . '/themes/' . $mofile, $cached_mofiles ) ) {
    876                 return load_textdomain( $domain, WP_LANG_DIR . '/themes/' . $mofile );
     921        $path = WP_LANG_DIR . '/themes/' . $mofile;
     922        if ( in_array( $path, $cached_mofiles ) ) {
     923                return $path;
    877924        }
    878925
    879926        return false;