当前位置: 首页 > 工具软件 > HHVM > 使用案例 >

php hhvm_PHP的性能追求:从C到hhvm

督烨赫
2023-12-01

php hhvm

While focus of recent core PHP development has been on new language features, a growing concern in recent years has centered around performance. While it’s sufficient for many users, as PHP sees increased use by large sites like Wikipedia and Facebook, the ability to serve more requests on fewer servers becomes increasingly important.

尽管最近PHP核心开发的重点一直放在新的语言功能上,但近年来越来越关注的是性能。 尽管对于许多用户来说已经足够了,但随着PHP看到诸如Wikipedia和Facebook之类的大型网站越来越多地使用它,在更少的服务器上满足更多请求的能力变得越来越重要。

Some efforts have been made in this area in the last few years, both within and outside the PHP internals team. However, understanding exactly what’s going on requires a bit of background both in history and concepts.

过去几年,在PHP内部团队内部和外部都在此领域做出了一些努力。 但是,准确地了解正在发生的事情需要历史和概念方面的一些背景知识。

PHP的起源 (Origins of PHP)

In 1994, Rasmus Lerdorf wanted a simpler way to display his résumé to Internet users and monitor traffic to it. At the time, web development was done by writing CGI programs in C, which could be tedious and required knowledge of many low-level concepts like manual memory management. And so Rasmus began work on what was to become PHP.

1994年,拉斯穆斯·勒多夫(Rasmus Lerdorf)想要一种更简单的方式来向互联网用户显示简历并监控访问量。 当时,Web开发是通过用C编写CGI程序完成的,这可能是乏味的,并且需要了解许多低级概念,例如手动内存管理 。 于是Rasmus开始着手开发PHP

To truly appreciate how novel this idea was for its time requires an understanding of a few concepts. You’ve probably heard before that computers function on ones and zeros, the constituents of the binary number system. Computer processors build on this foundation by establishing sets of numerical instructions collectively referred to as assembly language which can be translated into binary. These instructions are limited to fairly simple operations, like adding two numbers together.

要真正领会这个想法在当时是多么新颖,需要了解一些概念。 您可能已经听说过,计算机在二进制数字系统的组成部分1和0上运行 。 计算机处理器在此基础上建立数字化指令集,这些数字化指令统称为汇编语言 ,可以将其转换为二进制。 这些指令仅限于相当简单的操作,例如将两个数字加在一起。

Compilers like the C compiler Rasmus used to build PHP build upon this by accepting code written in a higher-level language and converting it into binary for a specific processor. These tools allow programming languages to be more human-readable which in turn allows more complex tasks to be accomplished with less code than assembly would require, and make it possible for the same code to be compiled for multiple processors with different instruction sets.

编译器等C编译器拉斯穆斯用于通过接受写在一个高级语言代码,并将其转换成二进制针对特定处理器已在此构建PHP构建。 这些工具使编程语言更易被人阅读,从而可以用比汇编所需的代码少的代码来完成更复杂的任务,并且可以为具有不同指令集的多个处理器编译同一代码。

PHP itself is somewhat different: it’s an interpreted language rather than a compiled language like C. Instead of your program being compiled, the PHP interpreter is compiled for your server’s particular processor and that interpreter executes your program. This layer of abstraction has the advantage of letting you run your PHP code without compiling it. This same ability was touted as an advantage of Java, which appeared a year later in 1995, with the phrase “write once, run anywhere.”

PHP本身有点不同:它是一种解释语言,而不是像C这样的编译语言。PHP解释器不是针对您的服务器的特定处理器进行编译,而是针对您的服务器的特定处理器进行编译,并由该解释器执行您的程序。 这一抽象层的优点是,您无需编译即可运行PHP代码。 这种功能被吹捧为Java的优势, Java于一年后的1995年出现,用语“一次编写,随处运行”。

Interpreted languages don’t come without a cost, though. Compiled programs run faster because they exist as instructions that the processor can understand directly. Interpreters must essentially implement the function of the processor hardware as software. For example, the PHP interpreter converts PHP code into opcodes, its equivalent of assembly, and executes them. Interpreters are often called virtual machines for this reason; like the (system) virtual machines you use to run a guest operating system inside of a host operating system, they’re an abstraction within an abstraction that offers convenience in exchange for performance. In the case of PHP, the Zend Engine (1.0 in PHP 4, 2.0 in PHP 5) is its virtual machine. This is where our story begins.

