1. PyStringObject --> variable-length immutable object
Typedef struct {PyObject_VAR_HEAD // The ob_size variable stores the longob_shash variable-length memory maintained in the object; // cache the hash value of the object for dict query intob_sstate; // indicates whether the object is processed through the intern mechanism. char ob_sval [1]; // character pointer, pointing to a memory segment} PyStringObject;
PyString_Type --> PyStringObject type object.
PyTypeObjectPyString_Type = {PyObject_HEAD_INIT(&PyType_Type)0,str,sizeof(PyStringObject),sizeof(char),//……(reprfunc)string_repr, /* tp_repr */&string_as_number, /* tp_as_number */&string_as_sequence, /* tp_as_sequence */&string_as_mapping, /* tp_as_mapping */(hashfunc)string_hash, /* tp_hash */0, /* tp_call *///……string_new, /* tp_new */PyObject_Del, /* tp_free */};
PyStringObject supports numeric, sequential, and ing operations.
2. Create a PyStringObject
There are two ways:
PyObject *PyString_FromString(const char *str)PyObject *PyString_FromStringAndSize(const char *str, intsize)
PyString_FromString
1. Judge the string length: PY_SSIZE_T_MAX
2. Process null string
3. process a single character
4. Apply for memory, create a new PyStringObject object, and initialize it.
5. PyStringObject objects with short intern Length
3. intern mechanism of string objects
For strings after intern, there is only one PyStringObject object corresponding to the "Ruby" string in the system during the entire python runtime, such as "Ruby.
If intern is applied to a, Python will first search for the PyStringObject object that has been processed by the intern mechanism in the system when B is created, if you find that the PyStringObject object corresponding to the character array already exists, you can directly return the reference of this object.
Interned is the (key, value) set --> key, value is the reference of the corresponding PyStringObject object
The pointer in interned can also be used as a valid reference of a. Therefore, after adding the PyObject pointer of a as the key and value to interned, the reference count of a should be reduced by 2, otherwise, a will never be deleted before the end of python.
After Python creates a PyStringObject object temp at runtime, it basically calls PyString_InternInPlace to process temp. The intern mechanism reduces the reference count of temp, the temp object will be destroyed because the reference count is reduced to 0.
4. Character Buffer Pool
1) create a PyStringObject "P"
2) Intern operation on object "P"
3) cache the object "P" to the character buffer pool.
Is it just for accelerated search?
5. PyStringObject efficiency problems
String connection
"+" Low efficiency, connect N PyStringObject to N-1 memory application and handling work
Join is recommended.