php7 zval

php7的特点是规则上不从堆上分配内存,改为从栈上分配内存

由于从php代码上不好分析,只要从扩展上分析

<?php
$a=array();
$a[‘name‘]=‘taek-007‘;
?>

///////////////////////////////////////////////////////////////////////

PHP_FUNCTION(confirm_variable_compiled)
{
    zval val;
    array_init(&val);
    add_assoc_string(&val, "name", "taek-007");

    RETURN_ZVAL(&val,1,0);
}

为了能在gdb调试中打开宏,在编译php之前,修改configure该文件,增加红色的字样

 15533 if test "$PHP_DEBUG" = "yes"; then
  15534   PHP_DEBUG=1
  15535   ZEND_DEBUG=yes
  15536
  15537   CFLAGS=`echo "$CFLAGS" | $SED -e ‘s/-O[0-9s]*//g‘`
  15538   CXXFLAGS=`echo "$CXXFLAGS" | $SED -e ‘s/-O[0-9s]*//g‘`
  15539
  15540     if test "$GCC" = "yes" || test "$ICC" = "yes"; then
  15541     CFLAGS="$CFLAGS -gdwarf-2 -g3  -O0"

直接上gdb调试吧

gdb /usr/local/php/bin/php

(gdb)
(gdb) source /home/source/php-7.0.2/.gdbinit

(gdb) break /home/source/php-7.0.2/ext/variable/variable.c:66
No source file named /home/source/php-7.0.2/ext/variable/variable.c.
Make breakpoint pending on future shared library load? (y or [n]) y

Breakpoint 1 (/home/source/php-7.0.2/ext/variable/variable.c:66) pending.
(gdb)

(gdb) run /home/debug_php/variable.php
Starting program: /usr/local/php/bin/php /home/debug_php/variable.php
[Thread debugging using libthread_db enabled]
[New Thread 0xb7f4e6c0 (LWP 3761)]

Breakpoint 1, zif_confirm_variable_compiled (execute_data=0xb7c15090,
return_value=0xb7c15060) at /home/source/php-7.0.2/ext/variable/variable.c:67
67 array_init(&val);

(gdb) n
68 add_assoc_string(&val, "name", "taek-007");
(gdb) s
add_assoc_string_ex (arg=0xbf80cddc, key=0x493a7c "name", key_len=4,
str=0x493a73 "taek-007") at /home/source/php-7.0.2/Zend/zend_API.c:1383
1383 ZVAL_STRING(&tmp, str);
(gdb) macro expand ZVAL_STRING(&tmp, str)
expands to: do { const char *_s = (str); do { do { zval *__z = (&tmp); zend_string *__s = (zend_string_init(_s, strlen(_s), 0)); (*(__z)).value.str = __s; (*(__z)).u1.type_info = (6 | (( (1<<2) | (1<<4)) << 8)); } while (0); } while (0); } while (0)

(gdb) p (char*)str
$4 = 0x493a73 "taek-007"

(gdb) s
zend_string_init (str=0xe22a73 "taek-007", len=8, persistent=0)
at /home/source/php-7.0.2/Zend/zend_string.h:157
157 zend_string *ret = zend_string_alloc(len, persistent);

(gdb) n
159 memcpy(ZSTR_VAL(ret), str, len);

(gdb) p ret
$39 = (zend_string *) 0xb7c58900  //可以发现从堆上分配内存给了ret

(gdb) n
160 ZSTR_VAL(ret)[len] = ‘\0‘;

(gdb) p (char *)ret.val
$47 = 0xb7c58910 "taek-007"

.... 一直n

1384 ret = zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
(gdb) p tmp.value.str
$51 = (zend_string *) 0xb7c58900

(gdb) p tmp.value
$54 = {lval = -1211791104, dval = 1.8828939190657202e-307, counted = 0xb7c58900,
str = 0xb7c58900, arr = 0xb7c58900, obj = 0xb7c58900, res = 0xb7c58900,
ref = 0xb7c58900, ast = 0xb7c58900, zv = 0xb7c58900, ptr = 0xb7c58900,
ce = 0xb7c58900, func = 0xb7c58900, ww = {w1 = 3083176192, w2 = 4254880}} //由于value本身是个结构体,故其中每个元素的地址都是0xb7c58900

