This article will take you to analyze the zval of php7

藏色散人
Release: 2023-02-18 08:14:01
forward
2941 people have browsed it

Zval is one of the most important data structures in PHP. It contains information about the value and type of variables in PHP.

1. zval

1.1 The structure of zval (zend_types.h)

typedef struct _zval_struct zval;struct _zval_struct { zend_value value; /* value */ union { struct { ZEND_ENDIAN_LOHI_4( zend_uchar type, /* active type */ zend_uchar type_flags, zend_uchar const_flags, zend_uchar reserved) /* call info for EX(This) */ } v; uint32_t type_info; } u1; union { uint32_t var_flags; uint32_t next; /* hash collision chain */ uint32_t cache_slot; /* literal cache slot */ uint32_t lineno; /* line number (for ast nodes) */ uint32_t num_args; /* arguments number for EX(This) */ uint32_t fe_pos; /* foreach position */ uint32_t fe_iter_idx; /* foreach iterator index */ } u2;};
Copy after login

The zval structure is relatively simple and consists of three parts:

  • zend_value: Save the value or pointer of a specific variable type
  • The core function of u1 is to distinguish the type
  • u2 is an auxiliary value

In order to look at zval more intuitively The structure and core field values are as shown in the picture above.
This article will take you to analyze the zval of php7

Explanation of u1.v.type:

  1. IS_UNDEF: The mark is undefined, indicating that the data can be overwritten or deleted. For example, when performing an unset operation on an array element, PHP 7 will not directly delete the data from the memory allocated to the HashTable. Instead, it will first mark the location of the Bucket where the element is located as IS_UNDEF. When the number of IS_UNDEF elements in the HashTable is When a certain threshold is reached, the elements marked by IS_UNDEF will be overwritten or deleted during the rehash operation.
  2. IS_TRUE and IS_FALSE: Here IS_BOOL is optimized into two, and the Boolean type tag is directly recorded in type.
  3. IS_REFERENCE: It is a new type. PHP7 uses different processing methods to handle "&"
  4. IS_INDIRECT: It is also a new type. Since the design of HashTable in PHP 7 is different from that of PHP5 There is a big difference between them, so to solve the problem of the global symbol table accessing the CV variable table, the IS_INDRECT type was introduced.
  5. IS_PTR: This type is defined as a pointer type, used to parse value.ptr, usually used on function types. For example, declare a function or method.

1.2 zend_value

The definition of zend_value in zval is as follows:

typedef union _zend_value { zend_long lval; /* long value */ double dval; /* double value */ zend_refcounted *counted; zend_string *str; zend_array *arr; zend_object *obj; zend_resource *res; zend_reference *ref; zend_ast_ref *ast; zval *zv; void *ptr; zend_class_entry *ce; zend_function *func; struct { uint32_t w1; uint32_t w2; } ww; } zend_value;
Copy after login

1.3 zval memory usage

In a zval:

  • zend_value is union, occupying only 8 bytes. Just enough to store a zend_long, a double, or a pointer.
  • u1 is 4 bytes and stores a v or type_info
  • u2 is 4 bytes

This article will take you to analyze the zval of php7

So a zval is occupied 16 bytes. Correspondingly, in php5, the size of a zval is 48 bytes, which is indeed a huge improvement.

2. Variable storage

2.1 true, false, null

can be directly distinguished according to zval.u1.v.type without zend_value involvement

2.2 long, double

are stored directly in the lval or dval of zend_value.

2.3 Other types (string, array, object, resource, etc.)

Use the pointer corresponding to zend_value to point to its specific structure.

For example, the structure of the string type is

struct _zend_string { zend_refcounted_h gc; zend_ulong h; /* hash value */ size_t len; char val[1]; };
Copy after login

The memory organization of a string variable is as shown in the figure below, zval.value.str points to the zend_string structure.
This article will take you to analyze the zval of php7

Zval is one of the most important data structures in PHP, which contains the values of variables in PHP and type-related information.

Recommended study: "PHP7 Tutorial"

The above is the detailed content of This article will take you to analyze the zval of php7. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!