Or, I dunno, the language semantics. But hey, you armchair compiler/runtime enthusiasts go
on thinking that C/C++ gives a 20x performance advantage when executing dynamicly-typed
language semantics. I mean, obviously the compiler can just turn $x+$y into a single
machine op, right? It's not like it has to translate that into a function call that dispatches into
the correct + operator implementation for the operands after possibly converting them to
compatible types at runtime or anything, and then encapsulating the result into a dynamic
type. Nice and simple machine code translation like native C math just isn't even remotely
possible here, guys. That's why there is so much hoopla over tracing JIT engines for
dynamic languages, although a tracing engine for a staticly-typed language is still even
faster.
Posted Feb 2, 2010 20:15 UTC (Tue) by JoeBuck (subscriber, #2330)
[Link]
It's possible to do better. But to win, you need to have good enough dataflow analysis to know that, for example, $x is always an integer and $y is always a string, so your compiled code can omit the dynamic dispatch.
Facebook's "HipHop" PHP translator
Posted Feb 2, 2010 23:33 UTC (Tue) by bk (guest, #25617)
[Link]
Right, so your whole server blows up when a malicious user enters "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#!/bin/sh\nrm-rf" instead of "1/1/72".
Facebook's "HipHop" PHP translator
Posted Feb 3, 2010 9:51 UTC (Wed) by alankila (subscriber, #47141)
[Link]
The point of the dataflow analysis is to know when that is possible and act accordingly.
Facebook's "HipHop" PHP translator
Posted Feb 4, 2010 23:48 UTC (Thu) by efexis (guest, #26355)
[Link]
No, but now I have to horizontally scroll to read this thread... thanks for that :-/
Facebook's "HipHop" PHP translator
Posted Feb 2, 2010 20:52 UTC (Tue) by dskoll (subscriber, #1630)
[Link]
I mean, obviously the compiler can just turn $x+$y into a single
machine op, right?
Did you read the article? Among other things, it claimed:
Whenever possible our generated code uses static binding for functions and variables. We also use type inference to pick the most specific type possible for our variables and thus save memory.
So yes, I would think that in a lot of cases, it could generate very fast code for $x+$y.
Facebook's "HipHop" PHP translator
Posted Feb 3, 2010 0:09 UTC (Wed) by elanthis (guest, #6227)
[Link]
Reading an article and a site's home page does not make you an expert,
clearly. Let's pretend I don't have actual real experience with this kind
of tech and just think this through logically instead of quoting soundbites
from a web page. If you are writing:
code like:
$x = 1;
$y = 3;
$z = $x + $y;
Then sure, it can use type inference. When you have something like:
function foo($x, $y) {
return $x + $y;
}
Then the compiler has no freaking clue what is going to be passed in.
Generally, I find, people do not create variables to hold static values,
but instead they tend to hold data from various other sources, many of
which by PHP semantics simply cannot have their types inferred. Some code
can be highly optimized by such techniques, some cannot, and the latter
just happens to be more common in at least the apps I've worked with.
Unless the compiler is compiling multiple versions of these functions (like
how a tracing JIT compiler might), then most of the code in a real-life app
(not a micro-benchmark) is going to be inelligable for type lowering, and
the runtime has to fall back on full dynamic type semantics.
This is one reason that, no matter what advances the dynamic language folks
working on JS or Python or whatever might possibly make, the speed of a
statically-typed language can always be greater by just using the same
techniques the dynamic runtimes use but without the type guards (for
tracing implementations) or dynamic fallbacks.
Facebook's "HipHop" PHP translator
Posted Feb 3, 2010 10:31 UTC (Wed) by PO8 (guest, #41661)
[Link]
Static analysis techniques used in the last 20 years can generally infer the type of parameters to a procedure or function call from context. Often they can do it even without looking at the whole program. They are good at looking at small clues and following chains of reasoning.
I'm a huge fan of static typing, but from an efficiency point of view it's just not a huge win in 2010. There are too many well-known clever tricks for making dynamically-typed languages run fast. I like static typing because it serves as a kind of formal analysis that increases the odds of my writing a correct program. If it didn't do that I'd have ditched it for dynamic typing long ago.
Facebook's "HipHop" PHP translator
Posted Feb 3, 2010 12:06 UTC (Wed) by HelloWorld (guest, #56129)
[Link]
If it is "not a huge win in 2010", then why are statically typed languages still vastly faster in any benchmark?
For example, compare JavaScript in Google's V8 to Haskell: http://shootout.alioth.debian.org/u32/benchmark.php?test=...
JavaScript is between three and onehundred times slower. And this is a state-of-the-art VM using tracing and all that.
Facebook's "HipHop" PHP translator
Posted Feb 3, 2010 14:50 UTC (Wed) by darthscsi (subscriber, #8111)
[Link]
Google's V8 implementation of JavaScript is not the fastest dynamically
typed language implementation. PLT Scheme is hardly the fastest
implementation Scheme and it handily beats Google's V8.
I wish Chez Scheme was on the list, as it's the fastest scheme I know of,
but it is a commercial implementation. It does all sorts of tricks with
object padding, stashing type tags for the common types inside pointers,
placing objects in per type regions (middle bits become type tags), type
inference and unboxing, and more.
Facebook's "HipHop" PHP translator
Posted Feb 5, 2010 20:47 UTC (Fri) by HelloWorld (guest, #56129)
[Link]
Yes, perhaps it is possible to make a dynamically typed language almost as fast as a statically typed one. The question is, why bother? It makes the implementation much more complex in order to achieve the questionable goal of dynamic typing. I don't think that's a good idea.
Facebook's "HipHop" PHP translator
Posted Feb 3, 2010 14:08 UTC (Wed) by dskoll (subscriber, #1630)
[Link]
function foo($x, $y) {
return $x + $y;
}
Then the compiler has no freaking clue what is going to be passed in.
Possibly. But still, I'd expect a better than 2x speedup. Parsing the PHP code, especially all the included and required files has to be pretty slow.
Also, the "+" operator in PHP can only work on numbers. So $x and $y
are either integers or floats; sorting out the possible cases is pretty quick.
Facebook's "HipHop" PHP translator
Posted Feb 3, 2010 15:10 UTC (Wed) by liljencrantz (guest, #28458)
[Link]
Parsing is done ahead of time, so it is in fact more or less free. The plus operator also works on arrays.
Facebook's "HipHop" PHP translator
Posted Feb 3, 2010 15:18 UTC (Wed) by dskoll (subscriber, #1630)
[Link]
Parsing is done ahead of time, so it is in fact more or less free.
Eh? Not in standard PHP. Parsing is free only if you're using an opcode cache like APC or similar.
The plus operator also works on arrays.
So it does. Still, it's relatively restricted. It doesn't work on strings or objects.
Facebook's "HipHop" PHP translator
Posted Feb 4, 2010 14:17 UTC (Thu) by jond (subscriber, #37669)
[Link]
I think it can be assumed a site dealing with "400 billion PHP-based page
views every month" is using an opcode cache already.