Can we expect a speed gain by transitioning from PHP to Hack on HHVM?
I'm thinking of the strongly typed parameters / return types, in particular scalars, does that allow HHVM to do a better job at compiling the code to native code, or is the speed gain insignificant as compared to using classic PHP and its mixed types?
Hack is built from the ground up to enable static type checking, with features PHP lacks like generics, shapes, enums, hack arrays, and a well-typed standard library to enable rigorous static analysis.
Both the PHP interpreter and HHVM take PHP code and execute it. Therefore, the process of writing the code is pretty much the same in each case.
Access Hardware: Hackers use C programming to access and manipulate system resources and hardware components such as the RAM. Security professionals mostly use C when they are required to manipulate system resources and hardware. C also helps penetration testers write programming scripts.
HipHop Virtual Machine (HHVM) is an open-source virtual machine based on just-in-time (JIT) compilation that serves as an execution engine for the Hack programming language and used to support PHP execution before the release of HHVM version 4.
I answered this on Reddit a few months back. I've copied my answer below, since the state of the world hasn't changed much since then. But keep in mind that HHVM is still evolving, pretty quickly actually, and so this could easily be out of date in a month or two more.
I work on the Hack team at Facebook. The answer to this question is somewhat subtle.
Moving your PHP code from PHP5 to HHVM is likely to result in a significant speedup, as others have said. How significant depends on a ton of factors. If you're already IO bound, you may not see much at all; if you are closer to CPU bound, speedups of up to something like 5x have been reported, though you are likely to get something somewhere inbetween. You should go and try it on your own code, with a real workload -- HHVM has a bunch of factors, notably a larger startup time, that make it not do so well on microbenchmarks, but on real workloads it should outperform PHP5. For maximum benefit, refactoring your code to get things out of toplevel and into functions/classes will help a ton (we can't JIT code at toplevel), as will setting up repo authoritative mode.
But that's just plain PHP on HHVM, not Hack on HHVM. What speedup do you get from then converting your code to Hack? It depends on how you do the conversion, but the answer is, at least right now, "not very much". If you just go stick <?hh
at the top of every file, instead of <?php
, and fix any incompatibilities that come up when you run the typechecker, then your code will very likely perform the same as before. Hack and PHP code have the same runtime representation, so you haven't really changed much.
If you do that, though, then you aren't taking full advantage of Hack! If you go in and start adding type annotations, you can build up more and more information for HHVM to use at runtime. This process is what can speed up your code -- HHVM can generate type-specialized (i.e., faster) code in a lot of circumstances where before it might have not been able to infer the type. Don't expect a huge speedup here either -- this is largely theoretical right now, and there are lots of places we can be taking better advantage of the type information to generate faster code. (We don't do much with return types at runtime right now, for example.) But this is the part that might help, and might help more as HHVM gets smarter.
But of course, keep in mind that execution speed wasn't the point of Hack -- it was about developer efficiency. Any performance gain is probably not going to be worth the effort going from PHP on HHVM to Hack on HHVM. The gain in developer productivity, though, probably is.
So does moving from PHP5 to HHVM speed up your code? Very likely. Does doing a quick conversion to Hack speed it up? No. Does adding more type annotations speed it up? Maybe a little, maybe more in the future, but that's not really the point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With