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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Explain different error types in PHP (Notice, Warning, Fatal Error, Parse Error). Apr 08, 2025 am 12:03 AM

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

See all articles