Table of Contents
什么是NOP
PHP中的空操作opcode NOP: ZEND_NOP
优化
Home php教程 php手册 PHP中的NOP及为什么有这个opcode

PHP中的NOP及为什么有这个opcode

Jun 06, 2016 pm 08:07 PM
php Why What this

什么是NOP NOP 是一个特殊的opcode,表示空操作,在很多地方存在,汇编中的NOP含义也一样,机器指令中的空操作通常用来将内存地址进行对齐,以提高CPU访问内存的效率,GCC等编译器也会将特定的语句进行优化而产生空操作。 PHP中的空操作opcode NOP: ZEND_NOP

什么是NOP

NOP 是一个特殊的opcode,表示空操作,在很多地方存在,汇编中的NOP含义也一样, 机器指令中的空操作通常用来将内存地址进行对齐,以提高CPU访问内存的效率, GCC等编译器也会将特定的语句进行优化而产生空操作。

PHP中的空操作opcode NOP: ZEND_NOP

PHP基于Zend虚拟机,其他基于虚拟机的语言中大都会有类似NOP的指令, PHP文档有对此的简单说明:

<?php /*
 * no operation
 * opcode number: 0
 */
function A(){};
?>
/* VLD 的输出结果 */
line  #   op  fetch   ext return  operands
6 0   NOP              
7 1   RETURN              1
Function name: A
Compiled variables: none
line  #   op  fetch   ext return  operands
6 0   RETURN              null
Copy after login

上面的VLD结果可以看出,函数A()的声明编译后变成了NOP操作。

Zend虚拟机是高级抽象,不要考虑内存对齐等的问题,为什么还需要空操作这样的opcode呢?

原因简单讲就是:编译过程优化的结果。有的内容由于可以在编译时就可确定(称为提早绑定Early Binding), 那么一部分opcode可以在编译时替换成空操作。

我们来看看这段代码的编译结果:

<?php if(true) {
  class Foo {}
}
new Bar();
class Bar {}
filename:       /Users/reeze/Opensource/php-test/php-src-5.4/test.php
function name:  (null)
number of ops:  8
compiled vars:  none
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   2     0  > > JMPZ                                                     true, ->3
   3     1  >   ZEND_DECLARE_CLASS                               $0      '%00foo%2FUsers%2Freeze%2FOpensource%2Fphp-test%2Fphp-src-5.4%2Ftest.php0x106cd601f', 'foo'
   4     2    > JMP                                                      ->3
   6     3  >   ZEND_FETCH_CLASS                              4  :1      'Bar'
       4      NEW                                                      :1
       5      DO_FCALL_BY_NAME                              0
   7     6      NOP
   9     7    > RETURN
Copy after login

和前面官方函数定义代码一样,上面VLD输出的第7行看到类Bar的opcode变成了NOP,不过请留意第3行, 这一行类Foo定义的opcode是ZEND_DECLARE_CLASS,也就是类的声明。

为什么同样是类的声明,第一个类声明的OPCODE和第二个的不一样呢?

上例中的代码,在Bar类声明之前是可以执行的new Bar,但是此时Bar类的声明并没有执行到, 那为什么可以访问到Bar类呢,这是因为Bar类的声明在编译时就已经完成了, 因为Bar类已经在编译时声明好了,所以在真正执行的时候就不需要再次执行声明类的操作了, 所以它所对应的opcode被替换成NOP了。

而Foo这个类由于处在条件判断块之中,编译期无法确定Foo类是否一定会被执行,所以还是需要在 执行时来声明这个类,所以opcode没有改变。

对于使用了opcode缓存的代码来说,把函数和类的声明移到了编译时,也就减少了执行时的opcode执行, 这能加快代码的执行。

优化

你可能会想,既然类及函数的声明可以优化掉,为什么不能直接丢弃这个opcode呢? 一能减少opcode占用的内容,比如很多的框架中有大量的类定义,也能减少执行时间, 因为空操作并不是0成本的,执行NOP的时候还是需要消耗CPU的。

先看看opcode编译过程的一个重要函数:

zend_op *get_next_op(zend_op_array *op_array TSRMLS_DC)
{
  zend_uint next_op_num = op_array->last++;
  zend_op *next_op;
  if (next_op_num >= CG(context).opcodes_size) {
      if (op_array->fn_flags & ZEND_ACC_INTERACTIVE) {
          /* we messed up */
          zend_printf("Ran out of opcode space!\n"
                      "You should probably consider writing this huge script into a file!\n");
          zend_bailout();
      }
      CG(context).opcodes_size *= 4;
      op_array_alloc_ops(op_array, CG(context).opcodes_size);
  }
  next_op = &(op_array->opcodes[next_op_num]);
  init_op(next_op TSRMLS_CC);
  return next_op;
}
Copy after login

这个函数每次会返回一个zend_op(也就是opcode一个最小单位),opcode的存储空间申请和哈希表类似, 通过预先申请空间的方式,如果空间不足则适当扩容。在编译时,opcode是以文件为单位的,而通常 在一个文件中函数或类声明的个数是不会太多的。而在编译时opcode数组已经是预先申请好的,所以 及时优化掉这个opcode,而实际在编译时的内存占用也不会有任何的优化。

目前只有少数几处使用了ZEND_NOP这个opcode。读者可以参考Zend/zend_compile.c: zend_do_early_binding(), 这个函数进行就是在确定在编译时能确定的函数以及类声明,在完成函数或类的声明后将当前编译的opcode设置为ZEND_NOP。 因为后续在执行是并需要再次对该函数或类进行声明了。

NOTE 当然也并不是所有的全局类或者方法都会进行提早绑定,具体可以参考前面提到的Zend_compile.c文件的实现

在这个函数中其实可以将生成的ZEND_NOP优化掉的,比如eAccelarator扩展中就对opcode进行了优化,将ZEND_NOP从 opcode_array数组中移除了,因为使用了opcode cache扩展优化只进行一次,而执行对多次执行, 这样的优化是值得的。目前Zend引擎并没有进行任何的优化,首先从代码上来看,类和函数声明的数量和其他指令的数量 之间差很多个等级,所以至少这个地方优化的收益是有限的,为了保证Zend引擎的简洁它没有进行优化。

目前APC扩展已经基本确定为将要进入PHP默认opcode缓存的官方扩展了,那么这些优化都可以在扩展中进行, 保证Zend引擎的简单易维护更为重要。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Centos stops maintenance 2024 Centos stops maintenance 2024 Apr 14, 2025 pm 08:39 PM

CentOS will be shut down in 2024 because its upstream distribution, RHEL 8, has been shut down. This shutdown will affect the CentOS 8 system, preventing it from continuing to receive updates. Users should plan for migration, and recommended options include CentOS Stream, AlmaLinux, and Rocky Linux to keep the system safe and stable.

See all articles