Archive for the ‘PHP’ Category

Facebook PHP compiler

Tuesday, February 15th, 2011

More and more php compilers are emerging, and it now finally looks like there are a number of decent ones out there.

Just a couple of years ago, there only were bcompiler, eaccelerator and perhaps some others, but none really good. Eaccelerator would be sensitive to server environment and only work under certain conditions, and even have special problems with different php programs, and others had similair problems.

But now it seems like there are 5-10 different compilers, and all seem to work really nice. An especially interesting one is HipHop for PHP – a php compiler written by facebook to run in their own servers. The ultimate goal of all compilers is the SPEED (and, hwell, perhaps, obsfucation too), and I can only imagine what kind of speed problems the facebook experiences, with all their traffic, so if hiphop is good for them, then it should be too for the rest of the normal mortal aplications out there.

One important thing to know though is that hiphop for PHP is not an opcode cacher or a JIT. It is actually a compiler, and produces binary code as such. Probably because of that it has problems with eval() and some other dynamic features of PHP. If that is the case with your application, then it’s probably not for you, but how often does one really use eval?

Definetely check it out if you are looking to speed up your php application without changing a line of code.


Embedding a scripting language in your own C++ programs

Wednesday, October 13th, 2010

One of my current projects has gained the critical mass complexity that can justify an embedding of a scripting language, so that some parts of the projects can be flexibly scripted instead of being hardcoded into the C++ code and thus require a time-consuming compilation every time anything changes.

After reading a little about this, I found out that there are a lot of options, and most of them are really easy!

My first idea was to have two different programs, one in C++ and the other one in PHP (but any other multipurpose script language would suffice), and make them communicate with each other. The best choice for communication would the TCP/IP sockets, and interface could be coded pretty easily. Each function that needed to be exported, could easily be mapped through the network code. Two different programs could even be located on different machines and easily connect through the net.

This solution is possible, and is not that bad actually, but it can get pretty annoying to have two different programs to run and maintain.

So there I came upon the Real solution.

The two script embedding options I liked the most were Lua and JavaScript engine spidermonkey.

Lua is an opensource scripting language, which is very easy to embed and is being used worldwide mostly in games to script the gaming logic. It’s biggest advantage is the speed. By checking different benchmarks, I found out that lua code can be just around 30 times slower than c++ code, which is VERY fast for a scripting language. (For comparison, php is like 300-400 times slower, perl is something like 100-300 times, and Java approx. 10 times).

With this speed, some pretty complex calculations could be easily done in lua, instead of being hardcoded. The other advantage is that lua is semantically extensible, which means that it has very few features out of the box, but you could easily implement almost any paradigm or design pattern, whichever is most suitable for your application.

For example, there are no classes in their usual meaning in lua, they are instead “implemented” in code. When using lua, you are not only writing programs, but also sort of create your own language as you go.

As for embedding, with a small additional opensource library (of mostly macros) Luabind, binding functions and classes of your c++ program to lua becomes a piece of cake. It almost comes down to 1 extra line of code per mapped function, really smooth.

The second option I am considering is JavaScript spidermonkey, which is an embeddable JavaScript engine from Mozilla.

It would be great to have JavaScript in my project, since it’s awesomely flexible, wide-spread and very easy to code in, having a common syntax. Also, for most web developers, JavaScript is a language where you know every class and every function already.

The downside is the speed (approx. 300-400 times slower than native) and the complexity. Also, since the language itself is much more complex than barebone lua, embedding it to applications and mapping functions and classes from c++ can sometimes be tricky and bloated with type conversions and argument checks.

So this seems to be the pro way to do it, so that’s what I am gonna do.  (This time it’s gonna be lua but I am eager to try the spidermonkey some time also).

PS. It’s a pity I have overlooked this at first, but apparently, boost (which you should use most of the time anyway, cz its awesome) has built-in python bindings which work almost as easy (sometimes even easier) than luabind. So that’s a great alternative also, especially if you are a python fan.