


PHP source code analysis Zend HashTable detailed explanation page 1/3_PHP tutorial
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 can help 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), and provides a very efficient data storage and query mechanism for PHP.
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).
typedef struct bucket {
ulong h; /* Used for numeric indexing */
uint nKeyLength; /* key length*/
void *pData; /* pointer to the data saved in the Bucket*/
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*/
struct bucket *pNext; /* Points to the same hash value The next element of the bucket column*/
struct bucket *pLast; /* Points to the previous element of the bucket column with the same hash value*/
char arKey[1]; /* Must be the last member , key name*/
} Bucket;
In Zend HashTable, each data element (Bucket) has a key name (key), which is unique in the entire HashTable and cannot repeat. The data elements in the HashTable can be uniquely determined based on the key name. There are two ways to represent key names. The first method uses the string arKey as the key name, and the length of the string is nKeyLength. Note that in the above data structure, although arKey is just a character array with a length of 1, it does not mean that the key can only be one character. In fact, Bucket is a variable-length structure. Since arKey is the last member variable of Bucket, a key with a length of nKeyLength can be determined by combining arKey with nKeyLength. This is a common technique in C language programming. Another key name representation method is the index method. In this case, nKeyLength is always 0, and the long integer field h represents the key name of the data element. To put it simply, if nKeyLength=0, the key name is h; otherwise, the key name is arKey, and the length of the key name is nKeyLength.
When nKeyLength > 0, it does not mean that the h value at this time is meaningless. In fact, what it saves at this time is the hash value corresponding to arKey. No matter how the hash function is designed, conflicts are inevitable, which means that different arKeys may have the same hash value. Buckets with the same hash value are stored in the bucket column corresponding to the same index in the arBuckets array of HashTable (refer to the explanation below). This bucket column is a doubly linked list, and its forward elements and backward elements are represented by pLast and pNext respectively. The newly inserted Bucket is placed at the front of the bucket column.
In Bucket, the actual data is stored in the memory block pointed to by the pData pointer. Usually this memory block is allocated separately by the system. But there is an exception, that is, when the data saved by the Bucket is a pointer, the HashTable will not request the system to allocate space to save the pointer, but directly save the pointer to pDataPtr, and then point pData to the member of this structure. address. This improves efficiency and reduces memory fragmentation. From this we can see the subtleties of PHP HashTable design. If the data in the Bucket is not a pointer, pDataPtr is NULL.
All Buckets in HashTable form a doubly linked list through pListNext and pListLast. The latest inserted Bucket is placed at the end of this doubly linked list.
Note that in general, Bucket cannot provide information about the size of the data it stores. Therefore, in the implementation of PHP, the data saved in the Bucket must have the ability to manage its own size.
typedef struct _hashtable {
uint nTableSize;
uint nTableMask;
uint nNumOfElements;
ulong nNextFreeElement;
Bucket *pInternalPointer;
Bucket *pListHead;
Bucket *pListTail; ;
unsigned char nApplyCount;
zend_bool bApplyProtection;
#if ZEND_DEBUG
int inconsistent;
#endif
} HashTable;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op
