Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of zval in php7

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shares with you the content of the sample analysis of zval in php7. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Zval is one of the most important data structures in PHP, which contains information about the values and types of variables in PHP.

1. The structure of zval1.1 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;}

Zval has a relatively simple structure and consists of three parts:

Zend_value: holds a value or pointer to a specific variable type

The core function of U1 is to distinguish types.

U2 is the auxiliary value

In order to take a more intuitive look at the structure of the zval and the values of the core fields, we have the previous figure.

Description of u1.v.type:

IS_UNDEF: the tag is not defined, indicating that the data can be overwritten or deleted. For example, when unset an array element, PHP 7 will not delete the data directly from the memory allocated to HashTable, but will first mark the location of the Bucket where the element is located as IS_UNDEF, and when the number of IS_ Undef elements in the HashTable reaches a certain threshold, the elements marked by IS_UNDEF will be overwritten or deleted during the rehash operation.

IS_TRUE and IS_FALSE: here the IS_BOOL is optimized into two, recording Boolean tags directly in type.

IS_REFERENCE: a new type that uses different processing methods to handle "&" in PHP7.

IS_INDIRECT: it is also a new type. Because the design of HashTable in PHP 7 is very different from that in PHP5, the IS_INDRECT type is introduced to solve the problem of global symbol table accessing CV variable table.

IS_PTR: this type is defined as a pointer type and is used to parse value.ptr, usually on function types. Such as declaring 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;1.3 zval memory footprint

In a zval:

Zend_value is union and occupies only 8 bytes. It just happens to hold a zend_long or a double, or a pointer.

U1 is 4 bytes and stores a v or type_info

U2 is 4 bytes

So a zval takes up 16 bytes. In the corresponding php5, the size of a zval is 48 bytes, which is indeed a huge improvement.

two。 Variable storage 2.1 true, false, null

Can be distinguished directly according to zval.u1.v.type, without the participation of zend_value

2.2 long,double

It is stored directly in 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 a string type is

Struct _ zend_string {zend_refcounted_h gc; zend_ulong h; / * hash value * / size_t len; char val [1];}

The memory organization of a string variable is shown in the following figure, with zval.value.str pointing to the zend_string structure.

Thank you for reading! This is the end of this article on "sample Analysis of zval in php7". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report