Reference detailed explanation of PHP7 kernel
Problem
Reference (REFERENCE) was a flag in PHP5, but after PHP7 we turned it into a New type: IS_REFERNCE. However, references are a very common application, so this change has brought a lot of changes, and it has also caused us to accidentally forget to handle this type when developing PHP7, which has caused many problems. There are a lot of bugs.
The simplest case is when dealing with various types. From now on, we have to consider this new type more. For example, in PHP7, this code form becomes very confusing. Common:
try_again:swtich (Z_TYPE_P(zv)) { case IS_TRING: break; case IS_ARRAY: break; ... case IS_REFERENCE: zv = Z_REFVAL_P(zv); // Dereference goto try_again; break ;}
If you write your own extensions and forget to consider this new type, it will cause problems.
Why?
So since this new type will bring so many problems, why did we use it to turn the reference into a type? Why not just use a flag?
In a word, it’s us I have to do this. -_
#As mentioned earlier, Hashtable directly stores zval, so in the symbol table, how can two zval share a value? It is okay for complex types such as strings, we seem to You can add a flag bit to the zend_refcounted structure to indicate that it is a reference to solve the problem. However, this will also encounter copying caused by Change On Write, but we know that in PHP7, some types are directly stored in zval, such as IS_LONG, But reference types require reference counting, so how to express a zval that is IS_LONG and IS_REFERNCE?
For this reason, we created this new type:As shown in the figure, Reference is a new type: zend_reference. For zval of IS_REFERNCE type, zval.value.ref is a pointer to zend_reference, which contains a reference count and a zval. The specific value of zval is stored in zval.value.ref- >val in .
So for the reference of IS_LONG, use a zval of type IS_REFERNCE, which points to a zend_reference, and this zend_reference->val is a zval of type IS_LONG.
Change On Write
PHP uses reference counting to do simple garbage collection. Consider the following code:
<?php1. $val = "laruence";2. $ref = &$val;3. $copy = $val;?>
$ref and $val point to the same For the reference of zval, in PHP5, we represented this situation through a reference count of 2 and a reference flag of 1. When copying $val to $copy(line 3), we found that $val is A reference with a count greater than 1, so a Change on write, that is, separation, is required. So we need to copy this zval.
In PHP7, the situation becomes much simpler. First, assign the reference to $ref(line 2), an IS_REFERNCE type is generated, and because there are two variables referencing it at this time, the reference count of the zend_reference structure zval.value.ref->gc.refcount is 2.
Then it is subsequently assigned to $ When copying (line 3), it is found that $val is a reference, so $copy points to zval.value.ref->val, which is the zval whose string value is laruence, and then counts the reference of zval by 1. That is, zval.value.ref->val.value.str.gc.refcount is 2. No copying occurs.
This effectively solves the classic problem of PHP5 mentioned in the previous chapter. , For example, when we run the problem in the previous chapter under PHP7, the result we get is:
$ php-7.0/sapi/cli/php /tmp/1.phpUsed 0.00021380008539Used 0.00020173048281
It can be seen that no copying occurs, so there will be no performance problems.
Recommended tutorial :《PHP》
The above is the detailed content of Reference detailed explanation of PHP7 kernel. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...