但是,口译语言并非没有代价。 编译程序的运行速度更快,因为它们作为处理器可以直接理解的指令存在。 口译员必须实质上将处理器硬件的功能实现为软件。 例如,PHP解释器将PHP代码转换为opcodes (相当于程序集)并执行它们。 因此,口译员通常被称为虚拟机 。 就像您用来在主机操作系统中运行来宾操作系统的(系统)虚拟机一样,它们是抽象中的抽象,为交换性能提供了便利。 对于PHP,Zend Engine(PHP 4中为1.0,PHP 5中为2.0)是其虚拟机。 这是我们故事的起点。

会编译吗? (Will It Compile?)

A man by the name of Paul Biggar decided to explore this question with PHP. He joined the phc project in 2006 with the goal of being able to translate PHP to equivalent C code which could then be compiled down to native code, among other things. Its development continues today, though releases aren’t very frequent.

一个叫Paul Biggar的人决定用PHP探索这个问题。 他于2006年加入phc项目 ,目标是能够将PHP转换为等效的C代码,然后再将其编译为本机代码。 今天,它的开发仍在继续,尽管发行不是很频繁。

Not long after Paul began his efforts, in early 2007, Roadsend began a project with similar aims under the name pcc, which was later changed to rphp or Roadsend PHP. However, at the time this article was written, the project hasn’t seen a commit in roughly a year. Its support forums still see the occasional post, but most are indicate that working with Roadsend is rather troublesome. The project’s IRC channel also appears inactive.

保罗开始努力后不久,在2007年初Roadsend开始了一个以pcc为目标的类似目标的项目, 后来该项目改为rphp或Roadsend PHP。 但是,在撰写本文时,该项目在大约一年内尚未实现承诺 。 它的支持论坛仍然偶尔会看到帖子,但是大多数都表明与Roadsend合作非常麻烦。 该项目的IRC频道也显示为无效。

Other projects exist that port some form of PHP to a similar platform: Phalanger runs it on the .NET CLR and Quercus and Project Zero run it on the Java JVM. These projects remain at least somewhat active. Unlike phc and rphp, though, they compile source code to bytecode that requires a runtime library to be executed, rather than to native code the processor can execute directly.

还存在其他将某种形式PHP移植到类似平台的项目: Phalanger.NET CLRQuercus上运行它,而Zero ProjectJava JVM上运行它。 这些项目至少保持活跃。 但是,与phc和rphp不同,它们将源代码编译为需要执行运行时库的字节码,而不是处理器可以直接执行的本机代码。

增强PHP性能 (Enhancing PHP Performance)

In early 2008, the original traits RFC paved the way for the RFC standard and process that would later be adopted by the PHP internals team. Two years later, Dmitry Stogov and Stanislav “Stas” Malyshev, two members of the PHP internals team, filed a proposal outlining potential performance improvements of up to 20% in the Zend Engine which was later implemented. That same year, PHP 5.3 was released and included a switch from the flex lexer to re2c which also resulted in a slight performance improvement. It was clear that the performance of PHP had become more than just an academic concern.

在2008年初, RFC原始特征为RFC标准和流程铺平了道路,后来该标准和流程被PHP内部团队采用。 两年后,PHP内部团队的两名成员Dmitry Stogov和Stanislav“ Stas” Malyshev 提交了一份提案,概述了Zend Engine最多可将性能提高20%的特性,后来又实现了该性能。 同年,PHP 5.3发布,其中包括从flex lexerre2c的切换,这也导致性能略有提高。 显然,PHP的性能已不仅仅是学术问题。

输入HipHop (Enter HipHop)

Facebook announced their initiative in mid-February 2010 to improve their overall responsiveness twofold. About that same time they announced the release of HipHop for PHP, a project that has goals similar to phc and rphp. HipHop transforms PHP code into C++ code that is then compiled by the g++ compiler. This followed numerous performance-enhancing contributions to the APC extension and the Zend Engine, and HipHop seemed the logical next step for Facebook to continue their use of PHP and maintain their substantial existing PHP codebase.

Facebook在2010年2月中旬宣布了他们的倡议 ,以将其整体响应速度提高两倍。 大约在同一时间,他们宣布发布了适用于PHP的HipHop,该项目的目标类似于phc和rphp。 HipHop将PHP代码转换为C ++代码,然后由g ++编译器进行编译。 这是对APC扩展和Zend Engine进行了许多提高性能的贡献之后,HipHop似乎是Facebook继续使用PHP并维护其大量现有PHP代码库的合理下一步。

