Python source code analysis notes: string objects in python

Source: Internet
Author: User

 

 

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.

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.