PHP-Mix
Last Updated 2002/12/27
 
Menu
 Index
 Introduction
 Download
 Demo
 PHP-Mix ML
 SourceForge
 
Links
 PHP
 Partial Evaluation
 Takebe Page
The Mechanism of PHP-Mix
 
It is empirically known that most of dynamic web pages contain fragments that are not so frequently changed compared to other parts of the page. So we can improve performance of dynamic web pages by caching these fragments. Many technologies have been developed to cache fragments of web pages, such as ASP.NET and ESI.

However, to cache page fragments, we need some special scripting engine that supports this kind of caching operation. It is also necessary for us to develop dynamic web pages by separated fragments. It may take more time and effort.

PHP-Mix solves these problems using program transformation technique.

To see this, consider a web page in the following figure. This page shows a list of news and a name of a user who browses this page.


Figure 1. An Example Page.

An outline of a script that generates this page may look like the following. We assume that news are stored in a database and a user name is generated from a cookie.

  <html>
  <?
    // Getting a user name from cookie.
  ?>
  ...
  Hello, <%= $username %>!
  ...
  * Latest news
  <?
    // Fetching news from database
    ...
    while ($row = ...) {
      echo $row["title"];
      ...
    }
  ?>
  </html>
Unfortunately, this script generates news list for every request in spite of the fact that news list is not changed so frequently. This will lead poor performance. And we cannot cache entire of this page because user name part must be changed depending on a cookie from a user.

PHP-Mix automatically transforms this script as follows.

  <html>
  <?
    // Getting a user name from cookie.
  ?>
  ...
  Hello, <%= $username %>!
  ...
  * Latest news
  North Korea moves nuke fuel rods
  Koizumi may pick BOJ chief from outside private sector
  ...
  </html>
In this script, news list is appears as static text. On the other hand, the user name part remains as a PHP code. As a result, only the user name part is generated for every request.

This kind of program transformation technique is known as partial evaluation and has been researched mainly for functional programming languages. PHP-Mix applies this technique to a subset of PHP.

PHP-Mix uses this transformed script as long as news in a database remains the same. When a database is changed, PHP-Mix invalidates this script and transforms the source script again.
 
The Basics of Partial Evaluation in PHP-Mix
 
In this section, I will describe how PHP-Mix transforms a PHP script. Here is the first (very simple) example.

  <?
    $a = $_COOKIE["a"];
    $b = "b";
    echo $a;
    echo $b;
  ?>
Partial evaluation technique used by PHP-Mix consists of two phases.

In the first phase, PHP-Mix determines which part of a script can be executed only using input to a script available when PHP-Mix transform a script.

For example, the above script accepts cookie value as an input. But cookie value is not available at transformation time. It is not known until some user access this script. So PHP-Mix analyzes which part of this script can be executed without cookie.

In the above script, the value of $a depends on $_COOKIE which contains cookie. So $a is also analyzed as unknown. On the other hand, $b depends only on a static string, which value is "b".

In this way, we obtain the following result by the analysis.

  <?
    $a = $_COOKIE["a"];
    $b = "b";
    echo $a;
    echo $b;
  ?>
The blue part can be executed at transformation time and the red part can't.

In the second phase, PHP-Mix executes the blue part and generates PHP scripts using the red part. In case of the above scripts, we get the result like the following.

  <?
    $a = $_COOKIE["a"];
    echo $a;
  ?>b
Things become more difficult when a source script contains controll statemens, user defined functions, etc.

(To be continued.)
 
Future Work (If Any)
 
Now I am working on developing another prototype. In this prototype, I use a partial evaluation technique called "cogen approach" which seems to be superior to a traditional partial evaluation technique used in the current prototype version of PHP-Mix.