Python basic Teaching List Object Goto

Source: Internet
Author: User

Python Basic Teaching List Object time: 2014-01-19 Source: Server home Contributor: Root 1. Pylistobject Object
typedef struct {
Pyobject_var_head
Pyobject **ob_item;
py_ssize_t allocated;
} Pylistobject;

The obsize in Pyobject_var_head indicates the number of elements that the list object contains.

The allocated represents the memory space occupied by the list object.

Ob_item actually points to an array of pointers, each of which points to the true pyobject.

2. Pylistobject Object Use
When created, you can specify the size to open up the required memory for the element list.
Op->ob_item = (pyobject * *) Pymem_malloc (nbytes);
memset (op->ob_item, 0, nbytes);
After you create a new list object, you can insert, add, and delete elements. If the allocated is insufficient, the memory space can be automatically applied.
The memory growth model for list in Python is as follows:
0, 4, 8, 16, 25, 35, 46, 58, 72, 88
The implementation is as follows:
new_allocated = (newsize >> 3) + (NewSize < 9? 3:6);
When you delete a list object
i = py_size (OP);
while (---->= 0) {//reduces references to real pyobject objects individually
Py_xdecref (Op->ob_item[i]);
}
Pymem_free (Op->ob_item); Then release the element pointer list

and whether the list object is destroyed, as follows.

3. Pylistobject Object Buffer Pool

#define PYLIST_MAXFREELIST 80
Static Pylistobject *free_list[pylist_maxfreelist];
Free_list is an array of pointers that point to the list object.

When you create a list object:

if (numfree) {//If free_list has idle, point directly to
numfree--;
op = free_list[numfree];
_py_newreference ((Pyobject *) op);
} else {//otherwise new open memory space
op = pyobject_gc_new (Pylistobject, &pylist_type);
}

When you delete a list object:

if (Numfree < pylist_maxfreelist)//If you have free space, put the List object in Free_list
free_list[numfree++] = op;
else//otherwise directly destroyed
Py_type (OP)->tp_free ((Pyobject *) op);

Python basic Teaching List Object Goto

Related Article

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.