osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Python-Dev] Const access to CPython objects outside of GIL?


Hi all,

one of our Python projects calls for pretty heavy, low-level optimizations.

We went down the rabbit hole and determined that having access to
PyList_GET_ITEM(list), PyInt_AS_LONG(int) and PyDict_GetItem(dict, unicode)
on Python objects **outside of GIL** might be a good-enough solution. The
Python objects in question are guaranteed to live and not be mutated
externally in any way. They're "frozen" and read-only.

Under what conditions is it OK to call these 3 functions on such objects?

More generally, what is the CPython 2.7/3.5 contract regarding (lack of)
object mutation, and the need for reference counting and synchronization
via GIL?

Which C API functions are safe to call on "const" objects?

Obviously releasing GIL and then calling C API is hacky, but from initial
experiments, it seems to work (see
https://stackoverflow.com/questions/51351609/can-i-const-access-cpython-objects-without-gil).
But I'm wondering if there's a more formal contract around this behaviour.

Cheers,
Radim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180717/75fcd3e1/attachment.html>