Hi,

Here's a very useful performance patch to avoid useless stat() calls
in Template::Provider, if we set Caching on and set a large number to
$Template::Provider::STAT_TTL.

The scenario is,

1) we have INCLUDE_PATH => [ "/foo", "/bar", "/baz" ]
2) we only have /baz/xxx.tt (the last one)

and the template [% INCLUDE xxx.tt %] will cause stat() (i.e. -f $path
in Provider.pm line 528) calls to "/foo/xxx.tt", "/bar/xxx.tt" every
time we process the template, even if we set caching on and set a huge
number to $STAT_TTL, which is sad.

This causes a severe performance problem in the large scale web app
like what we Six Apart are running. We set a ridiculously large number
to $STAT_TTL and use the app version number part of the cache path, so
once our app is deployed to the production, we hope TT won't ever do
stat() calls to the original .tt file more than once.

Thanks,

--- lib/Template/Provider.pm    2006-05-26 21:21:24.000000000 +0900
+++ /usr/local/lib/perl/5.8.4/Template/Provider.pm      2006-10-24
14:44:39.000000000 +0900
@@ -495,6 +495,7 @@
    # caching is enabled if $size is defined and non-zero or undefined
    my $caching = (! defined $size || $size);

+    my $now = time;
    INCLUDE: {

        # the template may have been stored using a non-filename name
@@ -525,6 +526,10 @@
                    unless $error;
                last INCLUDE;
            }
+            elsif ($self->{ NOTFOUND }->{ $path } and
+                   ($now - $self->{ NOTFOUND }->{$path} < $STAT_TTL)) {
+                $self->debug("$path was not found, skip.") if $self->{ DEBUG };
+            }
            elsif (-f $path) {
                $compiled = $self->_compiled_filename($path)
                    if $compext || $compdir;
@@ -553,6 +558,8 @@
                # all done if $error is OK or ERROR
                last INCLUDE if ! $error
                    || $error == Template::Constants::STATUS_ERROR;
+            } else {
+                $self->{ NOTFOUND }->{ $path } = time;
            }
        }
        # template not found, so look for a DEFAULT template


--
Tatsuhiko Miyagawa

_______________________________________________
templates mailing list
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to