Implementing HipHop required sacrificing some of PHP’s more dynamic features, like eval() support, as well as reimplementing PHP’s runtime and rewriting several of its extensions. However, it removed the need for developers to use C or C++ and deal with the tedium that comes with such languages to write compiled code. Indeed, Facebook claims CPU usage decreased by 50% after deploying HipHop to their servers.

实现HipHop需要牺牲一些PHP的更多动态功能,例如eval()支持,以及重新实现PHP的运行时并重写其一些扩展。 但是,它消除了开发人员使用C或C ++并处理此类语言附带的繁琐代码来编写已编译代码的需求。 实际上,Facebook声称在将HipHop部署到服务器后,CPU使用率降低了50%。

The project spurred a number of opinions from members the PHP community upon its initial release including Sebastian Bergmann, lead developer of PHPUnit; David Coallier, PEAR President and CTO of echolibre; and Brandon Savage, a developer at Mozilla. Overall, its reception was positive, and development on the project continued. Facebook worked with several popular projects in the PHP community, including Drupal and WordPress, to help them remove code using dynamic features not supported by HipHop and to fix bugs found while attempting to deploy those projects on HipHop. They’ve also continued to improve performance, even to the extent of implementing more efficient memory allocation.

该项目在PHP最初发布时引起了PHP社区的许多意见,包括PHPUnit的首席开发人员Sebastian Bergmann 。 PEAR总裁兼echolibre首席技术官David Coallier ; 和Mozilla的开发人员Brandon Savage 。 总体而言,它受到了好评,并且该项目的开发仍在继续。 Facebook PHP社区中的多个热门项目 (包括Drupal和WordPress)合作,以帮助他们使用HipHop不支持的动态功能删除代码,并修复尝试在HipHop上部署这些项目时发现的错误。 他们也一直致力于提高性能 ,甚至达到实施更有效的内存分配的程度。

新的虚拟机 (The New Virtual Machine)

Recently, Facebook released the HipHop Virtual Machine, or hhvm, which received coverage from several sources like Ars Technica and ReadWriteWeb. hhvm blurs the lines between compiler and virtual machine like the Zend Engine.

最近,Facebook发布了HipHop虚拟机 (hhvm),该技术Ars TechnicaReadWriteWeb等多个来源获得报道。 hhvm模糊了编译器和虚拟机(例如Zend Engine)之间的界线。

hhvm is what’s called a Just-In-Time (or JIT) compiler. Rather than going through the C++ compilation phase to translate PHP to native code, hhvm translates PHP to an intermediary bytecode language called HipHop Byte Code (HHBC) that can be translated to native code in real time when it’s needed.

hhvm是所谓的即时 (JIT)编译器。 hhvm无需经过C ++编译阶段将PHP转换为本地代码,而是将PHP转换为称为HipHop字节码(HHBC)的中间字节代码语言,该语言可以在需要时实时转换为本地代码。

hhvm is intended to be a replacement for deploying statically compiled HipHop-based binaries to production. Upon its initial release, hhvm provides an overall relative performance increase of 60%.

hhvm旨在替代将静态编译的基于HipHop的二进制文件部署到生产中。 在最初发行时,hhvm的整体相对性能提高了60%。

结语 (Epilogue)

It’s users and members of the community who drive the innovative development of PHP and its architecture. Facebook is just one of many companies who have contributed to the ongoing success of PHP. Not only are they contributing changes back to the original projects, they’ve offered their own work on PHP to the community and work with other PHP projects to enable them to take advantage of projects like HipHop.

推动PHP及其体系结构创新发展的是用户和社区成员。 Facebook只是为PHP的持续成功做出贡献的众多公司之一。 他们不仅将更改返回到原始项目中,而且还向社区提供了自己PHP工作,并与其他PHP项目一起工作,以使他们能够利用HipHop等项目的优势。

Indeed, PHP has come a long way from its humble beginnings in 1995. While HipHop may not be suitable for all projects or all servers, it can provide performance enhancements to large-scale projects and help them substantially mitigate their operating hardware requirements. It’s definitely a project to keep on your radar moving forward, and is a testament to the vibrancy of the PHP ecosystem.

的确,PHP从1995年的不起眼起步已经走了很长一段路。尽管HipHop可能不适合所有项目或所有服务器,但它可以为大型项目提供性能增强,并帮助他们大大减轻对操作硬件的需求。 这绝对是一个使您不断前进的项目,并且证明了PHP生态系统的活力。

Image via Iscatel / Shutterstock

图片来自Iscatel / Shutterstock

翻译自: https://www.sitepoint.com/phps-quest-for-performance/

php hhvm

 类似资料: