0

0

facebook's HipHop for PHP: Move Fast

php中文网

php中文网

发布时间:2016-06-23 14:37:46

|

979人浏览过

|

来源于php中文网

原创

    one of the key values at facebook is to move fast. for the past six years, we have been able to accomplish a lot thanks to rapid pace of development that php offers. as a programming language, php is simple. simple to learn, simple to write, simple to read, and simple to debug. we are able to get new engineers ramped up at facebook a lot faster with php than with other languages, which allows us to innovate faster.

   today i'm excited to share the project a small team of amazing people and i have been working on for the past two years; hiphop for php. with hiphop we've reduced the cpu usage on our web servers on average by about fifty percent, depending on the page. less cpu means fewer servers, which means less overhead. this project has had a tremendous impact on facebook. we feel the web at large can benefit from hiphop, so we are releasing it as open source this evening in hope that it brings a new focus toward scaling large complex websites with php. while hiphop has shown us incredible results, it's certainly not complete and you should be comfortable with beta software before trying it out.

   hiphop 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.
scaling php as a scripting language

   php's roots are those of a scripting language, like perl, python, and ruby, all of which have major benefits in terms of programmer productivity and the ability to iterate quickly on products. this is compared to more traditional compiled languages like c++ and interpreted languages like java. on the other hand, scripting languages are known to generally be less efficient when it comes to cpu and memory usage. because of this, it's been challenging to scale facebook to over 400 billion php-based page views every month.

   one common way to address these inefficiencies is to rewrite the more complex parts of your php application directly in c++ as php extensions. this largely transforms php into a glue language between your front end html and application logic in c++. from a technical perspective this works well, but drastically reduces the number of engineers who are able to work on your entire application. learning c++ is only the first step to writing php extensions, the second is understanding the zend apis. given that our engineering team is relatively small ? there are over one million users to every engineer ? we can't afford to make parts of our codebase less accessible than others.

   scaling facebook is particularly challenging because almost every page view is a logged-in user with a customized experience. when you view your home page we need to look up all of your friends, query their most relevant updates (from a custom service we've built called multifeed), filter the results based on your privacy settings, then fill out the stories with comments, photos, likes, and all the rich data that people love about facebook. all of this in just under a second. hiphop allows us to write the logic that does the final page assembly in php and iterate it quickly while relying on custom back-end services in c++, erlang, java, or python to service the news feed, search, chat, and other core parts of the site.

   since 2007 we've thought about a few different ways to solve these problems and have even tried implementing a few of them. the common suggestion is to just rewrite facebook in another language, but given the complexity and speed of development of the site this would take some time to accomplish. we've rewritten aspects of the zend engine ? php's internals ? and contributed those patches back into the php project, but ultimately haven't seen the sort of performance increases that are needed. hiphop's benefits are nearly transparent to our development speed.
hacking up hiphop

   one night at a hackathon a few years ago (see prime time hack), i started my first piece of code transforming php into c++. the languages are fairly similar syntactically and c++ drastically outperforms php when it comes to both cpu and memory usage. even php itself is written in c. we knew that it was impossible to successfully rewrite an entire codebase of this size by hand, but wondered what would happen if we built a system to do it programmatically.

   finding new ways to improve php performance isn't a new concept. at run time the zend engine turns your php source into opcodes which are then run through the zend virtual machine. open source projects such as apc and eaccelerator cache this output and are used by the majority of php powered websites. there's also zend server, a commercial product which makes php faster via opcode optimization and caching. instead, we were thinking about transforming php source directly into c++ which can then be turned into native machine code. even compiling php isn't a new idea, open source projects like roadsend and phc compile php to c, quercus compiles php to java, and phalanger compiles php to .net.

   needless to say, it took longer than that single hackathon. eight months later, i had enough code to demonstrate it is indeed possible to run faster with compiled code. we quickly added iain proctor and minghui yang to the team to speed up the pace of the project. we spent the next ten months finishing up all the coding and the following six months testing on production servers. we are proud to say that at this point, we are serving over 90% of our web traffic using hiphop, all only six months after deployment.
how hiphop works

   the main challenge of the project was bridging the gap between php and c++. php is a scripting language with dynamic, weak typing. c++ is a compiled language with static typing. while php allows you to write magical dynamic features, most php is relatively straightforward. it's more likely that you see if (...) {...} else {..} than it is to see function foo($x) { include $x; }. this is where we gain in performance. 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.

   the transformation process includes three main steps:

   static analysis where we collect information on who declares what and dependencies,
type inference where we choose the most specific type between c++ scalars, string, array, classes, object, and variant, and
code generation which for the most part is a direct correspondence from php statements and expressions to c++ statements and expressions.

   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.

   overall hiphop allows us to keep the best aspects of php while taking advantage of the performance benefits of c++. in total, we have written over 300,000 lines of code and more than 5,000 unit tests. all of this will be released this evening on github under the open source php license.
learn more this evening

   this evening we're hosting a small group of developers to dive deeper into hiphop for php and will be streaming this tech talk live. check back here around 7:30pm pacific time if you'd like to watch.

   as i'm sure there will be plenty of questions, starting this evening take a look at the hiphop wiki or join the hiphop developer mailing list. you'll also find us at fosdem, scale, php uk, confoo, tek x, and oscon over the next few months talking about hiphop for php. we're very excited to evolve hiphop into a thriving open source project along with all of you.

小绿鲸英文文献阅读器
小绿鲸英文文献阅读器

英文文献阅读器,专注提高SCI阅读效率

下载
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法

本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。

1142

2026.02.13

微博网页版主页入口与登录指南_官方网页端快速访问方法
微博网页版主页入口与登录指南_官方网页端快速访问方法

本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。

371

2026.02.13

Flutter跨平台开发与状态管理实战
Flutter跨平台开发与状态管理实战

本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。

245

2026.02.13

TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

37

2026.02.13

Redis高可用架构与分布式缓存实战
Redis高可用架构与分布式缓存实战

本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。

114

2026.02.13

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

77

2026.02.12

雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

17

2026.02.12

豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

863

2026.02.12

PostgreSQL性能优化与索引调优实战
PostgreSQL性能优化与索引调优实战

本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

123

2026.02.12

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号