Sorry, you need to enable JavaScript to visit this website.


By David Stewart on September 18, 2015


When I get a job assignment in some area that is completely new to me, once I decide to go for it, I usually react with enthusiasm. You know, "I can conquer anything!" kind of mentality.

Then as I start working, I begin to realize that my enthusiasm was totally unfounded, I'm really in big trouble. It's the Reality Moment. "Why did I ever agree to do this!" That is, before some sense of competency starts to take hold again.

(This is actually quite common, and effective managers will realize this and adapt their behaviors as well, but that's subject of a different blog).

When I first took this job of leading our scripting languages area, the Reality Moment struck when I had my first real look at the challenges of dynamic languages.

For example, if you compare two strings in a program, we have some new instructions in the processor which can make these go very fast. But unfortunately, if you compare two strings in PHP, you first need to determine if the strings could be interpreted as numbers. If so, you need to treat them as numbers.

if ("1000" == "1e3") {echo "oops\n";} else {echo "whew\n";}

Anyone can see that the strings don't match, so this scrap of PHP code should print "whew", right? Let's try it. I have PHP installed on my Mac, so I can try it out on the command line:

$ php -r 'if ("1000" == "1e3") {echo "oops\n";} else {echo "whew\n";}' 

Oops indeed. The "==" operator here is helping the programmer deal with strings as numbers. So if you compare two strings which could be interpreted as numbers, you must convert them to numbers. If you want to force the strings to be compared as strings, PHP has a "===" operator:

$ php -r 'if ("1000" === "1e3") {echo "oops\n";} else {echo "whew\n";}' 

But the usage of "===" is fairly uncommon in most PHP programs. So before we can employ a fast compare instruction, we have to parse through the string to see if it could be interpreted as a number, which probably eliminates the value of our fast compare instruction.

In Python, we don't have this kind of problem.

if "1000" == "1e3" : print "oops" 
else : print "whew"

This will print "whew", so the "==" behavior is different in Python. But as I mentioned, the "+" operator will work equally well on different types. For example, consider this scrap of Python code:

a = 5 
b = 1 
print a + b

The above will print 6.

a = "5" 
b = "1" 
print a + b

The above will print 51.

That all seems rational and quite pythonic. But it also means that you can't reduce Python's BINARY_ADD opcode to a simple integer or string compare without checking the types first. Features like these are intended to make writing scripts extremely quick and easy, trusting the interpreters to figure out the right thing to do. But figuring out the "right thing to do" takes an awful lot of time when the script becomes a massive system.

How about with Javascript, very popular when used with node.js on servers? When I installed Node.js on my Mac, I can do the following:

$ node 
> "5" + "1" 
> "5" - "1" 

Surprised? Not if you are an experienced Javascript programmer. It scared the willies out of me.

What this boils down to is that when we approach any of these server dynamic languages to optimize, we can't employ the same tried-and-true techniques we have used for decades (literally) in C, C++ and Java. We need creativity mixed with discipline to make a difference here.

Source: Intel Software | Article: My Reality Moment. Why did I ever agree to do this? by David Stewart (Intel)