简体   繁体   中英

Is code written in Hack faster than code written in PHP on HHVM?

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?

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 (ie, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM