Zend to PHP all the variables are used in the structure of the way to save, and string preservation and array of Save is also different, arrays using a hash table way to save (you know that the address of the hash save effective reduce conflict-hash the concept of the hash list you understand), The structure in PHP behaves as follows:
Copy CodeThe code is as follows:
File 1:zend/zend.h
/*
* Zval
*/
typedef struct _ZVAL_STRUCT Zval;
...
typedef Union _ZVALUE_VALUE {
Long lval; /* Long value */
Double Dval; /* Double Value */
struct {
Char *val;
int Len;
} str;
HashTable *ht; /* Hash Table value */
Zend_object_value obj;
} Zvalue_value;
struct _zval_struct {
/* Variable Information */
Zvalue_value value; /* Value */
Zend_uint refcount__gc;
Zend_uchar type; /* Active type */
Zend_uchar is_ref__gc;
};
The structure of the hash table is as follows
File 2:zend/zend_hash.h
typedef struct _HASHTABLE {
UINT Ntablesize;
UINT Ntablemask;
UINT Nnumofelements;
ULONG Nnextfreeelement;
Bucket *pinternalpointer; /* Used for element traversal */
Bucket *plisthead;
Bucket *plisttail;
Bucket **arbuckets;
dtor_func_t Pdestructor;
Zend_bool persistent;
unsigned char napplycount;
Zend_bool bapplyprotection;
#if Zend_debug
int inconsistent;
#endif
}
HashTable;
The general variable (string) in the use of strlen to obtain the length, in fact, is the zvalue_value.str of the structure of the Len attribute, the efficiency of O (1) times, in particular, the point is: strlen in PHP does not have a core implementation, Instead, the macro definitions in Zend are used to obtain:
Copy CodeThe code is as follows:
File 3:zend/zend_operators.php
#define Z_strlen (Zval) (zval). Value.str.len
...
#define Z_STRLEN_P (zval_p) Z_strlen (*zval_p)
...
#define Z_STRLEN_PP (ZVAL_PP) Z_strlen (**ZVAL_PP)
For the count operation of the array, there are actually two results, and the second parameter, mode "http://www.php.net/manual/en/function.count.php", is also mentioned in the Count API, which indicates that the mode parameter Whether it is necessary to re-count, and its re-statistics will be traversed once the array, efficiency is O (n) [n: Length], by default is not re-statistics, then this time will be directly output hashtable in the nnumofelements, the efficiency is also O (1) Times: The count code is as follows:
Copy CodeThe code is as follows:
File 4:ext/standard/array.c
Php_function (count)
{
Zval *array;
Long mode = Count_normal;
if (Zend_parse_parameters (Zend_num_args () tsrmls_cc, "Z|l", &array, &mode) = = FAILURE) {
Return
}
Switch (z_type_p (array)) {
Case Is_null:
Return_long (0);
Break
Case Is_array:
Return_long (php_count_recursive (array, mode TSRMLS_CC));
Break
.....
The realization of php_count_recursive
static int php_count_recursive (Zval *array, long mode TSRMLS_DC)/* {{{* * *
{
Long cnt = 0;
Zval **element;
if (z_type_p (array) = = Is_array) {
Error handling
if (z_arrval_p (array)->napplycount > 1) {
Php_error_docref (NULL tsrmls_cc, e_warning, "recursion detected");
return 0;
}
Direct access to length via zend_hash_num_elements
CNT = zend_hash_num_elements (z_arrval_p (array));
If you specify a need to re-count, you will enter a cyclic statistic
if (mode = = count_recursive) {
Hashposition POS;
For (ZEND_HASH_INTERNAL_POINTER_RESET_EX (z_arrval_p (array), &pos);
ZEND_HASH_GET_CURRENT_DATA_EX (z_arrval_p (array), (void * *) &element, &pos) = = SUCCESS;
ZEND_HASH_MOVE_FORWARD_EX (z_arrval_p (array), &pos)
) {
Z_arrval_p (Array)->napplycount++;
CNT + = Php_count_recursive (*element, count_recursive tsrmls_cc);
Z_arrval_p (Array)->napplycount--;
}
}
}
return CNT;
}
File 5:zend/zend_hash.c
The realization of zend_hash_num_elements
ZEND_API int zend_hash_num_elements (const HashTable *HT)
{
Is_consistent (HT);
Return ht->nnumofelements;
}
http://www.bkjia.com/PHPjc/324079.html www.bkjia.com true http://www.bkjia.com/PHPjc/324079.html techarticle Zend to PHP all the variables are used in the structure of the way to save, and the string of the preservation of the array is also different, the use of the hash table is the way to save (you know the hash save ... )