in reply to Re: Re: Speed, and my sanity.
in thread Speed, and my sanity.

Turning a template into Perl code means turning a template inside out: typical templating systems use a mixture of HTML and Perl (or Perl and "something else", which isn't necessarily HTML), where the Perl is "inside" the HTML. Like this, sort of:
<b>Name: </b><% $name %><br>
The HTML is the outer layer, and the Perl is contained within special sections marked off by some delimiters. In this case, <% and %>.

Most templating systems will turn something like the above into this:

$_out->("<b>Name: </b>"); $_out->( $name ); $_out->("<br>");
Imagine $_out is a subroutine reference that is basically just
my $_out = sub { print @_ }
So essentially, "turning a template into Perl code" has created a piece of Perl code with a bunch of print statements, which is what you might have written if you hadn't used templates at all. This is what I mean by turning the template inside out: you have taken a template that was HTML with embedded Perl, and have turned it into a Perl script with embedded HTML.

The advantage to Perl code is that Perl code is fast. :) Ie. if you have a block of Perl code, you can execute it directly; you don't have to parse it as a template. So you can store it in this intermediate Perl script form, and then just run it again without reparsing the original template.

This is a relatively standard way of caching templates for many Perl templating engines. And obviously, this is just a small example, but it shows the basics of how it works.

Replies are listed 'Best First'.
Re: Re: Re: Re: Speed, and my sanity.
by Dylan (Monk) on Aug 26, 2001 at 21:42 UTC

    One would think that:

    <body bgcolor="<@BGCOLOR@>">, which with my engine has this done to it:

    s/<\@(\w+)\@>/$params{parse}->{$1}/ge; done to it, would be slower than:

    <body bgcolor="<% $params{parse}-{BGCOLOR} %>">,

    Embeding Perl in html, I guess, would be a good idea..
    But would I need a major rewrite? I have been working on Manifold for about 2.5 months.. :(

      First off I wasn't really suggesting that you use
      <body bgcolor="<% $params{parse}->{BGCOLOR} %>">
      The only difference between that and what your system currently uses is that you use a different syntax for tags. And there's nothing wrong with that. The above is just one way of using templates.

      What I was suggesting, rather, is that your compilation phase could be turning this:

      <body bgcolor="<@BGCOLOR@>">
      into this:
      $_out->('<body bgcolor="'); $_out->( $params{parse}->{BGCOLOR} ); $_out->('">');
      This can be done by tokenizing your template, then turning everything like
      <@ FOO @>
      into a statement like
      $_out->( $params{parse}->{FOO} );
      This Perl statement can then be directly executed, and moreover you can write the Perl code to disk, then directly execute it again; this means that on subsequent requests to your template, it does not have to be reparsed.

      As for whether this requires a major rewrite: it would require separating your compilation and execution phases, essentially. This is the biggest difference, because this is a mental difference: when you use the substitution operator to walk through a string and replace your template tags with other values, you are performing "compilation" and "execution" of your template at the same time. When you first compile to Perl code, then execute, you are splitting up those steps.

      So it would require a rethink of a piece of your system, and yes, it would also require rewriting some of it. But it's something to think about.