[TYPO3-core] RFC #0011512: replace while(list()...) by foreach in the TYPO3 core part 1

Ernesto Baschny [cron IT] ernst at cron-it.de
Mon Jul 27 15:36:49 CEST 2009


Hi Rupi,

I haven't really had time to look at all the changes and probably won't
have any time soon.

So just a note of what I would take special attention to: if there are
while-loops which modify the elements inside the loop. If this is the
case, it might break things when simply changing to a foreach with a
reference value-var.

Have you spotted any situation like that in your patching orgy?

Cheers,
Ernesto

Rupert Germann schrieb:
> REMINDER
> 
> 
> 
> Rupert Germann schrieb:
>> Hi,
>>
>> this is a SVN patch request.
>>
>> Type: performance improvement
>>
>> Bugtracker references:
>> http://bugs.typo3.org/view.php?id=11512
>>
>> Branches: Trunk
>>
>> Problem:
>> foreach() has proven[1] that it is way faster than
>> while(list()=each()) and
>> in current PHP versions foreach() even needs less memory than while().
>>
>> Solution:
>> replace while(list()=each()) with the appropriate foreach() statements in
>> frontend classes
>> (part 2 will take care of the rest)
>>
>>
>> Info:
>> the patch affects the following files:
>> t3lib/class.t3lib_page.php
>> t3lib/class.t3lib_tsparser.php
>> typo3/sysext/cms/tslib/class.tslib_menu.php
>> typo3/sysext/cms/tslib/class.tslib_pagegen.php
>> typo3/sysext/cms/tslib/class.tslib_content.php
>>
>>
>> thats what I changed:
>>
>> variant 1
>> - reset($array);
>> - while(list($k)=each($array)) {
>> + foreach ($array as $k => $v) {
>>
>> this leaves $v unused but in this way it is faster than
>> while(list()...) or
>> calling array_keys() before
>>
>> variant 2
>> - reset($loadDB->tableArray);
>> - while(list($table,)=each($loadDB->tableArray)) {
>> + foreach ($loadDB->tableArray as $table => $v) {
>>
>> same as variant 1
>>
>>
>> variant 3
>> - reset($Hobjs);
>> - while(list(,$index)=each($Hobjs)) {
>> + foreach ($Hobjs as $index) {
>>
>>
>> variant 4
>> - reset($inArray);
>> - while(list($fN,$fV)=each($inArray)) {
>> + foreach ($inArray as $fN => $v) {
>>
>>
>> in some cases I changed lines where very deep arrays were accessed during
>> loops to use a local copy of this array instead.
>>
>>
>> performance gain of this patch depends very much on your sites settings -
>> will say: if you have a big menu the changes in class.tslib_menu will
>> have
>> much more impact than with a small one.
>>
>> I tested it with a (uncached) page which displays 4 tt_news plugins
>> calling
>> cobj->getmarkerArrayCached() hundreds of times and I got a performance
>> gain
>> of 7-10% The rendering speed of cached pages is not influenced by this
>> patch.
>>
>> greets
>> rupert
>>
>> "There shall be no new TYPO3 version delivering content slower than its
>> predecessor!"
>>
>>
>> [1]
>> http://lists.netfielders.de/pipermail/typo3-dev/2009-June/035735.html [^]
>>


More information about the TYPO3-team-core mailing list