The content of this article is about the calculation of small memory specifications in PHP (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
small memory allocation calculation bin_num
In the PHP source code, there is a calculation of small memory specifications, specifically in the zend_mm_small_size_to_bin function of Zend/zend_alloc.c, its purpose is to pass in a size , calculate the corresponding specifications. See the code:
if (size > 3; } else { t1 = size - 1; t2 = zend_mm_small_size_to_bit(t1) - 3; t1 = t1 >> t2; t2 = t2 - 3; t2 = t2 <p> It can be seen that this code is divided into two situations for discussion: </p>
-
1. The case where size is less than or equal to 64;
2. The case where size is greater than 64;
Let’s analyze these two situations in detail below.
For the case where size is less than or equal to 64
See
ZEND_MM_BINS_INFO
This macro knows that when size is less than or equal to 64, it is an arithmetic sequence, increasing 8, so just use size divided by 8 (right shift by 3 bits in the source code)size >> 3
But you must consider that size is equal to 8, 16 and so on, so
(size - 1) >> 3
Then we need to consider the situation of 0, so ## in the source code The processing of #-1
is
!!size. When size is 0,
!!0 = 0. So when size is 0,
-1is converted into
-0, and finally there is the expression
(size - !!size) >> in the source code ; 3
t1 = size - 1;
t2 = zend_mm_small_size_to_bit(t1) - 3;
t1 = t1 >> t2;
t2 = t2 - 3;
t2 = t2
Initial confusion
- When I first saw this code, It’s easy to get confused, what are these t1 and t2?
- But don’t be afraid, let’s analyze it step by step ##Step analysis
/* num, size, count, pages */ #define ZEND_MM_BINS_INFO(_, x, y) \ _( 0, 8, 512, 1, x, y) \ _( 1, 16, 256, 1, x, y) \ _( 2, 24, 170, 1, x, y) \ _( 3, 32, 128, 1, x, y) \ _( 4, 40, 102, 1, x, y) \ _( 5, 48, 85, 1, x, y) \ _( 6, 56, 73, 1, x, y) \ _( 7, 64, 64, 1, x, y) \ _( 8, 80, 51, 1, x, y) \ _( 9, 96, 42, 1, x, y) \ _(10, 112, 36, 1, x, y) \ _(11, 128, 32, 1, x, y) \ _(12, 160, 25, 1, x, y) \ _(13, 192, 21, 1, x, y) \ _(14, 224, 18, 1, x, y) \ _(15, 256, 16, 1, x, y) \ _(16, 320, 64, 5, x, y) \ _(17, 384, 32, 3, x, y) \ _(18, 448, 9, 1, x, y) \ _(19, 512, 8, 1, x, y) \ _(20, 640, 32, 5, x, y) \ _(21, 768, 16, 3, x, y) \ _(22, 896, 9, 2, x, y) \ _(23, 1024, 8, 2, x, y) \ _(24, 1280, 16, 5, x, y) \ _(25, 1536, 8, 3, x, y) \ _(26, 1792, 16, 7, x, y) \ _(27, 2048, 8, 4, x, y) \ _(28, 2560, 8, 5, x, y) \ _(29, 3072, 4, 3, x, y) #endif /* ZEND_ALLOC_SIZES_H */
- size = size - 1;
This is a boundary case, just like the previous one, the sizes that appear later are considered to be nearly reduced by one
Assuming we don’t look at this source code, we need to find the corresponding bin_num in - ZEND_MM_BINS_INFO
## is learned from ZEND_MM_BINS_INFO - Subsequently, 4 will be added as a group, respectively
2^4, 2^5, 2^6...
- Find which group this size belongs to
- And what is the offset of the size within the group
- Calculate the starting position of the group
- Now the problem is converted into the above three small problems, let’s solve them one by one
- The easiest way to find which group the size belongs to is to compare the sizes, right? You can use if...else to compare one by one, but obviously the PHP source code does not work like this, then What other options do we have?
-
64 | 100 0000 80 | 101 0000 96 | 110 0000 112 | 111 0000 128 | 1000 0000 160 | 1010 0000 192 | 1100 0000 224 | 1110 0000 256 | 1 0000 0000 320 | 1 0100 0000 384 | 1 1000 0000 448 | 1 1100 0000 .....
- If we look at the binary numbers above, we will find that the lengths of the binary numbers in each group are equal, and each of the following ones has one more digit than the previous one.
- 1
- Then the question is converted to Find the number of digits of the highest bit
1
in the binary number
- The solution to the PHP source code is given below. We will not parse it here for the time being, as long as you know that it returns the binary number The number of digits of the highest
1
int n = 16; if (size
Assuming that the size we apply for is 65, then n here returns 7
- This is simple, directly subtract the starting siez size of each group from size and then divide it by the current group The difference (16, 32, 64...) is enough, that is, (size-64)/16 (size-128)/32 (size-256)/64
- Now let’s take a look at the returned value in the previous step. Each group is
7, 8, 9...
, so let’s now take a look at how to calculate such data. The offset within the group
(size - 2^4 * 4) / 16 = size / 2^4 - 4 (size - 2^5 * 4) / 32 = size / 2^5 - 4 (size - 2^6 * 4) / 64 = szie / 2^6 - 4
Can we use
7, 8, 9
- 4, 5, 6
- , so that we can get the difference of the current group (16, 32, 64...) based on the information of which group it is in
When size is 65, is the offset equal to
(64-64) / 2^4 = 0
Calculate the starting position of the group - Now we have Offset information, assuming our grouping is 1, 2, 3
- from the number of digits in the highest bit
- 1
? 6
You can get the grouping information -
We know The starting positions areAfter getting the grouping information, how do you know the starting position of each group
8, 12, 16... - It is also an arithmetic sequence, which is
4n 4
We are looking at Look at the example of size=65
计算的偏移量是0
计算的起始位置是
4*1 + 4 = 8
所以当size=65的bin_num就是起始位置加上偏移量
8 + 0 = 8
我们再看一个size=129的例子
二进制中最高位的
1
的位数为8然后用8减去3得到5
(129 - 1 - 32 * 4) / 64 = 0
偏移量是
计算起始位置是
4 * 2 + 4 = 12
两者相加就是
12 + 0 = 0
size=193
二进制中最高位的
1
的位数为8(193 - 1 - 32 * 4) / 64 = 2
偏移量是
计算起始位置是
4 * 2 + 4 = 12
两者相加就是
12 + 2 = 14
size=1793
二进制中最高位的
1
的位数为11(1793 - 1 - 256 * 4) / 256 = 3
偏移量是
计算起始位置是
4 * 5 + 4 = 24
两者相加就是
24 + 3 = 27
代码分析
php实现代码
1 t1 = size - 1; 2 t2 = zend_mm_small_size_to_bit(t1) - 3; 3 t1 = t1 >> t2; 4 t2 = t2 - 3; 5 t2 = t2 <h4 id="第一行">第一行</h4>
t1 = size - 1;
是为了考虑size为64、128...这些边界情况
第二行
t2 = zend_mm_small_size_to_bit(t1) - 3;
这里调用了
zend_mm_small_size_to_bit
这个函数,我们看看这个函数
/* higher set bit number (0->N/A, 1->1, 2->2, 4->3, 8->4, 127->7, 128->8 etc) */ int n = 16; if (size
看注释我们就知道这个函数是用来返回当前size二进制中最高位1的位数,具体的做法呢其实就是二分法
-
我们通过
zend_mm_small_size_to_bit
这个函数获取了size二进制中最高位1的位数,那么这个-3
是什么神奇的操作呢(size - 2^4 * 4) / 16 = size / 2^4 - 4 (size - 2^5 * 4) / 32 = size / 2^5 - 4 (size - 2^6 * 4) / 64 = szie / 2^6 - 4
这里获取二进制的位数是7、8、9...通过
-3
的操作来获取相应的 4、5、6...上问的分析中提到,我们计算size在组内的偏移量的公式
第三行
t1 = t1 >> t2;
把t1右移t2位,这又是什么神奇的操作?
这里我们把最后计算bin_num的数学公式给写出来,它是等于每组的起始位置加上组内的偏移量
binnum = (4n + 4) + (size / 2^n - 4) binnum = 4n + size / 2^n
所以第三行的意思我们就知道了,就是size右移2^n次方为
第四行
t2 = t2 - 3;
这个好理解,可以参照上文得到每组的起始位置的方法
第五行
t2 = t2
我们再看看bin_num的计算公式
binnum = (4n + 4) + (size / 2^n - 4) binnum = 4n + size / 2^n
那么这行就好理解了,就是计算每组的起始位置
4n
对吧,左移两位就是乘以4
第六行
return (int)(t1 + t2);
这行没啥说的,就是返回了一个int类型的bin_num
The above is the detailed content of Calculation of small memory specifications in php (code example). For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

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

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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