Skip to content

Commit 9a1de3c

Browse files
committed
rename Cpython to CPython
1 parent 3efeb23 commit 9a1de3c

29 files changed

Lines changed: 278 additions & 278 deletions

BasicObject/bytearray/bytearray.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ The **ob_alloc** field represent the real allocated size in bytes
3939
4353755712
4040

4141

42-
![empty](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/empty.png)
42+
![empty](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/empty.png)
4343

4444
##### append
4545

4646
after append a charracter 'a', **ob_alloc** becomes 2, **ob_bytes** and **ob_start** all points to same address
4747

4848
a.append(ord('a'))
4949

50-
![append_a](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/append_a.png)
50+
![append_a](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/append_a.png)
5151

5252
##### resize
5353

@@ -67,47 +67,47 @@ In appending, ob_alloc is 2, and request size is 2, 2 <= 2 * 1.125, so the new a
6767

6868
a.append(ord('b'))
6969

70-
![resize](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/resize.png)
70+
![resize](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/resize.png)
7171

7272
##### slice
7373

7474
b = bytearray(b"abcdefghijk")
7575

76-
![slice](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/slice.png)
76+
![slice](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/slice.png)
7777

7878
After the slice operation, **ob_start** points to the real beginning of the content, and **ob_bytes** still points to the begin address of the malloced block
7979

8080
b[0:5] = [1,2]
8181

82-
![after_slice](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/after_slice.png)
82+
![after_slice](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/after_slice.png)
8383

8484
as long as the slice operation is going to shrink the bytearray, and the **new_size < alloc / 2** is False, the resize operation won't shrink the real mallcoed size
8585

8686
b[2:6] = [3, 4]
8787

88-
![after2_slice](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/after2_slice.png)
88+
![after2_slice](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/after2_slice.png)
8989

9090
now, in the shrink operation, the **new_size < alloc / 2** is True, the resize operation will be triggered
9191

9292
b[0:3] = [7,8]
9393

94-
![after3_slice](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/after3_slice.png)
94+
![after3_slice](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/after3_slice.png)
9595

9696
The grow pattern in slice operation is same as the append operation
9797

9898
request size is 6, 6 < 6 * 1.125, so new allocated size is 6 + (6 >> 3) + 3 ==> 9
9999

100100
b[0:3] = [1,2,3,4]
101101

102-
![after_grow_slice](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/after_grow_slice.png)
102+
![after_grow_slice](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/after_grow_slice.png)
103103

104104
##### ob_exports
105105

106106
what's field **ob_exports** mean ? If you need detail, you can refer to [less-copies-in-python-with-the-buffer-protocol-and-memoryviews](https://eli.thegreenplace.net/2011/11/28/less-copies-in-python-with-the-buffer-protocol-and-memoryviews) and [PEP 3118](https://www.python.org/dev/peps/pep-3118/)
107107

108108
buf = bytearray(b"abcdefg")
109109

110-
![exports](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/exports.png)
110+
![exports](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/exports.png)
111111

112112
the **bytearray** implements the **buffer protocol**, and **memoryview** is able to access the internal data block via the **buffer protocol**, **mybuf** and **buf** are all sharing the same internal block
113113

@@ -116,25 +116,25 @@ field **ob_exports** becomes 1, which indicate how many objects currently sharin
116116
mybuf = memoryview(buf)
117117
mybuf[1] = 3
118118

119-
![exports_1](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/exports_1.png)
119+
![exports_1](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/exports_1.png)
120120

121121
so does **mybuf2** object(**ob_exports** doesn't change because you need to call the c function defined by **buf** object via the **buffer protocol**, **mybuf2** barely calls the slice function of **mybuf**)
122122

123123
mybuf2 = mybuf[:4]
124124
mybuf2[0] = 1
125125

126-
![exports_2](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/exports_2.png)
126+
![exports_2](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/exports_2.png)
127127

128128
**ob_exports** becomes 2
129129

130130
mybuf3 = memoryview(buf)
131131

132-
![exports_3](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/exports_3.png)
132+
![exports_3](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/exports_3.png)
133133

134134
**ob_exports** becomes 0
135135

136136
del mybuf
137137
del mybuf2
138138
del mybuf3
139139

140-
![exports_4](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/exports_4.png)
140+
![exports_4](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/exports_4.png)

BasicObject/bytearray/bytearray_cn.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@
3939
4353755712
4040

4141

42-
![empty](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/empty.png)
42+
![empty](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/empty.png)
4343

4444
##### append
4545

4646
增加了一个字符 'a' 之后, **ob_alloc** 变成了 2, **ob_bytes****ob_start** 都指向同一个地址
4747

4848
a.append(ord('a'))
4949

50-
![append_a](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/append_a.png)
50+
![append_a](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/append_a.png)
5151

5252
##### resize
5353

@@ -67,47 +67,47 @@
6767

6868
a.append(ord('b'))
6969

70-
![resize](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/resize.png)
70+
![resize](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/resize.png)
7171

7272
##### slice
7373

7474
b = bytearray(b"abcdefghijk")
7575

76-
![slice](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/slice.png)
76+
![slice](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/slice.png)
7777

7878
slice 操作之后, **ob_start** 指向真正的数组内容开始的位置, **ob_bytes** 仍然指向内存分配的开始位置
7979

8080
b[0:5] = [1,2]
8181

82-
![after_slice](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/after_slice.png)
82+
![after_slice](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/after_slice.png)
8383

8484
只要 slice 操作是缩减 bytearray 大小的, 并且如下判断 **new_size < alloc / 2** 不成立, bytearray 就不会在对应的操作下对内存占用重新分配
8585

8686
b[2:6] = [3, 4]
8787

88-
![after2_slice](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/after2_slice.png)
88+
![after2_slice](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/after2_slice.png)
8989

9090
现在, 在缩小的操作中, **new_size < alloc / 2** 成立, 重新分配被触发
9191

9292
b[0:3] = [7,8]
9393

94-
![after3_slice](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/after3_slice.png)
94+
![after3_slice](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/after3_slice.png)
9595

9696
在 slice 操作中的增长策略和 append 是相同的, 他们都是共用一个函数来调整空间
9797

9898
申请的大小是 6, 6 < 6 * 1.125, 所以新分配的大小为 6 + (6 >> 3) + 3 ==> 9
9999

100100
b[0:3] = [1,2,3,4]
101101

102-
![after_grow_slice](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/after_grow_slice.png)
102+
![after_grow_slice](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/after_grow_slice.png)
103103

104104
##### ob_exports
105105

106106
**ob_exports** 这里表示的值是什么意思呢? 你需要先理解 **buffer protocol** 的概念和设计初衷, 请参考 [less-copies-in-python-with-the-buffer-protocol-and-memoryviews](https://eli.thegreenplace.net/2011/11/28/less-copies-in-python-with-the-buffer-protocol-and-memoryviews)[PEP 3118](https://www.python.org/dev/peps/pep-3118/)
107107

108108
buf = bytearray(b"abcdefg")
109109

110-
![exports](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/exports.png)
110+
![exports](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/exports.png)
111111

112112
**bytearray** 对象实现了 **buffer protocol** 的标准, **memoryview** 可以通过 **buffer protocol** 去获取对象中的实际的字节块, 而不是拷贝一份, **mybuf****buf** 就是共享同一个内存块
113113

@@ -116,25 +116,25 @@ slice 操作之后, **ob_start** 指向真正的数组内容开始的位置, **o
116116
mybuf = memoryview(buf)
117117
mybuf[1] = 3
118118

119-
![exports_1](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/exports_1.png)
119+
![exports_1](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/exports_1.png)
120120

121121
**mybuf2** 也进行了同样的操作(ob_exports并未增加, 是因为 **mybuf2** 调用的是 **mybuf** 的 slice 操作, 增加 **ob_exports** 是通过调用 **buf****buffer protocol** 定义的c函数完成的)
122122

123123
mybuf2 = mybuf[:4]
124124
mybuf2[0] = 1
125125

126-
![exports_2](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/exports_2.png)
126+
![exports_2](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/exports_2.png)
127127

128128
**ob_exports** 现在变成了 2
129129

130130
mybuf3 = memoryview(buf)
131131

132-
![exports_3](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/exports_3.png)
132+
![exports_3](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/exports_3.png)
133133

134134
**ob_exports** 现在变成了 0
135135

136136
del mybuf
137137
del mybuf2
138138
del mybuf3
139139

140-
![exports_4](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/exports_4.png)
140+
![exports_4](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytearray/exports_4.png)

BasicObject/bytes/bytes.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
![memory layout](https://img-blog.csdnimg.cn/20190318160629447.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMxNzIwMzI5,size_16,color_FFFFFF,t_70)
2424

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.
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.
2626

2727
#### example
2828

@@ -32,21 +32,21 @@ The memory layout of **PyBytesObject** looks like [memory layout of tuple object
3232

3333
s = b""
3434

35-
![empty](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytes/empty.png)
35+
![empty](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytes/empty.png)
3636

3737
##### ascii characters
3838

3939
let's initialize a byte object with ascii characters
4040

4141
s = b"abcdefg123"
4242

43-
![ascii](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytes/ascii.png)
43+
![ascii](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytes/ascii.png)
4444

4545
##### nonascii characters
4646

4747
s = "我是帅哥".encode("utf8")
4848

49-
![nonascii](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytes/nonascii.png)
49+
![nonascii](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytes/nonascii.png)
5050

5151
#### summary
5252

BasicObject/bytes/bytes_cn.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
![memory layout](https://img-blog.csdnimg.cn/20190318160629447.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMxNzIwMzI5,size_16,color_FFFFFF,t_70)
2424

25-
**PyBytesObject** 的内存构造和 [tuple的内存构造](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/tuple/tuple_cn.md#%E5%86%85%E5%AD%98%E6%9E%84%E9%80%A0)/[int的内存构造](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/long/long_cn.md#%E5%86%85%E5%AD%98%E6%9E%84%E9%80%A0) 非常相似, 但是实现起来比上面的其他元素要简单一些
25+
**PyBytesObject** 的内存构造和 [tuple的内存构造](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/tuple/tuple_cn.md#%E5%86%85%E5%AD%98%E6%9E%84%E9%80%A0)/[int的内存构造](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/long/long_cn.md#%E5%86%85%E5%AD%98%E6%9E%84%E9%80%A0) 非常相似, 但是实现起来比上面的其他元素要简单一些
2626

2727
#### 示例
2828

@@ -32,21 +32,21 @@
3232

3333
s = b""
3434

35-
![empty](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytes/empty.png)
35+
![empty](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytes/empty.png)
3636

3737
##### ascii characters
3838

3939
我们来初始化一个只包含 ascii 字符的 bytes 对象
4040

4141
s = b"abcdefg123"
4242

43-
![ascii](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytes/ascii.png)
43+
![ascii](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytes/ascii.png)
4444

4545
##### nonascii characters
4646

4747
s = "我是帅哥".encode("utf8")
4848

49-
![nonascii](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytes/nonascii.png)
49+
![nonascii](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/bytes/nonascii.png)
5050

5151
#### 总结
5252

BasicObject/dict/dict.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ to
155155

156156
now, the overview is clear
157157

158-
![dictkeys_basic](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/dict/dictkeys_basic.png)
158+
![dictkeys_basic](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/dict/dictkeys_basic.png)
159159

160160
#### hash collisions and delete
161161

@@ -190,26 +190,26 @@ I've altered the source code to print some information
190190
'{1: 1}'
191191

192192

193-
![hh_1](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/dict/hh_1.png)
193+
![hh_1](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/dict/hh_1.png)
194194

195195
d[4] = 4
196196

197-
![hh_2](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/dict/hh_2.png)
197+
![hh_2](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/dict/hh_2.png)
198198

199199
d[7] = 111
200200

201-
![hh_3](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/dict/hh_3.png)
201+
![hh_3](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/dict/hh_3.png)
202202

203203
# delete, mark as DKIX_DUMMY
204204
# notice, dk_usable and dk_nentries don't change
205205
del d[4]
206206

207-
![hh_4](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/dict/hh_4.png)
207+
![hh_4](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/dict/hh_4.png)
208208

209209
# notice, dk_usable and dk_nentries now change
210210
d[0] = 0
211211

212-
![hh_5](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/dict/hh_5.png)
212+
![hh_5](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/dict/hh_5.png)
213213

214214
d[16] = 16
215215
# hash (16) & mask == 0
@@ -223,7 +223,7 @@ I've altered the source code to print some information
223223
# i = (i*5 + perturb + 1) & mask ===> i = 6
224224
# position 6 is empty, so we take it
225225

226-
![hh_6](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/dict/hh_6.png)
226+
![hh_6](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/dict/hh_6.png)
227227

228228
#### resize
229229

@@ -232,7 +232,7 @@ I've altered the source code to print some information
232232
d[5] = 5
233233
# step1: resize, when resizing, the deleted object which mark as DKIX_DUMMY in entries won't be copyed
234234

235-
![resize](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/dict/resize.png)
235+
![resize](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/dict/resize.png)
236236

237237
# step2: insert key: 5, value: 5
238238

@@ -244,6 +244,6 @@ Notice, the indices array is variable size. when size of your hash table is <= 1
244244

245245
static PyDictObject *free_list[PyDict_MAXFREELIST];
246246

247-
cpython also use free_list to reuse the deleted hash table, to avoid memory fragment and improve performance, I've illustrated free_list in [list object](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/list/list.md#why-free-list)
247+
cpython also use free_list to reuse the deleted hash table, to avoid memory fragment and improve performance, I've illustrated free_list in [list object](https://github.com/zpoint/CPython-Internals/blob/master/BasicObject/list/list.md#why-free-list)
248248

249249
now, you understand how python dictionary object work internally.

0 commit comments

Comments
 (0)