Replacing include with include_once in Smarty::fetch()?

[email protected] ("Matthias Pigulla")
Newsgroups php.smarty.dev
Message-ID <00A2E2156BEE8446A81C8881AE117F19110ABA@companyweb>
Dear smarty-developers,

I'm extending a mailing application to allow for sending HTML emails. I
use Smarty as template engine to build personalized e-mail messages
(Smarty outputs html as well as text mime parts).

Right now, I'm doing some performance tests and have to call
$smarty->fetch a few thousand times (once for every personalized e-mail
being sent), which is a very realistic - if not even small - setup for
my purposes.

The problem is that the include($_smarty_compile_path) around line 1258
in Smarty.php makes up a huge share of total execution time and also
consumes a lot of memory, although it just includes the same compiled
template again and again. I could imagine that the PHP parser will
analyze/precompile the file upon every inclusion? I'm not that familiar
with PHP internals...

As a quick experiment, I wrote a postfilter to add "<?php function
foo(&$this) { ?>" and "<? } ?>" around the compiled template and changed
Smarty::fetch() to "include_**once**(...); foo($this);".

That way I could reduce the execution time spent in that code region to
a fraction of 1/8th, memory consumption is reduced by about 3.5MB. I
suppose that longer template code and more repetitions would even yield
better improvements.

I know that usually you would hardly fetch the same template repeatedly
during the same request, so maybe that problem only occurs with my very
specific setup.

However, what do you think of automatically placing template code in a
function body? The function name could be derived from the resource or
file name. Note that this would require calling the function instead of
just including the compiled template; however, as long as Smarty::fetch
is the only section of code including templates, that should not be a
problem.

Any ideas what else might break? Possible benefits?

Best regards,
Matthias
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.