Re: Comments about PHPTAL package proposal?
| From: | Greg Beaver | Date: | Mon, 20 Apr 2009 13:20:56 +0000 |
| Subject: | Re: Comments about PHPTAL package proposal? | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-51767@lists.php.net to get a copy of this message | ||
Kornel Lesiński wrote:
> I've got one more related issue: Is it OK to include files within
> methods (conditionally)?
yes, many packages do this to include drivers
>
> 99% of the time PHPTAL runs compiled, cached copies of templates, and
> only requires small fraction of classes to run.
> By including only necessary minimum, I get 3-fold performance increase
> without opcode cache, and 2-fold with opcode cache (on simple
> benchmark that runs PHPTAL with empty template).
>
> Currently I have:
>
> class PHPTAL {
> …
> function parse()
> {
> require_once 'PHPTAL/Dom/DocumentBuilder.php';
> …
> }
>
> This isn't pretty and probably APC-unfriendly as well. Is there a
> better solution?
The best solution allowed by PEAR CS is to determine which files are
always needed, and to always require_once them.
Obviously, since you observe a real-world performance improvement, this
means you've found files that are not needed, and it is very smart not
to load them.
Because the require_once is in a method, it is already conditional
(processed at run-time), and so you might find even more performance
enhancement if you were to use:
if (!class_exists('PHPTAL_Dom_DocumentBuilder')) {
include 'PHPTAL/Dom/DocumentBuilder.php';
}
I am wary of this kind of construct, as it is hugely slow in the
top-level, but I have not benchmarked it inside a method. However, a
better system might be to put all of the lazy loading into a separate method
function loadClasses()
{
static $loaded = 0;
if ($loaded) {
return;
}
include 'PHPTAL/Random/Class1.php';
include 'PHPTAL/Random/Class2.php'; //etc.
}
and call loadClasses() from parse().
Even better yet, split the parsing into a completely separate class, so
that the parser is loaded only if a template needs recompiling.
function parse()
{
static $loaded = null;
if (!$loaded) { // this may be slightly faster if several templates
need parsing, but who knows
include 'PHPTAL/Parser.php';
$parser = new PHPTAL_Parser();
}
return $parser->parse($this);
}
Again, I haven't benchmarked any of these, so would be interested to
hear if any solution results in an improvement for you.
Thanks,
Greg