ZF-2999: performance optimization

Description

1.


isset(self::$_localeData[$locale])
// instead of
array_key_exists($locale, self::$_localeData)

2.


if ( isset(self::$_localeData[$locale])  && ($locale == 'root' || self::$_localeData[$locale] === false) ) {
    // ....
}
// instead of
if (($locale == "auto") or ($locale == "root") or 
    ($locale == "environment") or ($locale == "browser")) {
    // ...
}

// -> OK this isn't for performance but for flexibly
  1. cache return value of getBrowser and if the environment variable must changable add an argument e.g. $reparse=false

Comments

Could you please evaluate and categorize as necessary.

  1. has been integrated with r9534 within the I18N core, not only Zend_Locale.

  2. brakes existing functionality as it disallows locales like "en_DE" and therefor brakes the downgrading

  3. is not necessary as the locale itself is cached, so the browser response has not to be cached alone.

Hi Thomas

Regarding my last statement: it is very slow to parse the result of "$locale->getBrowser()" on every method call, because the clients browser will not change in one connection, but the environment value of "HTTP_ACCEPT_LANGUAGE" can change in one connection during script execution. To get the array of user accepted locales is not the same as to create a browser locale "new Zend_Locale('browser')". Furthermore the method getBrowser will be called by getDefault() and the method getDefault() will be called by setLocale()


public function getBrowser($reparse=false)
{
    if ($reparse === false && $this->_getBrowserCache !== null) {
        return $this->_getBrowserCache;
    }
    
    // ... parse
    
    return $this->_getBrowserCache;
}

Locale is locale, independently from the creation. The locale can change within the same session.

Also if you need the browsers locale you would not parse it again and again. You would normally store it within your session to reuse it.

Only if you accept that the locale can change within the same session you would parse the locale once more. And when you use setLocale the browser will only then be parsed when you give one of the autolocales or define that you want to retrieve the autolocale.

In my opinion there is still no benefit of having another option added.

Hi Thomas

this is a little test script which displays all calls of the getBrowser-method and its output:

Zend_Locale:


public function getBrowser()
{
    echo " - getBrowser called\n";
    // ...
}

testscript:


echo "new Zend_Locale():\n";
$locale = new Zend_Locale();

echo "new Zend_Locale():\n";
$locale = new Zend_Locale();

echo "new Zend_Locale(Zend_Locale::BROWSER);\n";
$locale = new Zend_Locale(Zend_Locale::BROWSER);

echo "\$locale->getDefault();\n";
$locale->getDefault();

echo "\$locale->getDefault(Zend_Locale::BROWSER);\n";
$locale->getDefault(Zend_Locale::BROWSER);

echo "\$locale->setLocale(Zend_Locale::BROWSER);\n";
$locale->setLocale(Zend_Locale::BROWSER);

output:


new Zend_Locale():
 - getBrowser called
 - getBrowser called
 - getBrowser called
 - getBrowser called
new Zend_Locale():
 - getBrowser called
new Zend_Locale(Zend_Locale::BROWSER);
 - getBrowser called
$locale->getDefault();
 - getBrowser called
$locale->getDefault(Zend_Locale::BROWSER);
 - getBrowser called
$locale->setLocale(Zend_Locale::BROWSER);
 - getBrowser called

Updating for the 1.6.0 release.