• Do not register here on develop.twiki.org, login with your twiki.org account.
• Use View topic Item7848 for generic doc work for TWiki-6.1.1. Use View topic Item7851 for doc work on extensions that are not part of a release. More... Close
• Anything you create or change in standard webs (Main, TWiki, Sandbox etc) will be automatically reverted on every SVN update.
Does this site look broken?. Use the LitterTray web for test cases.

Item4699: 'Precompile' templates for performance

Item Form Data

AppliesTo: Component: Priority: CurrentState: WaitingFor: TargetRelease ReleasedIn
Engine   Enhancement No Action Required   n/a  

Edit Form Data

Summary:
Reported By:
Codebase:
Applies To:
Component:
Priority:
Current State:
Waiting For:
Target Release:
Released In:
 

Detail

Template loading is a multi-stage process. At the beginning there is an "invariant" phase where %TMPL:INCLUDE and simpe TMPL:Pare expanded. The results of this phase do not depend on any settings except those in configure.

With the splitting up of template files, TWiki spends quite a while loading templates. If we cache the results of the invariant load phase, then we can achieve a 20% performance improvement in a simple page view, for the sake of ~30 lines of code.

 

 Index: TWiki/Templates.pm
===================================================================
--- TWiki/Templates.pm  (revision 14986)
+++ TWiki/Templates.pm  (working copy)
@@ -223,6 +223,9 @@
 sub readTemplate {
     my( $this, $name, $skins, $web ) = @_;

+    my $result = $this->_loadFromCache($name, $skins, $web);
+    return $result if defined($result);
+
     $this->{files} = ();

     # recursively read template file(s)
@@ -241,7 +244,7 @@
         return $text;
     }

-    my $result = '';
+    $result = '';
     my $key  = '';
     my $val  = '';
     my $delim = '';
@@ -275,6 +278,9 @@
     $result =~ s/(%TMPL\:P{.*?}%)/_expandTrivialTemplate( $this, $1)/geo;

     $result =~ s|^(( {3})+)|"\t" x (length($1)/3)|geom;  # leading spaces to tabs
+
+    $this->_addToCache($name, $skins, $web, $result);
+
     return $result;
 }

@@ -395,4 +401,32 @@
    return $text;
 }

+# Dumb cache for precompiled templates
+sub _loadFromCache {
+    my ($this, $name, $skins, $web) = @_;
+    my $session = $this->{session};
+    $skins = $session->getSkin() unless defined( $skins );
+    $web ||= $session->{webName};
+    my $id = "$name$skins$web";
+    my $result = undef;
+    if (open(F,"<$TWiki::cfg{WorkingDir}/tmp/$name.tmpl_cache")) {
+        local $/;
+        $result = <F>;
+        close(F);
+    }
+    return $result;
+}
+
+sub _addToCache {
+    my ($this, $name, $skins, $web, $result) = @_;
+    my $session = $this->{session};
+    $skins = $session->getSkin() unless defined( $skins );
+    $web ||= $session->{webName};
+    my $id = "$name$skins$web";
+    if (open(F,">$TWiki::cfg{WorkingDir}/tmp/$name.tmpl_cache")) {
+        print F $result;
+        close(F);
+    }
+}
+
 1;

 

I checked into MAIN because I can't see why we wouldn't want to do this. Note that I also modified the checkin code for d.t.o to take account of the fact that template files may change on each checkin, and also to make sure this gets a good workout.

-- TWiki:Main/CrawfordCurrie - 22 Sep 2007

idiot, that won't work, for two reasons. See me after school. The sections of the checkins I have left in are general cleanup.

CC 

ItemTemplate
Summary 'Precompile' templates for performance
ReportedBy TWiki:Main.CrawfordCurrie
Codebase

SVN Range TWiki-4.3.0, Thu, 20 Sep 2007, build 14972
AppliesTo Engine
Component

Priority Enhancement
CurrentState No Action Required
WaitingFor

Checkins TWikirev:14996 TWikirev:14997
TargetRelease n/a
ReleasedIn

Edit | Attach | Watch | Print version | History: r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r4 - 2007-09-22 - CrawfordCurrie
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback