Is this excessive optimization and paranoia?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm reworking some relatively simple contact forms on my company's
various websites, and I've written the following bit of code to
"generate" HTML versions of the emails sent out:


if( is_readable( TEMPLATE_DIR . '/email.tpl.php' ) ) {
    if( ( @include( TEMPLATE_DIR . '/email.tpl.php' ) ) !== false ) {
        $mime->setHTMLBody( ob_get_contents() );


(Note: I'm using the PEAR Mail and Mail_mime class.)

Now, this bit of code will, in some of the scripts, be executed in a
loop (potentially up to 50 times in one request). I have two question
about this...

1) As you can see, I'm checking if the file that the HTML email is
created from can actually be read before firing up the output
buffering mechanism. I've done this as an optimization, to prevent the
output buffering mechanism from unnecessarily and repeatedly starting
and stopping in the event that the template just can't be read. Is
this a good idea, or is it just excessive?

2) I'm also checking to see if the include() actually succeeded before
setting setting the HTML body. Can you think of a situation in which
is_readable() will return true, but the include() will fail, or am I
being overly paranoid?

Thanks in advance,


Re: Is this excessive optimization and paranoia?

include will check if it's readable and will fail on other file system
errors also.

On Feb 10, 7:25 pm, wrote:
Quoted text here. Click to load it

Re: Is this excessive optimization and paranoia?

ryan.m.lange wrote:

Quoted text here. Click to load it

Well, an obvious answer would be that this will happen when the file
system permissions are changed during the split-second period between
your calls to is_readable() and include().

I'm sure there are plenty of other possible causes too.

Either way though, this code is incredibly unlikely to be a bottleneck for
you unless you're reading from an incredibly slow file system (e.g. a
network share over a GPRS connection). If your script's speed is causing
problems, spend some time properly analysing which parts of it are causing
the slow-down, and then look at ways of optimising them -- but I'd be very
surprised to find that a few extra calls to is_readable() are responsible
for noticeable performance problems. If your script's speed is *not*
causing problems, then what are you worrying about?

Toby A Inkster BSc (Hons) ARCS
[Geek of HTML/SQL/Perl/PHP/Python/Apache/Linux]
[OS: Linux, up 12 days, 19:17.]

                             Mince & Dumplings /

Site Timeline