php

Optimize cache for autoloading classes in haXe/PHP

in

The code generated using haXe/PHP uses the PHP autoloading feature to dynamically load classes definitions when they are required. This method has many advantages but a drawback too; the thing is that the autoloading function (hidden in php.Boot) needs to scan the lib folder to look for type definitions. It is not really a big work since only the file names are read without the need to immediately open the files, nevertheless this work is made each time the page is accessed. To reduce the impact of this operation you can simply create a cache folder at the same level of lib and give it the proper attributes for PHP being able to write files in there. On the next use of your script, a new file haxe_autload.php will be automatically generated and will contain all the info required by the autoloading function.

As you can guess, the impact of this feature is proportional to the number of classes included in your project.

Be warned that it is better to use the cache only on production sites because during development it is quite normal adding new classes and the cache will not include them unless the haxe_autload.php file is manually deleted. If you need to upgrade your production code, remember to delete that file first!

edit: I've made a few informal tests compiling a project that generates 76 files but uses only a few of them to maximize the impact of the cache trick. The results are the following but remember that your mileage may vary greatly and you can also find out that you will not have any benefit from abilitating the cache feature.

Execution time:
198ms no cache
151ms cache activated (31% speed gain)

haxe.Stack for PHP

in

Starting from haXe 2.04 the PHP generator supports the -debug switch. The implementation is almost identical to the haXe/JS one where the function calls are stored in a stack. You can see it working when you have an uncaught exception or using the haxe.Stack methods as in the following example:

import haxe.Stack;
class Test {
  static function main() {
    var f = function() { ref(); };
    f();
  }
	
  static function ref() {
    throw "error";
  }
}

The output will be:

uncaught exception: error

Called from Test::ref
Called from Test::main@4
Called from Test::main

Note that @4 means that a local function is involved.
You can obtain the same output without generating an exception replacing throw "error" with:

trace(Stack.toString(Stack.callStack()));

Is it appropriate to remove the -debug switch when compiling for deployment since it adds extra code to your output.

Templo

Templo is the advanced template system for haXe. It consists of one small haXe loader, and one commandline tool temploc which is capable of compiling a .mtt template file either to Neko or PHP.

temploc is written in NekoML because of its parser facilities.

haXe/PHP in version 2.01, what's next?

in

If you are a haXe user you already know that PHP is already an official target of the haXe platform. The second version that includes PHP is 2.01 and it has reached a nice level of stability mainly thank to Nicolas who has adopted it for his personal website. He pointed out some annoying bugs that have been promptly fixed by me :). But unfortunately for my personal life I realized that I still have a lot to work on to make haXe/PHP still better.

haXe/PHP

haXe/PHP is an extension to the haXe compiler that enables the generation of PHP code.
Why haXe/PHP? One of the biggest concerns in using haXe as a language for the server-side environment is that you need to fully control you web-server environment. This is a no go on many “cheap” (and not so cheap) shared-hosting services but PHP on the contrary is almost globally available.

Starting from version 2.01 haXe/PHP is officially part of haXe!

haXe/PHP Alpha 2 Status

in

The second release of haXe/PHP is available.

I would like to discuss here the development status of the project and maybe receive some feedback.

First of all, my todo list is almost empty and this is a good thing. The generator and the class libraries have made a big step forward from the first alpha. I consider the generator usable and I am actually using it for two small projects I am developing.