Archive for February, 2010

Outfox The Fox

No Comments »

Sometimes I encounter code like this:

  u >>= 2; // Fastest way to divide by 4.

I usually burst out singing: Those who try to tangle with my daring do, end up at the angle that herring do. (Danny Kaye) [Outfox the Fox] Never try to outfox the compiler. Take a look at what gcc generates to divide an integer by 13 in x64 and PowerPC assembler (approximately):

x64Idiv PowerIdiv

It is not uncommon for a divide instruction to take more than four times as long as a multiplication. Shift and register loading typically are up to fifty times faster. Above (speed) optimization is a simplified (i.e., unsigned) example. It does not take into account that floating point divides can be executed in parallel with integer code, it does not take into account the instructions in the CPU’s pipeline, it does not take into account surrounding code, it does not take into account MMX/SSE instructions, etc. For the curious, there is a excellent blog post on the why and how of multiply/shift-back. Remember, the rule is: you will never outfox the GNU-fox or any other optimizing fox. But if you have to outfox the fox look for inspiration first.

Augmented Virtual Reality

No Comments »

In a few years, this is going to be totally normal to use. People will just complain about how slow it runs… on their phone. Seriously.

It’s Easy, It’s Obvious

No Comments »

No comment. Well, ok, as a coder, I know I have to comment.
So here: // No Comment Necessary.

The Counts

1 Comment »

countingMy attention was recently focused (thanks John) on an article by Flickr’s Kellan, describing how Flickr generates system wide unique IDs for user data like photos and comments. In short they have a dedicated ID generator that issues unique, bigint(20) numbers. They implemented this by keeping an almost empty table in MySQL and have the server do a non-standard REPLACE INTO and return the SELECT LAST_INSERT_ID(); of the table. The article claims that they needed a dedicated ID generator because they could not come up with a way to make independent servers generate a system wide unique ID and to prevent the introduction of a single-point-of-failure they came up with a way to make multiple independent servers generate a system wide unique ID. It is “a great example of the Flickr engineering dumbest possible thing that will work design principle.” (Kellan Elliott-McCrea) [Ticket Servers: Distributed Unique Primary Keys on the Cheap] But there is no such thing as dumbest-possible-thing. Give me a candidate dumbest-possible-thing and I’ll add a client-server layer and presto you have something even dumber. Like solving a multi-machine problem by building a multi-machine server.
However I am in favor of system design that goes for the “simplest possible thing that will work (but not simpler than that).” And others might rightfully argue that it’s impossible to prove a candidate simplest thing to actually be the simplest. Oh, I am such a hypocrite, and I did do nothing in my life that comes even remotely close to an installation like Flickr, they rock! But it’s amusing nonetheless.

HipHop: PHP to C++ Compiler


HipHopHipHop for PHP isn’t technically a compiler itself. Rather it is a source code transformer. HipHop programmatically transforms your PHP source code into highly optimized C++ and then uses g++ to compile it. HipHop executes the source code in a semantically equivalent manner and sacrifices some rarely used features — such as eval() — in exchange for improved performance. HipHop includes a code transformer, a reimplementation of PHP’s runtime system, and a rewrite of many common PHP Extensions to take advantage of these performance optimizations. (Haiping Zhao) [HipHop for PHP: Move Fast]
Given the totally adhock way PHP is structured combined with the fact that Zhao claims his compiler is not a compiler but a “source code transformer” makes me wonder what exactly is going on. These steps the transformer takes to do it’s transforming look suspiciously like compiler steps:
Whenever I heard people redefine the term “compiler” it usually turned out they were hand coding a recursive decent parser. I know, it’s easy to bash something you have no experience with, but if someone is talking about “reimplementation of PHP’s runtime system” any professional-coder should raise an eyebrow. Besides, would you want to clone all that PHP magic stuff into a new runtime system? Next to a PHP compiler, Zaho is also writing a PHP interpreter: We have also developed HPHPi, which is an experimental interpreter designed for development. When using HPHPi you don’t need to compile your PHP source code before running it. It’s helped us catch bugs in HipHop itself and provides engineers a way to use HipHop without changing how they write PHP. My guess is that facebook would have been better off with a formal language that is PHP like but much simpler and mostly orthogonal. On the other hand, being well designed is no prerequisite for being popular.

Control Is The Ultimate Illusion

No Comments »

peoplewareTom DeMarco has turned 180o, finally! As one of the initiators and most prolific proponents, if not principle responsibles of the software engineering myth, he finally sees his erring ways. After selling a fuckton of books on software-engineering like “Peopleware,” “Deadline,” “Controlling Software Projects,” and “Waltzing with Bears.” he now, finally, takes his distance. A bit late after 40 years, but I guess, the money was too good. He, at long last, acknowledges the folly of his most famous quote: “You can’t control what you can’t measure.” Never came so much stress to so many coders from so few words. Pity he does not apologize profoundly, but still it is good that he basically tells the world he has been wrong and misinterpreted. Finally he acknowledges, what all real-life-coders knew all along, the more you focus on control, the more likely you’re working on a project that’s striving to deliver something of relatively minor value. (Tom DeMarco) [Software Engineering: An Idea Whose Time Has Come and Gone?] It is ending the era of the software engineering myth. It has hit the community hard, just look at a few reactions: CH, iTwire, Stackoverflow, Software Architecture, Never In Doubt.