Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do i need the gil for PyMem_Malloc()?

As per this discussion, PyMem_Malloc() requires the GIL; however, if the function is nothing more than an alias for malloc(), who cares?

like image 530
Noob Saibot Avatar asked Mar 01 '15 04:03

Noob Saibot


People also ask

What is *pyobject_malloc()?

void *PyObject_Malloc(size_t n)¶ Part of the Stable ABI. Allocates nbytes and returns a pointer of type void*to the allocated memory, or NULLif the request fails. Requesting zero bytes returns a distinct non-NULLpointer if possible, as if PyObject_Malloc(1)had been called instead. The memory will not have been initialized in any way.

What is pymalloc in Python?

Python has a pymalloc allocator optimized for small objects (smaller or equal to 512 bytes) with a short lifetime. It uses memory mappings called “arenas” with a fixed size of 256 KiB. It falls back to PyMem_RawMalloc () and PyMem_RawRealloc () for allocations larger than 512 bytes.

What is Gil in Python and how does it work?

The GIL is a single lock on the interpreter itself which adds a rule that execution of any Python bytecode requires acquiring the interpreter lock. This prevents deadlocks (as there is only one lock) and doesn’t introduce much performance overhead. But it effectively makes any CPU-bound Python program single-threaded.

What is the gilectomy in Python?

The multi-threading programmers don’t have to fret as some of the brightest minds in the Python community are working to remove the GIL from CPython. One such attempt is known as the Gilectomy. The Python GIL is often regarded as a mysterious and difficult topic.


Video Answer


1 Answers

Because it is sometimes more than simply an alias for malloc(). Sometimes it is an alias for _PyMem_DebugMalloc() and there is some global accounting there to keep track of unique memory objects. There's no real point in releasing the GIL just for a PyMem_Malloc() call, so you're probably doing something more complicated in C. If that's the case, you can simply call malloc() and not get any of the debugging stuff.

like image 90
Nathan Binkert Avatar answered Oct 09 '22 03:10

Nathan Binkert