Besides of the slow performance of the direct getChunk() method, do the other way arounds have some kind of policies attached or inherited?Yes, the sourceCache contains the policies for the element, so once loaded, from database, previous call to it in same request, or from a resource cache file (which carries it's sourceCache with it), it is simply instantiated with the policy data in place.
After 2.1, there is a parser method for working with single instances of any Element which directly uses an internal cache which is loaded by cached Resources without any db hits, and reused by the parser anytime the same element is called within the same request:
<?php $chunk = $modx->getParser()->getElement('modChunk', 'testSpeed'); for ($i = 0; $i < 1000; $i++) { $chunk->process(); }
Note that getChunk also uses the sourceCache, but does not avoid object instantiation, and thus is not good to use iteratively like this.
Future work on MODX will be focused on reducing the overhead of class loading, object instantiation, unnecessary parsing passes, and total code so that these bottlenecks can be more easily avoided, and using them is as efficient as including a simple file and parsing it.
[2012-03-01 07:15:31] (INFO @ /index.php) Processed 1000 chunks using getObject = 5.1633 s [2012-03-01 07:15:34] (INFO @ /index.php) Processed 1000 chunks using modParser::getElement = 3.6700 s [2012-03-01 07:15:38] (INFO @ /index.php) Processed 1000 chunks using getChunk = 3.7573 s [2012-03-01 07:15:39] (INFO @ /index.php) Processed 1000 iterations on included content using modParser::processElementTags = 0.8264 s
$ph['display_price'] = '£30,000'; $ph['display_subtype'] = 'semi-detached house for sale'; $ph['display_address'] = 'A House Somewhere'; $ph['status'] = ''; $ph['summary'] = 'This is a nice house'; $y = 1000; $modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML'); $modx->setLogLevel(xPDO::LOG_LEVEL_INFO); $tstart = $modx->getMicroTime(); for ($i = 0; $i < $y; $i++) { $ph['status'] = $i; $modx->setPlaceholders($ph); $chunk = $modx->getObject('modChunk', array('name' => 'testSpeed')); $chunk->setCacheable(false); $chunk->process(); } $tend = $modx->getMicroTime(); $modx->log(modX::LOG_LEVEL_INFO, sprintf("Processed {$y} chunks using getObject = %2.4f s\n", $tend - $tstart)); $tstart = $modx->getMicroTime(); for ($i = 0; $i < $y; $i++) { $ph['status'] = $i; $modx->setPlaceholders($ph); $chunk = $modx->getParser()->getElement('modChunk', 'testSpeed'); $chunk->setCacheable(false); $chunk->process(); } $tend = $modx->getMicroTime(); $modx->log(modX::LOG_LEVEL_INFO, sprintf("Processed {$y} chunks using modParser::getElement = %2.4f s\n", $tend - $tstart)); $tstart = $modx->getMicroTime(); for ($i = 0; $i < $y; $i++) { $ph['status'] = $i; $modx->setPlaceholders($ph); $modx->getChunk('testSpeed', $ph); } $tend = $modx->getMicroTime(); $modx->log(modX::LOG_LEVEL_INFO, sprintf("Processed {$y} chunks using getChunk = %2.4f s\n", $tend - $tstart)); $tstart = $modx->getMicroTime(); for ($i = 0; $i < $y; $i++) { $ph['status'] = $i; $modx->setPlaceholders($ph); $content = file_get_contents( 'propertieslistitem.chunk.tpl'); $modx->getParser()->processElementTags('', $content); //echo $content; } $tend = $modx->getMicroTime(); $modx->log(modX::LOG_LEVEL_INFO, sprintf("Processed {$y} iterations on included content using modParser::processElementTags = %2.4f s\n", $tend - $tstart)); exit();
I thought I'd try to apply the methods provided by Opengeek to my particular situation. I found that in all cases I had to move the method retrieving or parsing the chunk inside the loop to prevent it caching and therefore always returning the first instance of itself. Even with $chunk->setCachable(false) the chunk still appears to be cached).Unless you change the placeholders between runs, any Element with the same tag signature will always come from the cache. You are not understanding some basics of the parsing steps.
$chunk = $modx->getObject('modChunk', array('name' => 'testSpeed')); $chunk->setCacheable(false); for ($i = 0; $i < $y; $i++) { $ph['status'] = $i; $chunk->setProperties($ph); $chunk->process(); }
I think you're missing the properties parameter of the process() method:
Is it that I can't and shouldn't be trying to use the methods you demonstrated in the way I am trying to is it just that I'm doing it wrong?
$chunk = $modx->getObject('modChunk', array('name' => 'testSpeed')); $chunk->setCacheable(false); for ($i = 0; $i < $y; $i++) { $chunk->process(array('i' => $i)); }
[2012-03-01 11:31:15] (INFO) Processed 1000 chunks using getObject — 0.3535 s [2012-03-01 11:31:16] (INFO) Processed 1000 chunks using modParser::getElement — 0.2690 s [2012-03-01 11:31:18] (INFO) Processed 1000 chunks using getChunk — 2.8828 s [2012-03-01 11:31:19] (INFO) Processed 1000 iterations on included content using modParser::processElementTags — 0.3932 s
[[+status]]
$chunk = $modx->getObject('modChunk', array('name' => 'testSpeed')); $chunk->setCacheable(false); for ($i = 0; $i < $y; $i++) { echo "Pass - $i<b> "; echo ' </b>Printing chunk output ... <b>' .$chunk->process(array('status'=>$i)); echo ' </b>Printing chunk content ... <b>' . $chunk->getContent(); echo ' </b>Printing properties ... <b>'; print_r($chunk->_properties); echo ' </b>Printing tag ... <b>' . $chunk->_tag; echo '</b> '; }
Pass - 0 Printing chunk output ... 0 Printing chunk content ... [[+status]] Printing properties ... Array ( [status] => 0 ) Printing tag ... [[$testSpeed?status=`0`]] Pass - 1 Printing chunk output ... 0 Printing chunk content ... [[+status]] Printing properties ... Array ( [status] => 1 ) Printing tag ... [[$testSpeed?status=`0`]] Pass - 2 Printing chunk output ... 0 Printing chunk content ... [[+status]] Printing properties ... Array ( [status] => 2 ) Printing tag ... [[$testSpeed?status=`0`]] Pass - 3 Printing chunk output ... 0 Printing chunk content ... [[+status]] Printing properties ... Array ( [status] => 3 ) Printing tag ... [[$testSpeed?status=`0`]] Pass - 4 Printing chunk output ... 0 Printing chunk content ... [[+status]] Printing properties ... Array ( [status] => 4 ) Printing tag ... [[$testSpeed?status=`0`]] Pass - 5 Printing chunk output ... 0 Printing chunk content ... [[+status]] Printing properties ... Array ( [status] => 5 ) Printing tag ... [[$testSpeed?status=`0`]] Pass - 6 Printing chunk output ... 0 Printing chunk content ... [[+status]] Printing properties ... Array ( [status] => 6 ) Printing tag ... [[$testSpeed?status=`0`]] Pass - 7 Printing chunk output ... 0 Printing chunk content ... [[+status]] Printing properties ... Array ( [status] => 7 ) Printing tag ... [[$testSpeed?status=`0`]] Pass - 8 Printing chunk output ... 0 Printing chunk content ... [[+status]] Printing properties ... Array ( [status] => 8 ) Printing tag ... [[$testSpeed?status=`0`]] Pass - 9 Printing chunk output ... 0 Printing chunk content ... [[+status]] Printing properties ... Array ( [status] => 9 ) Printing tag ... [[$testSpeed?status=`0`]]