(gdb) s  //进入函数zend_symtable_str_update
zend_symtable_str_update (ht=0xb7c59300, str=0xe22a7c "name", len=4,
pData=0xbfd1d0c4) at /home/source/php-7.0.2/Zend/zend_hash.h:392
392 if (ZEND_HANDLE_NUMERIC_STR(str, len, idx)) {
(gdb)

(gdb) n
395 return zend_hash_str_update(ht, str, len, pData);
(gdb) s
_zend_hash_str_update (ht=0xb7c59300, str=0xe22a7c "name", len=4, pData=0xbfd1d0c4,
__zend_filename=0x895c438 "/home/source/php-7.0.2/Zend/zend_hash.h",
__zend_lineno=395) at /home/source/php-7.0.2/Zend/zend_hash.c:653
653 zend_string *key = zend_string_init(str, len, ht->u.flags & HASH_FLAG_PERSISTENT);

(gdb) n
654 zval *ret = _zend_hash_add_or_update_i(ht, key, pData, HASH_UPDATE ZEND_FILE_LINE_RELAY_CC);

(gdb) p (char*)key.val
$56 = 0xb7c58940 "name"

(gdb) p (char *)pData.value.str.val
$58 = 0xb7c58910 "taek-007"

(gdb) p pData.value.str
$60 = (zend_string *) 0xb7c58900

(gdb) s
_zend_hash_add_or_update_i (ht=0xb7c59300, key=0xb7c58930, pData=0xbfd1d0c4, flag=1,
__zend_filename=0x895c438 "/home/source/php-7.0.2/Zend/zend_hash.h",
__zend_lineno=395) at /home/source/php-7.0.2/Zend/zend_hash.c:559
559 IS_CONSISTENT(ht);
(gdb) n
562 if (UNEXPECTED(!(ht->u.flags & HASH_FLAG_INITIALIZED))) {
(gdb)
563 CHECK_INIT(ht, 0);
(gdb) macro expand CHECK_INIT(ht, 0)
expands to: zend_hash_check_init(ht, 0)
(gdb) s
zend_hash_check_init (ht=0xb7c59300, packed=0)
at /home/source/php-7.0.2/Zend/zend_hash.c:162
162 if (UNEXPECTED(!((ht)->u.flags & HASH_FLAG_INITIALIZED))) {
(gdb) n
163 zend_hash_real_init_ex(ht, packed);

(gdb) s
zend_hash_check_init (ht=0xb7c59300, packed=0)
at /home/source/php-7.0.2/Zend/zend_hash.c:162
162 if (UNEXPECTED(!((ht)->u.flags & HASH_FLAG_INITIALIZED))) {
(gdb) n
163 zend_hash_real_init_ex(ht, packed);
(gdb) s
zend_hash_real_init_ex (ht=0xb7c59300, packed=0)
at /home/source/php-7.0.2/Zend/zend_hash.c:133
133 ZEND_ASSERT(!((ht)->u.flags & HASH_FLAG_INITIALIZED));
(gdb) n
134 if (packed) {
(gdb)
139 (ht)->nTableMask = -(ht)->nTableSize;
(gdb)
140 HT_SET_DATA_ADDR(ht, pemalloc(HT_SIZE(ht), (ht)->u.flags & HASH_FLAG_PERSISTENT));

(gdb) p packed
$61 = 0
(gdb) p ht->nTableSize
$62 = 8

(gdb) macro expand HT_SET_DATA_ADDR  //分配8个Bucket大小的内存,并将首地址赋给ht->arData
expands to: HT_SET_DATA_ADDR
(gdb) macro expand HT_SET_DATA_ADDR(ht, pemalloc(HT_SIZE(ht), (ht)->u.flags & HASH_FLAG_PERSISTENT));
expands to: do { (ht)->arData = (Bucket*)(((char*)((((ht)->u.flags & (1<<0))?__zend_malloc((((size_t)(((ht)->nTableSize)) * sizeof(Bucket)) + (((size_t)(uint32_t)-(int32_t)(((ht)->nTableMask))) * sizeof(uint32_t)))):_emalloc(((((size_t)(((ht)->nTableSize)) * sizeof(Bucket)) + (((size_t)(uint32_t)-(int32_t)(((ht)->nTableMask))) * sizeof(uint32_t)))) , __FILE__, __LINE__ , ((void *)0), 0)))) + (((size_t)(uint32_t)-(int32_t)((ht)->nTableMask)) * sizeof(uint32_t))); } while (0);

(gdb) n
141 (ht)->u.flags |= HASH_FLAG_INITIALIZED;
(gdb)
142 if (EXPECTED(ht->nTableMask == -8)) {
(gdb)
143 Bucket *arData = ht->arData;
(gdb)
145 HT_HASH_EX(arData, -8) = -1;
(gdb)
146 HT_HASH_EX(arData, -7) = -1;
(gdb)
147 HT_HASH_EX(arData, -6) = -1;
(gdb)
148 HT_HASH_EX(arData, -5) = -1;
(gdb)
149 HT_HASH_EX(arData, -4) = -1;
(gdb)
150 HT_HASH_EX(arData, -3) = -1;
(gdb)
151 HT_HASH_EX(arData, -2) = -1;
(gdb)
152 HT_HASH_EX(arData, -1) = -1;
(gdb)
157 }
(gdb)
zend_hash_check_init (ht=0xb7c59300, packed=0)
at /home/source/php-7.0.2/Zend/zend_hash.c:165
165 }
(gdb)
_zend_hash_add_or_update_i (ht=0xb7c59300, key=0xb7c58930, pData=0xbfd1d0c4, flag=1,
__zend_filename=0x895c438 "/home/source/php-7.0.2/Zend/zend_hash.h",
__zend_lineno=395) at /home/source/php-7.0.2/Zend/zend_hash.c:564
564 goto add_to_hash;
(gdb)
595 idx = ht->nNumUsed++;

(gdb) n
596 ht->nNumOfElements++;
(gdb) p ht->nNumUsed++
$64 = 2
(gdb) p idx
$65 = 1
(gdb) n
597 if (ht->nInternalPointer == HT_INVALID_IDX) {
(gdb)
598 ht->nInternalPointer = idx;
(gdb)
600 zend_hash_iterators_update(ht, HT_INVALID_IDX, idx);
(gdb)
601 p = ht->arData + idx;

$67 = (Bucket *) 0xb7c60620
(gdb) p ht->arData+1
$68 = (Bucket *) 0xb7c60638
(gdb) n
602 p->key = key;
(gdb) p p
$69 = (Bucket *) 0xb7c60638

(gdb) p idx
$70 = 1
(gdb) n
603 if (!ZSTR_IS_INTERNED(key)) {
(gdb)
604 zend_string_addref(key);
(gdb)
605 ht->u.flags &= ~HASH_FLAG_STATIC_KEYS;
(gdb)
606 zend_string_hash_val(key);
(gdb)
608 p->h = h = ZSTR_H(key);
(gdb)
609 ZVAL_COPY_VALUE(&p->val, pData); //(*(_z1)).value.counted = _gc; 将堆上的内存地址分配到了hashTable中的Bucket中的val中的value.str
(gdb) macro expand ZVAL_COPY_VALUE(&p->val, pData)
expands to: do { zval *_z1 = (&p->val); const zval *_z2 = (pData); zend_refcounted *_gc = (*(_z2)).value.counted; uint32_t _t = (*(_z2)).u1.type_info; do { uint32_t _w2 = _z2->value.ww.w2; (*(_z1)).value.counted = _gc; _z1->value.ww.w2 = _w2; (*(_z1)).u1.type_info = _t; } while (0); } while (0)
(gdb) p p->val
$71 = {value = {lval = 0, dval = 0, counted = 0x0, str = 0x0, arr = 0x0, obj = 0x0,
res = 0x0, ref = 0x0, ast = 0x0, zv = 0x0, ptr = 0x0, ce = 0x0, func = 0x0,
ww = {w1 = 0, w2 = 0}}, u1 = {v = {type = 0 ‘\0‘, type_flags = 0 ‘\0‘,
const_flags = 0 ‘\0‘, reserved = 0 ‘\0‘}, type_info = 0}, u2 = {var_flags = 0,
next = 0, cache_slot = 0, lineno = 0, num_args = 0, fe_pos = 0, fe_iter_idx = 0}}
(gdb) p pData.value
$72 = {lval = -1211791104, dval = 1.8828939190657202e-307, counted = 0xb7c58900,
str = 0xb7c58900, arr = 0xb7c58900, obj = 0xb7c58900, res = 0xb7c58900,
ref = 0xb7c58900, ast = 0xb7c58900, zv = 0xb7c58900, ptr = 0xb7c58900,
ce = 0xb7c58900, func = 0xb7c58900, ww = {w1 = 3083176192, w2 = 4254880}}
(gdb) p pData.value.counted
$73 = (zend_refcounted *) 0xb7c58900
(gdb) p (*(_z1)).value.counted
Cannot access memory at address 0x30
(gdb) p p->val.value.counted
$74 = (zend_refcounted *) 0x0
(gdb) p pData.u.type_info
There is no member named u.
(gdb) p pData.u1.type_info
$75 = 5126
(gdb) p p->val.value.u1.type_info
There is no member named u1.
(gdb) p p->val.u1.type_info
$76 = 0
(gdb) n
610 nIndex = h | ht->nTableMask;
(gdb) p p->val.value.counted
$77 = (zend_refcounted *) 0xb7c58900
(gdb) p p->val.value.u1.type_info
There is no member named u1.
(gdb) p p->val.u1.type_info
$78 = 5126
(gdb)

(gdb) n
610 nIndex = h | ht->nTableMask;
(gdb) p p->val.value.counted
$77 = (zend_refcounted *) 0xb7c58900
(gdb) p p->val.value.u1.type_info
There is no member named u1.
(gdb) p p->val.u1.type_info
$78 = 5126
(gdb) n
611 Z_NEXT(p->val) = HT_HASH(ht, nIndex);
(gdb) p HT_HASH(ht, nIndex);
Invalid character ‘;‘ in expression.
(gdb) n
612 HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
(gdb) p HT_HASH(ht, nIndex)
$79 = 4294967295
(gdb) n
615 return &p->val;
(gdb) p HT_HASH(ht, nIndex)
$80 = 24
(gdb) p HT_IDX_TO_HASH(idx)
$81 = 24
(gdb) p idx
$82 = 1
(gdb) p HT_HASH(ht, nIndex)
$83 = 24
(gdb) macro expand HT_HASH(ht, nIndex)
expands to: ((uint32_t*)((ht)->arData))[(int32_t)(nIndex)]
(gdb) macro expand HT_IDX_TO_HASH(idx)
expands to: ((idx) * sizeof(Bucket))
(gdb) p sizeof(Bucket)
$84 = 24
(gdb) p nIndex
$85 = 4294967294
(gdb) p h
$86 = 4238019654
(gdb) p ht->nTableMask
$87 = 4294967288
(gdb) l
610 nIndex = h | ht->nTableMask;
611 Z_NEXT(p->val) = HT_HASH(ht, nIndex);
612 HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
613 HANDLE_UNBLOCK_INTERRUPTIONS();
614
615 return &p->val;
616 }

时间: 2024-10-20 17:36:57

php7 zval的相关文章

PHP7做了哪些优化

一  zval使用栈内存 在Zend引擎和扩展中,经常要创建一个PHP的变量,底层就是一个zval指针.之前的版本都是通过MAKE_STD_ZVAL动态的从堆上分配一个zval内存.而PHP7可以直接使用栈内存.PHP代码中创建的变量也进行了优化,PHP7直接在栈内存上预分配zval.这样节约了大量内存分配和内存管理的操作. PHP5 zval *val; MAKE_STD_ZVAL(val); PHP7 zval val; 二 zend_string存储hash值,array查询不再需要重复计

php7新特新

1.变量类型 php7版本函数的参数和返回值增加了类型限定.为什么php要加入类型限定,实际上此项特性是为了php7.1版本的JIT特性做准备,增加类型后php JIT可以准确判断变量类型,生成最佳的机器指令. function test(int $a, string $b, array $c) : int { } 2.错误异常 php程序出错hou过去Zend引擎会发生致命错误并终止程序运行,php7可以使用try/catch捕获错误. try { non_exists_func(); } c

深入理解 PHP7 中全新的 zval 容器和引用计数机制

深入理解 PHP7 中全新的 zval 容器和引用计数机制 最近在查阅 PHP7 垃圾回收的资料的时候,网上的一些代码示例在本地环境下运行时出现了不同的结果,使我一度非常迷惑. 仔细一想不难发现问题所在:这些文章大多是 PHP5.x 时代的,而 PHP7 发布后,采用了新的 zval 结构,相关的资料也比较贫瘠,所以我结合一些资料做了一个总结, 主要侧重于解释新 zval 容器中的引用计数机制 ,如有谬误,还望不吝指教. PHP7 中新的 zval 结构 明人不说暗话,先看代码! struct 

深入理解PHP7之zval

PHP7已经发布, 如承诺, 我也要开始这个系列的文章的编写, 今天我想先和大家聊聊zval的变化. 在讲zval变化的之前我们先来看看zval在PHP5下面是什么样子 PHP5zval回顾在PHP5的时候, zval的定义如下: struct _zval_struct { union { long lval; double dval; struct { char *val; int len; } str; HashTable *ht; zend_object_value obj; zend_a

PHP7新特性的介绍

关于PHP 20年的发展历史: 迄今为止最流行的WEB开发语言: 超过82%的网站都会使用PHP作为他们的服务端开发语言: 新特性介绍 PHP NG – Zend Engine 3 抽象语法树 64位的 INT 支持 统一的变量语法 新增Closure::call() 一致性foreach循环 匿名类的支持 新增 <=>.**.?? .\u{xxxx}操作符 增加了返回类型的声明 增加了标量类型的声明 核心错误可以通过异常捕获 增加了上下文敏感的词法分析 PHP NG ZVAL 大小从24字节

php7新特性

1 php 7 性能 提升了 70%左右 2 固定变量类型,引入了错误处理 3 zval使用栈内存,php7可以直接使用栈内存. 4 zend_string存储hash值,array查询不再需要重复计算hash. php7 为字符串单独创建了新类型叫做zend_string,除了 char * 指针和长度之外,增加了一个hash字段,用于保存字符串的hash值,数组键值查找不需要反复计算hash值 5 hashtable 桶内之间存数据,减少了内存申请次数,提升了cache命中率和内存访问速度

php7扩展开发[9] zend_call_method和zend_call_function

场景:我们想要在php7扩展中调用用户自定的类中的方法,而且方法有多个参数,找到以下方 法,没有看到可以超过两个参数的方法.所以一直向下查找,发现zend_call_method调用的 zend_call_function,但是并非只能传两个参数.一直追下去.这样我们用zend_call_function来调用多个参数,实现我们要的目的. 函数原型:在 Zend/zend_interfaces.下: #define zend_call_method_with_0_params(obj, obj_

PHP7革新与性能优化

有幸参与2015年的PHP技术峰会(PHPCON),听了鸟哥(惠新宸)的关于PHP7的新特性和性能优化的分享,一切都令人感到激动.鸟哥是国内最权威的PHP专家,他的分享有很多非常有价值的东西,我通过整理分享的PPT和收集相关资料,整理为这篇解读性质的技术文章,希望能给做PHP开发的同学一些帮助. PHP已经走过了20年的历史,直到今天,PHP7都发布了RC版,据说,PHP7正式版应该会在2015年11月份左右发布.PHP7对于上一个系列的PHP5.*,可以说是一个大规模的革新,尤其是在性能方面实

PHP7扩展开发之字符串处理

本文和大家分享的主要是PHP7扩展开发中字符串的处理相关知识,希望通过本文的分享能帮助大家更好的学习php. 这次,我们来看看字符串在PHP扩展里面如何处理. 示例代码如下: <?phpfunction str_concat($prefix, $string) { $len = strlen($prefix); $substr = substr($string, 0, $len); if ($substr != $prefix) { return $prefix." ".$str