|
5 | 5 | * [related file](#related-file) |
6 | 6 | * [memory layout](#memory-layout) |
7 | 7 | * [example](#example) |
8 | | - * [empty bytes](#empty-bytes) |
9 | | - * [ascii characters](#ascii-characters) |
10 | | - * [nonascii characters](#nonascii-characters) |
| 8 | + * [0](#0) |
| 9 | + * [1](#1) |
| 10 | + * [0.1](#0.1) |
| 11 | + * [1.1](#1.1) |
| 12 | + * [-0.1](#-0.1) |
11 | 13 | * [summary](#summary) |
12 | | - * [ob_shash](#ob_shash) |
13 | | - * [ob_size](#ob_size) |
14 | | - * [summary](#summary) |
15 | 14 |
|
16 | 15 | #### related file |
17 | | -* cpython/Objects/bytesobject.c |
18 | | -* cpython/Include/bytesobject.h |
19 | | -* cpython/Objects/clinic/bytesobject.c.h |
| 16 | +* cpython/Objects/floatobject.c |
| 17 | +* cpython/Include/floatobject.h |
| 18 | +* cpython/Objects/clinic/floatobject.c.h |
20 | 19 |
|
21 | 20 | #### memory layout |
22 | 21 |
|
23 | | - |
| 22 | +**PyFloatObject** is no more than a wrapper of c type **double**, which takes 8 bytes to represent a floating point number |
24 | 23 |
|
25 | | -The memory layout of **PyBytesObject** looks like [memory layout of tuple object](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/tuple/tuple.md#memory-layout) and [memory layout of int object](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/long/long.md#memory-layout), but simpler than any of them. |
| 24 | +you can refer to [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754-1985)/[IEEE-754标准与浮点数运算](https://blog.csdn.net/m0_37972557/article/details/84594879) for more detail |
26 | 25 |
|
27 | | -#### example |
28 | | - |
29 | | -##### empty bytes |
30 | | - |
31 | | -**bytes** object is an immutable object, whenever you need to modify a **bytes** object, you need to create a new one, which keeps the implementation simple. |
32 | | - |
33 | | - s = b"" |
34 | | - |
35 | | - |
36 | | - |
37 | | -##### ascii characters |
| 26 | + |
38 | 27 |
|
39 | | -let's initialize a byte object with ascii characters |
| 28 | +#### example |
40 | 29 |
|
41 | | - s = b"abcdefg123" |
| 30 | +##### 0 |
42 | 31 |
|
43 | | - |
| 32 | +the binary representation of 0.0 in **IEEE 754** format is all bit in zero |
44 | 33 |
|
45 | | -##### nonascii characters |
| 34 | + f = 0.0 |
46 | 35 |
|
47 | | - s = "我是帅哥".encode("utf8") |
| 36 | + |
48 | 37 |
|
49 | | - |
| 38 | +##### 1.0 |
50 | 39 |
|
51 | | -#### summary |
| 40 | + f = 1.0 |
52 | 41 |
|
| 42 | + |
53 | 43 |
|
54 | | -##### ob_shash |
| 44 | +##### 0.1 |
55 | 45 |
|
| 46 | + f = 0.1 |
56 | 47 |
|
57 | | -The field **ob_shash** should stores the hash value of the byte object, value **-1** means not computed yet. |
| 48 | + |
58 | 49 |
|
59 | | -The first time the hash value computed, it will be cached to the **ob_shash** field |
| 50 | +##### 1.1 |
60 | 51 |
|
61 | | -the cached hash value can saves recalculation and speeds up dict lookups |
| 52 | +the difference between 1.1 and 0.1 is the last few exponent bits |
62 | 53 |
|
63 | | -##### ob_size |
| 54 | + |
64 | 55 |
|
65 | | -field **ob_size** is inside every **PyVarObject**, the **PyBytesObject** uses this **field** to store size information to keep O(1) time complexity for **len()** opeeration and tracks the size of non-ascii string(may be null characters inside) |
| 56 | +##### -0.1 |
66 | 57 |
|
67 | | -##### summary |
| 58 | +the difference between -0.1 and 0.1 is the first sign bit |
68 | 59 |
|
69 | | -The **PyBytesObject** is a python wrapper of c style null terminate string, with **ob_shash** for caching hash value and **ob_size** for storing the size information of **PyBytesObject** |
| 60 | + |
70 | 61 |
|
71 | | -The implementation of **PyBytesObject** looks like the **embstr** encoding in redis |
| 62 | +##### nonascii characters |
72 | 63 |
|
73 | | - redis-cli |
74 | | - 127.0.0.1:6379> set a "hello" |
75 | | - OK |
76 | | - 127.0.0.1:6379> object encoding a |
77 | | - "embstr" |
| 64 | + s = "我是帅哥".encode("utf8") |
0 commit comments