Home Backend Development PHP Tutorial 《PHP核心技术与实践》PHP内核中的HashTable分析

《PHP核心技术与实践》PHP内核中的HashTable分析

Jun 23, 2016 pm 01:15 PM

  1. HashTable是PHP的灵魂,因为在Zend引擎中大量地使用了HastTable,如变量表,常量表,函数表,数组等,所以了解HashTable对真正了解PHP很重要!
  2. PHP内核中HashTable的数据结构: PHP的HashTable同时维护一个双向链表,而这个双向链表是通过pListNext和pListLast这两个成员变量维护的。 成员变量中,pData和pDataStr较容易混淆,pData指向的是想要保存的内存块地址,一般是通过malloc之类的系统调用分配出来,但是有时候只想保存一个指针,如果这样也去调用malloc之类的系统调用分配内存会导致产生内存碎片,这种情况PHP内核是不能容忍的,所以出现了pDataPtr指针。pDataPtr的指针就是当想保存一个指针类型的数据时,就直接保存到pDataPtr成员变量中,而不调用malloc分配内存,从而防止了内存碎片的产生,然后pData直接指向pDataPtr,当要保存的数据不是指针时,pDataPtr被设置为NULL.

要找到某个保存在HashTable中的数据,则是通过索引(key),每个元素都有一个索引且不同,通过索引可以找到这个元素且可取得保存在里面的数据。索引保存在HashTable的最后一个成员arKey起始位置的长nKeyLength的内存中,虽然arKey只有一个字节,但这里使用了C语言的一个常用技巧(flesible array),通过申请sizeof(Bucket)+nKeyLength大小的内存,然后把索引保存到arKey成员中。

但如果索引为整数,本来也可以把整数当成字符串处理,但PHP不这样做,而是利用一个技巧来解决。当索引为整数时,PHP把索引保存到Bucket结构体的h成员变量中,然后把nKeyLength设置为0,表示这是一个整数而不是字符串,所以当nKeyLength大于0时,可在arKey中取到索引,而当nKeyLength等于0时,就在h中取得索引,那当nKeyLength为0时h成员变量是不是没有用呢?其实不然,当nKeyLength大于0时(索引为字符串时),h成员保存的是索引经过hash函数处理之后的值,这样做的好处就是,在重hash时不用重新计算索引的hash值。(hash函数得到的是一个整形数,所以可以保存在ulong h;中,可用来做下标)

  1. HashTable结构体分析,几图示:

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles