
1. 不可變的PyIntObject
Python原始碼剖析 - 物件初探 我們對 PyIntObject 已經有了初步的了解, Python 中的物件可以分為固定長度和可變長度兩種型別,除此之外,也可以按照可變和不可變進行劃分,
PyIntObject 則屬于長度固定且不可變的物件,相比其他的物件而言,最簡單,也最容易理解,
我們先來了解一下 PyIntObject 型別的型別資訊,代碼如下:
PyTypeObject PyInt_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"int",
sizeof(PyIntObject),
0,
(destructor)int_dealloc, /* tp_dealloc */
(printfunc)int_print, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
(cmpfunc)int_compare, /* tp_compare */
(reprfunc)int_to_decimal_string, /* tp_repr */
&int_as_number, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
(hashfunc)int_hash, /* tp_hash */
0, /* tp_call */
(reprfunc)int_to_decimal_string, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
Py_TPFLAGS_BASETYPE | Py_TPFLAGS_INT_SUBCLASS, /* tp_flags */
int_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
int_methods, /* tp_methods */
0, /* tp_members */
int_getset, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
int_new, /* tp_new */
};
核心代碼解釋:
| 代碼 | 說明 |
|---|---|
| PyVarObject_HEAD_INIT(&PyType_Type, 0) | 1. 設定ob_type指向PyInt_Type結構的地址 2.定長 |
| "int" | 設定 tp_name |
| int_dealloc | PyIntObject物件的解構式 |
| int_print | PyIntObject物件的標準輸出函式 |
| int_compare | 比較操作 |
| int_to_decimal_string | 將整數轉換為數字字串 |
| &int_as_number | 數學操作函式集合,如加減乘除等 |
| int_hash | 計算該物件的hash值 |
| int_methods | 物件成員函式的集合 |
2. PyIntObject物件創建三種方式
關于 PyIntObject 的物件創建程序,我們在Python原始碼剖析 - 物件初探中已經做了初步的介紹,通過閱讀原始碼我們可以發現有有以下三種方式,可以用來創建 PyIntObject 物件
PyAPI_FUNC(PyObject *) PyInt_FromString(char*, char**, int);
PyAPI_FUNC(PyObject *) PyInt_FromUnicode(Py_UNICODE*, Py_ssize_t, int);
PyAPI_FUNC(PyObject *) PyInt_FromLong(long);
也就是說,一個 PyIntObject 可以來源于 String、Unicode 和 Long 型別的變數,
PyObject *
PyInt_FromLong(long ival)
{
register PyIntObject *v;
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) {
v = small_ints[ival + NSMALLNEGINTS];
Py_INCREF(v);
#ifdef COUNT_ALLOCS
if (ival >= 0)
quick_int_allocs++;
else
quick_neg_int_allocs++;
#endif
return (PyObject *) v;
}
#endif
if (free_list == NULL) {
if ((free_list = fill_free_list()) == NULL)
return NULL;
}
/* Inline PyObject_New */
v = free_list;
free_list = (PyIntObject *)Py_TYPE(v);
(void)PyObject_INIT(v, &PyInt_Type);
v->ob_ival = ival;
return (PyObject *) v;
}
從代碼實作來看,如果是一個小整數,那么就直接增加對這個小整數物件的參考,否則,則需要從 free_list 中選取一個可用的物件,并將該物件的 ob_ival 設定為當前的數值,
接下來,我們詳細介紹 PyIntObject 中對小整數的處理方式,
3. PyIntObject的小整數物件
在 Python 中,代碼直接對小整數物件的范圍進行了限定,即 [-5, 257)
#ifndef NSMALLPOSINTS
#define NSMALLPOSINTS 257
#endif
#ifndef NSMALLNEGINTS
#define NSMALLNEGINTS 5
#endif
#if NSMALLNEGINTS + NSMALLPOSINTS > 0
/* References to small integers are saved in this array so that they
can be shared.
The integers that are saved are those in the range
-NSMALLNEGINTS (inclusive) to NSMALLPOSINTS (not inclusive).
*/
static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
#endif
這些小整數物件,類似于常量一樣常駐記憶體中,并不會不釋放,這樣做的優點在于:
- 使用效率高,這些小整數物件,像靜態常量一樣,直接拿來就可以用
- 避免經常使用小整數導致記憶體操作效率降低 - 假設我們沒有將小整數常駐記憶體,按照 Python 中其他物件的處理方式來處理,必然會導致 malloc() 和 free() 的頻繁呼叫,引起大量記憶體碎片,嚴重影響 Python 的整體性
但是這樣做有幾個的問題:
- 常量的設定,是一個經驗值,你沒辦法預計在你的程式里,這個樣的設定就是最優的
- 修改代價大,如果你發現我對小整數的范圍進行調整,你能做的唯一辦法,就是對原始碼進行修改,并進行重新編譯,
4. PyIntObject的大整數物件
對于小整數,Python 通過小整數物件池的方式來解決效率問題,那么對于其他整數物件,又是如何處理的呢,
其實與小整數類似,也是通過記憶體池技術,不同的是這個記憶體池中的數值并不是固定的,而是誰需要使用,就來申請,使用完了,則歸還到池子中去,
struct _intblock {
struct _intblock *next;
PyIntObject objects[N_INTOBJECTS];
};
typedef struct _intblock PyIntBlock;
static PyIntBlock *block_list = NULL;
static PyIntObject *free_list = NULL;
static PyIntObject *
fill_free_list(void)
{
PyIntObject *p, *q;
/* Python's object allocator isn't appropriate for large blocks. */
p = (PyIntObject *) PyMem_MALLOC(sizeof(PyIntBlock));
if (p == NULL)
return (PyIntObject *) PyErr_NoMemory();
((PyIntBlock *)p)->next = block_list;
block_list = (PyIntBlock *)p;
/* Link the int objects together, from rear to front, then return
the address of the last int object in the block. */
p = &((PyIntBlock *)p)->objects[0];
q = p + N_INTOBJECTS;
while (--q > p)
Py_TYPE(q) = (struct _typeobject *)(q-1);
Py_TYPE(q) = NULL;
return p + N_INTOBJECTS - 1;
}
通過 block_list 和 free_list 兩個指標來進行維護,free_list 是一個單向串列,維護 block_list 中所有可用的記憶體塊,如果 block_list 不夠用了,則呼叫 fill_free_list() 申請新的記憶體,
5. 更多內容
原文來自兔子先生網站:https://www.xtuz.net/detail-136.html
查看原文 >>> Python原始碼剖析 - Python中的整數物件
如果你對Python語言感興趣,可以關注我,或者關注我的微信公眾號:xtuz666
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/167462.html
標籤:Python
