Manage calls to calloc/free through Cython
Project description
cymem: A Cython Memory Helper
cymem provides two small memory-management helpers for Cython. They make it easy to tie memory to a Python object's life-cycle, so that the memory is freed when the object is garbage collected.
Overview
The most useful is cymem.Pool
, which acts as a thin wrapper around the calloc
function:
from cymem.cymem cimport Pool
cdef Pool mem = Pool()
data1 = <int*>mem.alloc(10, sizeof(int))
data2 = <float*>mem.alloc(12, sizeof(float))
The Pool
object saves the memory addresses internally, and frees them when the
object is garbage collected. Typically you'll attach the Pool
to some cdef'd
class. This is particularly handy for deeply nested structs, which have
complicated initialization functions. Just pass the Pool
object into the
initializer, and you don't have to worry about freeing your struct at all —
all of the calls to Pool.alloc
will be automatically freed when the Pool
expires.
Installation
Installation is via pip, and requires Cython. Before installing, make sure that your pip
, setuptools
and wheel
are up to date.
pip install -U pip setuptools wheel
pip install cymem
Example Use Case: An array of structs
Let's say we want a sequence of sparse matrices. We need fast access, and a Python list isn't performing well enough. So, we want a C-array or C++ vector, which means we need the sparse matrix to be a C-level struct — it can't be a Python class. We can write this easily enough in Cython:
"""Example without Cymem
To use an array of structs, we must carefully walk the data structure when
we deallocate it.
"""
from libc.stdlib cimport calloc, free
cdef struct SparseRow:
size_t length
size_t* indices
double* values
cdef struct SparseMatrix:
size_t length
SparseRow* rows
cdef class MatrixArray:
cdef size_t length
cdef SparseMatrix** matrices
def __cinit__(self, list py_matrices):
self.length = 0
self.matrices = NULL
def __init__(self, list py_matrices):
self.length = len(py_matrices)
self.matrices = <SparseMatrix**>calloc(len(py_matrices), sizeof(SparseMatrix*))
for i, py_matrix in enumerate(py_matrices):
self.matrices[i] = sparse_matrix_init(py_matrix)
def __dealloc__(self):
for i in range(self.length):
sparse_matrix_free(self.matrices[i])
free(self.matrices)
cdef SparseMatrix* sparse_matrix_init(list py_matrix) except NULL:
sm = <SparseMatrix*>calloc(1, sizeof(SparseMatrix))
sm.length = len(py_matrix)
sm.rows = <SparseRow*>calloc(sm.length, sizeof(SparseRow))
cdef size_t i, j
cdef dict py_row
cdef size_t idx
cdef double value
for i, py_row in enumerate(py_matrix):
sm.rows[i].length = len(py_row)
sm.rows[i].indices = <size_t*>calloc(sm.rows[i].length, sizeof(size_t))
sm.rows[i].values = <double*>calloc(sm.rows[i].length, sizeof(double))
for j, (idx, value) in enumerate(py_row.items()):
sm.rows[i].indices[j] = idx
sm.rows[i].values[j] = value
return sm
cdef void* sparse_matrix_free(SparseMatrix* sm) except *:
cdef size_t i
for i in range(sm.length):
free(sm.rows[i].indices)
free(sm.rows[i].values)
free(sm.rows)
free(sm)
We wrap the data structure in a Python ref-counted class at as low a level as
we can, given our performance constraints. This allows us to allocate and free
the memory in the __cinit__
and __dealloc__
Cython special methods.
However, it's very easy to make mistakes when writing the __dealloc__
and
sparse_matrix_free
functions, leading to memory leaks. cymem prevents you from
writing these deallocators at all. Instead, you write as follows:
"""Example with Cymem.
Memory allocation is hidden behind the Pool class, which remembers the
addresses it gives out. When the Pool object is garbage collected, all of
its addresses are freed.
We don't need to write MatrixArray.__dealloc__ or sparse_matrix_free,
eliminating a common class of bugs.
"""
from cymem.cymem cimport Pool
cdef struct SparseRow:
size_t length
size_t* indices
double* values
cdef struct SparseMatrix:
size_t length
SparseRow* rows
cdef class MatrixArray:
cdef size_t length
cdef SparseMatrix** matrices
cdef Pool mem
def __cinit__(self, list py_matrices):
self.mem = None
self.length = 0
self.matrices = NULL
def __init__(self, list py_matrices):
self.mem = Pool()
self.length = len(py_matrices)
self.matrices = <SparseMatrix**>self.mem.alloc(self.length, sizeof(SparseMatrix*))
for i, py_matrix in enumerate(py_matrices):
self.matrices[i] = sparse_matrix_init(self.mem, py_matrix)
cdef SparseMatrix* sparse_matrix_init_cymem(Pool mem, list py_matrix) except NULL:
sm = <SparseMatrix*>mem.alloc(1, sizeof(SparseMatrix))
sm.length = len(py_matrix)
sm.rows = <SparseRow*>mem.alloc(sm.length, sizeof(SparseRow))
cdef size_t i, j
cdef dict py_row
cdef size_t idx
cdef double value
for i, py_row in enumerate(py_matrix):
sm.rows[i].length = len(py_row)
sm.rows[i].indices = <size_t*>mem.alloc(sm.rows[i].length, sizeof(size_t))
sm.rows[i].values = <double*>mem.alloc(sm.rows[i].length, sizeof(double))
for j, (idx, value) in enumerate(py_row.items()):
sm.rows[i].indices[j] = idx
sm.rows[i].values[j] = value
return sm
All that the Pool
class does is remember the addresses it gives out. When the
MatrixArray
object is garbage-collected, the Pool
object will also be garbage
collected, which triggers a call to Pool.__dealloc__
. The Pool
then frees all of
its addresses. This saves you from walking back over your nested data structures
to free them, eliminating a common class of errors.
Custom Allocators
Sometimes external C libraries use private functions to allocate and free objects,
but we'd still like the laziness of the Pool
.
from cymem.cymem cimport Pool, WrapMalloc, WrapFree
cdef Pool mem = Pool(WrapMalloc(priv_malloc), WrapFree(priv_free))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for cymem-2.0.6-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b0d1a6b0a1296f31fa9e4b7ae5ea49394084ecc883b1ae6fec4844403c43468 |
|
MD5 | 83c6518b1e227a94a13fb0e01bbfd45f |
|
BLAKE2b-256 | bf7353e5f7db24dbee43ea79edebfaae5fcb81fbc71466ca04c41813ec7d7874 |
Hashes for cymem-2.0.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 971cf0a8437dfb4185c3049c086e463612fe849efadc0f5cc153fc81c501da7d |
|
MD5 | 99991dcdc7af44eca6e81756d2ac4570 |
|
BLAKE2b-256 | a0a4d505fd395ee5090d637f0818255812ba40257b7a2dd08ca4c33a2253e259 |
Hashes for cymem-2.0.6-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd818356b635d8ae546e152a6f2b95f00e959d128a16155c275b0c202cd6312b |
|
MD5 | 980f8d0669f72ccc1eb7c895676f4981 |
|
BLAKE2b-256 | df2be27b7edc492c6e85fcb523d1b2042a3fc918ecc3f56415748a51c5bc4e7c |
Hashes for cymem-2.0.6-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a261f51796a2705f3900ed22b8442519a0f230f50a816fb5bd89cb9b027dc5ac |
|
MD5 | 6d6ea15ffc435db3e3a3ec1fc2b61a56 |
|
BLAKE2b-256 | 1b82f003479d2734388b84f490651f535e6dad0a7a80add17b3af433c5ba1031 |
Hashes for cymem-2.0.6-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 700540b68e96a7056d0691d467df2bbaaf0934a3e6fe2383669998cbee19580a |
|
MD5 | 92da1b0f247e210e17c5e10e17329cd5 |
|
BLAKE2b-256 | a2565d85beb543578a5ecbf8568c71899f9f7a195117ea80d750f70ff135216f |
Hashes for cymem-2.0.6-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c59293b232b53ebb47427f16cf648e937022f489cff36c11d1d8a1f0075b6609 |
|
MD5 | d03a5bdb679013b2690227613451c66f |
|
BLAKE2b-256 | fbfb35bbbc83f72c5f2a97e83379ea54d8fba2fb668f8b8f1a81c9b7a5929613 |
Hashes for cymem-2.0.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04676d696596b0db3f3c5a3936bab12fb6f24278921a6622bb185e61765b2b4d |
|
MD5 | b494147a735bf02eee544582c018f329 |
|
BLAKE2b-256 | aa39038d9b5331b48aa27a96095b450057ce861384b9ee309ba111a88b531812 |
Hashes for cymem-2.0.6-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4dc378fb9dda3b0529361fe32cfe1a6de0fc16bb40c710aaec8d217534928d2 |
|
MD5 | e139c81e5b09145c367520abd55b0afd |
|
BLAKE2b-256 | 77dfad8b9539bba32abe491581cc932ed1d9aeb23c8fae11a29f705ea8b78906 |
Hashes for cymem-2.0.6-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d631239bfb07293ee444b269656308da952b6b003b12332ccb1c624dbfcda4b |
|
MD5 | 00cca551a2d4d7274549442a4f06704d |
|
BLAKE2b-256 | a50e9022dd387d4ed1992e29d2fa598a0c0f31c00e02c12dc84ce32f245605bd |
Hashes for cymem-2.0.6-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a93fba62fe79dbf6fc4d5b6d804a6e114b44af3ff3d40a28833ee39f21bd336b |
|
MD5 | efe1d3c1197890219c0e39d7098ba4cf |
|
BLAKE2b-256 | adbe59de8100ac212a7833af0d33be0290a8f4d9099077849d76aca708af83e3 |
Hashes for cymem-2.0.6-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f87fe087f2ae36c3e20e2b1a29d7f76a28c035372d0a97655f26223d975235a |
|
MD5 | bc5494de15eb78a9a2586a1aa8c380c7 |
|
BLAKE2b-256 | 34400d2f208c4a79f06aa530cb6becc67740bb30b0a72d41055a0fa491f3e50f |
Hashes for cymem-2.0.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea535f74ab6024e7416f93de564e5c81fb7c0964b96280de66f60aeb05f0cf53 |
|
MD5 | 4c4256ef3c9be904c898a319738bc273 |
|
BLAKE2b-256 | d423e1ea9b36df7320f0281ed7a3aaa135c9596c2409d4f6d55e958fdad9e11c |
Hashes for cymem-2.0.6-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38b51ac23f914d85b197dbd0fb2d3e2de9bf6112b9b30f16b45dbb6c9b4e509d |
|
MD5 | b336971e1b675961030fcdc16f3dc3f9 |
|
BLAKE2b-256 | fd329b0bbac7bdabe45d05639f58447903824472c6b22c9c28302da0f2ec63e3 |
Hashes for cymem-2.0.6-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 228bd261a85d92d870ed358f263ee028ac026302304f2186827377a3895c5819 |
|
MD5 | 4b27190cdee0288fcda133be0aaa9167 |
|
BLAKE2b-256 | baf6a3d22b5a1fe3d62ee559705f2999792b0489cff865337ff298e50d164311 |
Hashes for cymem-2.0.6-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2aa3fa467d906cd2c27fa0a2e2952dd7925f5fcc7973fab6d815ef6acb25aad8 |
|
MD5 | b22f0971136db91d330525471b162b10 |
|
BLAKE2b-256 | 67b365938e27e534cd4176544ec8800c2ed09f49069473aa2fea9af4624fe178 |
Hashes for cymem-2.0.6-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4749f220e4c06ec44eb10de13794ff0508cdc4f8eff656cf49cab2cdb3122c0c |
|
MD5 | ced592cb489aa0aace30e3f882d79a2e |
|
BLAKE2b-256 | 17abd9521dc100b51e7c62a09268d918eebda05cdcf69a0010e1531a611b1f60 |
Hashes for cymem-2.0.6-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd52d8a81881804625df88453611175ab7e0099b34f52204da1f6940cf2e83c9 |
|
MD5 | 4814e670ca53d4e1b6156ae2bc53b525 |
|
BLAKE2b-256 | 07bcf8cf9b928bbfe1a716a796d366425ad59fb53d64d8397cf8977d9f3abbbb |
Hashes for cymem-2.0.6-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c59ca1072769cb6c3eade59de9e080ff2cecde0122f7e0ca0dc9ef2ed9240f0e |
|
MD5 | 2b32278152c3fa8d6ce65120e9f2ef49 |
|
BLAKE2b-256 | b59802119adbbae09afcbcdb1419d6af647b3d4db18e9f1b9bd70d829134ed80 |
Hashes for cymem-2.0.6-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7a59cef8f2fa25d12e2c30138f8623acbd43ad2715e730a709e49c5eef8e1b0 |
|
MD5 | 0fad1f702f2b0b31924ff4592e887b80 |
|
BLAKE2b-256 | 919477ca1d1efad56008ee567fe2f23d94ad5be86de32b55ed8a6a15a0782ca8 |
Hashes for cymem-2.0.6-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | af3c01e6b20f9e6c07c7d7cdb7f710e49889d3906c9a3e039546ee6636a34b9a |
|
MD5 | 6b11f0e3cc7a32ca4b86c0775a94607d |
|
BLAKE2b-256 | d3ca0a7d6b7ac314b8b7d62c2c954107133db870c9afe02a3f532ee9b06ad688 |
Hashes for cymem-2.0.6-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 492084aef23ac2ff3da3729e9d36340bc91a96c2dc8c3a82a1926e384ab52412 |
|
MD5 | 7e0b8366a96575d97418efb32e9d5d42 |
|
BLAKE2b-256 | dccbc574231dfc167c8fe03a025d53d8845a2dff72a15a8c4a63ab2c0c4155a7 |
Hashes for cymem-2.0.6-cp36-cp36m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee73a48c5a7e0f2acf6830ddc958ffafd7a614cfb79d14017a459bc7a7145ecd |
|
MD5 | 9c04b12685e5f7a3614ac4feb8847831 |
|
BLAKE2b-256 | 0fe8888b78e089830be4c2ea57038c2485babe347cdc5fa03f542faf93b376ae |
Hashes for cymem-2.0.6-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8e1c18bb00800425576710468299153caad20c64ddb6819d40a6a34e21ee21c |
|
MD5 | f5fa7334071e26313cd4fae678b8a91d |
|
BLAKE2b-256 | e3f77946c60432cc28abab1cae0c78af8c650e7d4b1c479f7f3f5c9cddcc574e |