Issue Bug Closed

#20745replace while(list()...) by foreach in the TYPO3 core part 1

Rupert Germann 07/13/09, 18:10 updated 04/18/12, 21:59

Description

Problem:
foreach() has proven1 that it is way faster than while(list()=each()) and in current PHP versions foreach even needs less memory.

Solution:
replace while(list()=each()) with the appropriate foreach() statements in frontend classes
(part 2 will take care of the rest)

[1]
http://lists.typo3.org/pipermail/typo3-dev/2009-June/035735.html

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

that 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 where 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%

(issue imported from #M11512)

Reviews 0

No change resolves or names this issue in its commit message, and no comment links one.