Home > Article > Backend Development > hashtable PHP source code analysis Zend HashTable detailed explanation page 1/3
HashTable is also called hash table or hash table in common data structure textbooks. The basic principle is relatively simple (if you are not familiar with it, please consult any data structure textbook or search online), but the implementation of PHP has its own unique features. Understanding the data storage structure of HashTable is very helpful for us when analyzing the source code of PHP, especially the implementation of the virtual machine in Zend Engine. It helps us simulate the image of a complete virtual machine in our brains. It is also the basis for the implementation of other data structures in PHP such as arrays.
The implementation of Zend HashTable combines the advantages of two data structures: doubly linked list and vector (array), providing PHP with a very efficient data storage and query mechanism.
Let's begin!
1. Data structure of HashTable
The implementation code of HashTable in Zend Engine mainly includes the two files zend_hash.h and zend_hash.c. Zend HashTable includes two main data structures, one is the Bucket structure and the other is the HashTable structure. The Bucket structure is a container used to save data, and the HashTable structure provides a mechanism to manage all these Buckets (or bucket columns).
Copy the code The code is as follows:
typedef struct bucket {
ulong h; /* Used for numeric indexing */
uint nKeyLength; /* key length*/
void *pData; /* Point to Bucket Pointer to saved data*/
void *pDataPtr; /* Pointer data*/
struct bucket *pListNext; /* Points to the next element in the HashTable bucket column*/
struct bucket *pListLast; /* Points to the previous element in the HashTable bucket column An element*/
struct bucket *pNext; /* Points to the next element of the bucket list with the same hash value*/
struct bucket *pLast; /* Points to the previous element of the bucket list with the same hash value*/
char arKey[1]; /* Must be the last member, key name*/
} Bucket;
Copy code The code is as follows:
typedef struct _hashtable {
uint nTableSize;
uint nTableMask;
uint nNumOfElements;
ulong nNextFreeElement;
Bucket *pInternalPointer;
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;
Current page 1/3 123Next page
The above introduces hashtable PHP source code analysis Zend HashTable detailed explanation page 1/3, including hashtable content, I hope it will be helpful to friends who are interested in PHP tutorials.