Saturday, March 26, 2011

Need for speed

Working as a web developer maintaining a big web service can quickly make you understand that "everything counts in large amounts", and identifying and eliminating bottlenecks eventually becomes part of your daily routine. There are many ways to optimize your web application: from migrating to faster hardware platform, through reducing the number of network requests and extensive use of various caching layers, to refactoring of the code and optimizing your algorithms. Some very interesting performance tips can be found at Yahoo Developer Network - but sometimes it's just not enough, and you have to reach deep into the core of your software engine.

One way is to rewrite your application using either Java or C/C++. Another way, especially when your code base is so huge that it does not pay to rewrite it (even partially) is to try to compile the scripts to machine code. The best known engines implementing this technique are PyPy for Python, developed as a followup to the Psyco project, and HipHop for PHP, developed at Facebook. What is especially interesting about the HipHop story is that it has been announced shortly after Koen Deforche stated on his blog, that Facebook could reduce the power consumption in their data centers by 75% only by using C++ instead of PHP. What is not surprising, Koen Deforche is an author of Wt, a C++ toolkit designed for building web applications.

What makes Wt stand out among its alternatives is that it completely masks all underlying web technologies and makes Wt development look more like Qt than web. However, developing regular web services with Wt is not easy. For a last few days I tried to create a fairly simple application with Wt and I simply gave up. For me it was just like playing with Lego Technic for Aliens. Maybe it's because Wt, as its author admits, is not a toolkit for porting an existing website to C++, but a toolkit for porting an existing desktop application to the web, and I'm primarily a web developer. Or maybe it's because there is too little useful real life examples out there and the community seems to be almost non existing. In my opinion Wt is rather a niche product, best suited for developing highly scalable intranet applications for big companies.

At the opposite pole there is KLone, a lightweight web framework that at first glance looks like JSP. KLone allows creating dynamic content by embedding C++ code into HTML, much like JSP does with Java. It also provides all necessary mechanisms to deal with sessions, requests, etc. The application is of course compiled to native code and can distributed as a standalone executable, since it includes its own embedded http server. At the moment it's definitely my choice if you want to write a web service based on C++, no matter if it's because your hardware is too poor for Java or you just don't want to hog your system resources with Tomcat.