#15752Faster "substituteConstants"
Description
“substituteConstants” for typoscript is using loop and str_replace, which has a poor performance. O(M x N).
Neither longer typoscript setup or one more constants(even not used) will increase processing time a lot.
By replacing it into preg_replace_callback, it can finish the job with only 20% of time.
class.t3lib_TStemplate.php
—— 1.27.2.1 on CVS ——
function substituteConstants($all) {
if ($this->tt_track) $GLOBALS[‘TT’]->setTSlogMessage(‘Constants to substitute: ‘.count($this->flatSetup));
reset($this->flatSetup);
while (list($const,$val)=each($this->flatSetup)) {
if (!is_array($val)) {
$all = str_replace(‘{$’.$const.’}’,$val,$all);
}
}
return $all;
}
—— modified ——
function substituteConstantsCallBack($matches) {
return isset ($this->flatSetup[$matches1]) ? $this->flatSetup[$matches1] : $matches0;
}
function substituteConstants($all) {
if ($this->tt_track)
$GLOBALS[‘TT’]->setTSlogMessage(‘Constants to substitute: ‘.count($this->flatSetup));
return preg_replace_callback(‘/\{\$(.+?)\}/’, array ($this, ‘substituteConstantsCallBack’), $all);
}
(issue imported from #M2743)
Reviews 0
No change resolves or names this issue in its commit message, and no comment links one.