> For the complete documentation index, see [llms.txt](https://php7.shujuwajue.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://php7.shujuwajue.com/php-70x-xin-te-xing/bu-5145-php7-di-ceng-xing-neng-you-hua.md).

# 补充\*PHP7底层性能优化

> 请参考视频资料：[PHP7 底层性能优化(一)](http://www.imooc.com/video/8532) [PHP7 底层性能优化(二)](http://www.imooc.com/video/8533)

## 目录：

* zval使用栈内存
* zend\_string存储hash值，array查询不在需要重复计算hash
* zend\_parse\_parameters改为宏实现，性能提升5%
* 新增加了4种OPCODE, call\_user\_function,is\_int/string/array,strlen,defined 4个函数变为PHP OpCode指令，速度更快
* 其他更多性能优化：
* 其他：PHP7.0-final版本不会携带JIT特征

## **zval使用栈内存**

在Zend引擎和扩展中，经常要创建一个PHP变量，底层就是一个zval指针。之前的版本都是通过MAKE\_STD\_ZVAL动态从堆上分配一个zval内存。而PHP7可以直接使用栈内存。

32字节-》16字节

这样做的好处：大量节省了内存分配和内存管理的工作，性能就会得到很大的提升。

#### PHP **5**

```c
zval *val; MAKE_STD_ZVAL(val);
```

#### PHP7

```c
zval val;
```

## **zend\_string存储hash值，array查询不在需要重复计算hash**

PHP7为字符串单独创建了新类型叫zend\_string，除了char \*指针和长度之外，增加了一个hash字段，用于保存字符串的hash值。数组键值查找不需要反复计算hash值。

```c
struct _zend_string {
    zend_refcounted gc;
    zend_ulong      h;
    size_t          len;
    char            val[1]
};
```

为了优化数组的键值查找性能，h即为存储hash值的字段。

**hashtable桶内直接存数据，减少了内存申请次数，提升了Cache命中率和内存访问速度**

PHP 7 新的**hashtable实现**：

![](/files/-LfnTJowlOH2wk--hfom)\
老：之前的php底层HashTable的实现:<http://www.cnblogs.com/mingaixin/p/4318805.html>

## ![](/files/-LfnTJoyW-dEPsSiPtXA)**zend\_parse\_parameters改为宏实现，性能提升5%**

zend\_parse\_parameters：是从php变量到C扩展函数之间交换一些参数，还有交换返回值，这时候要用zend\_parse\_parameters函数来实现。

### **新增加了4种OPCODE, call\_user\_function,is\_int/string/array,strlen,defined 4个函数变为PHP OpCode指令，速度更快**

### **其他更多性能优化：**

如基础类型为int，float，bool等改为直接进行值拷贝；排序算法改进，PCRE with JIT（正则表达式直接编译成机器码）, execute\_data和opline使用全局寄存器，使用gdb4.8的PGO功能（运行一段时间，它会导出一份运行时的数据统计）

## 其他：PHP7.0-final版本不会携带JIT特征

> 请参考视频资料： PHP7 与 JIT

JIT是just in time的缩写，表示运行时将指令转为二进制机器码。

**为什么PHP7中没有引入JIT ?** 原因：JIT对于实际项目，如WordPress没有太大的性能提升。

**但是：**&#x5BF9;于计算密集型程序，JIT可以将PHP的OpCode直接转换为机器码，大幅提升性能。\
PHP开发组已重启JIT开发计划，我看了一下官方，截止到PHP7.1版本没有带有JIT特征。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://php7.shujuwajue.com/php-70x-xin-te-xing/bu-5145-php7-di-ceng-xing-neng-you-hua.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
