Fast, re-entrant optimistic lock implemented in Cython
Project description
FastRLock
This is a C-level implementation of a fast, re-entrant, optimistic lock for CPython. It is a drop-in replacement for threading.RLock. FastRLock is implemented in Cython and also provides a C-API for direct use from Cython code via from fastrlock cimport rlock.
Under normal conditions, it is about 10x faster than threading.RLock in Python 2.7 because it avoids all locking unless two or more threads try to acquire it at the same time. Under congestion, it is still about 10% faster than RLock due to being implemented in Cython.
This is mostly equivalent to the revised RLock implementation in Python 3.2, but still faster due to being implemented in Cython. However, in Python 3.4 and later, the threading.RLock implementation in the stdlib tends to be as fast or even faster than the lock provided by this package, when called through the Python API. FastRLock is still faster also on these systems when called through its Cython API from other Cython modules.
It was initially published as a code recipe here: https://code.activestate.com/recipes/577336-fast-re-entrant-optimistic-lock-implemented-in-cyt/
FastRLock has been used and tested in Lupa for several years.
How does it work?
The FastRLock implementation optimises for the non-congested case. It works by exploiting the availability of the GIL. Since it knows that it holds the GIL when the acquire()/release() methods are called, it can safely check the lock for being held by other threads and just count any re-entries as long as it is always the same thread that acquires it. This is a lot faster than actually acquiring the underlying lock.
When a second thread wants to acquire the lock as well, it first checks the lock count and finds out that the lock is already owned. If the underlying lock is also held by another thread already, it then just frees the GIL and asks for acquiring the lock, just like RLock does. If the underlying lock is not held, however, it acquires it immediately and basically hands over the ownership by telling the current owner to free it when it’s done. Then, it falls back to the normal non-owner behaviour that asks for the lock and will eventually acquire it when it gets released. This makes sure that the real lock is only acquired when at least two threads want it.
All of these operations are basically atomic because any thread that modifies the lock state always holds the GIL. Note that the implementation must not call any Python code while handling the lock, as calling into Python may lead to a context switch which hands over the GIL to another thread and thus breaks atomicity. Therefore, the code misuses Cython’s ‘nogil’ annotation to make sure that no Python code slips in accidentally.
How fast is it?
Here are some timings for Python 2.7 for the following scenarios:
five acquire-release cycles (‘lock_unlock’)
five acquire calls followed by five release calls (nested locking, ‘reentrant_lock_unlock’)
a mixed and partly nested sequence of acquire and release calls (‘mixed_lock_unlock’)
five acquire-release cycles that do not block (‘lock_unlock_nonblocking’)
All four are benchmarked for the single threaded case and the multi threaded case with 10 threads. I also tested it with 20 threads only to see that it then takes about twice the time for both versions. Note also that the congested case is substantially slower for both locks, so I only looped 1000x here to get useful timings instead of 100000x for the single threaded case.
Testing threading.RLock sequential (x100000): lock_unlock : 1.408 sec reentrant_lock_unlock : 1.089 sec mixed_lock_unlock : 1.212 sec lock_unlock_nonblocking : 1.415 sec threaded 10T (x1000): lock_unlock : 1.188 sec reentrant_lock_unlock : 1.039 sec mixed_lock_unlock : 1.068 sec lock_unlock_nonblocking : 1.199 sec Testing FastRLock sequential (x100000): lock_unlock : 0.122 sec reentrant_lock_unlock : 0.124 sec mixed_lock_unlock : 0.137 sec lock_unlock_nonblocking : 0.156 sec threaded 10T (x1000): lock_unlock : 0.911 sec reentrant_lock_unlock : 0.938 sec mixed_lock_unlock : 0.953 sec lock_unlock_nonblocking : 0.916 sec
How does it compare to Python 3.2 and later?
Here is the same benchmark run with Py3.2:
Testing threading.RLock sequential (x100000): lock_unlock : 0.134 sec reentrant_lock_unlock : 0.120 sec mixed_lock_unlock : 0.151 sec lock_unlock_nonblocking : 0.177 sec threaded 10T (x1000): lock_unlock : 0.885 sec reentrant_lock_unlock : 0.972 sec mixed_lock_unlock : 0.883 sec lock_unlock_nonblocking : 0.911 sec Testing FastRLock sequential (x100000): lock_unlock : 0.093 sec reentrant_lock_unlock : 0.093 sec mixed_lock_unlock : 0.104 sec lock_unlock_nonblocking : 0.112 sec threaded 10T (x1000): lock_unlock : 0.943 sec reentrant_lock_unlock : 0.871 sec mixed_lock_unlock : 0.920 sec lock_unlock_nonblocking : 0.908 sec
So, in the single-threaded case, the C implementation in Py3.2 is only about 20-50% slower than the Cython implementation here, whereas it is more or less as fast in the congested case.
fastrlock changelog
0.6 (2021-03-21)
Rebuild with Cython 0.29.22 to support Py3.9 and later.
0.5 (2020-06-05)
Rebuild with Cython 0.29.20 to support Py3.8 and later.
0.4 (2018-08-24)
Rebuild with Cython 0.28.5.
Linux wheels are faster through profile guided optimisation.
Add missing file to sdist. (patch by Mark Harfouche, Github issue #5)
0.3 (2017-08-10)
improve cimport support of C-API (patch by Naotoshi Seo, Github issue #3)
provide fastrlock.__version__
0.2 (2017-08-09)
add missing readme file to sdist
0.1 (2017-06-04)
initial release
Project details
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 fastrlock-0.6-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6bd625f8adfece45109b3fba8156a468d81862e7c5f24a9c09edf8413e9a65b0 |
|
MD5 | d6c020df2e4d68343c0d0d3455840adc |
|
BLAKE2b-256 | fff25a52d8612de8d175c37b41230af717fc8ddffcdc1c3f06669edc9b68bf6f |
Hashes for fastrlock-0.6-cp39-cp39-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36b57043b138bc60cbfa8061bd24ca083c1272976996ae9d69402797254bc38e |
|
MD5 | 613afd45f465ac456ac2e246f7a50ce5 |
|
BLAKE2b-256 | 667059544922981346f01dd3c25ada7c78437ef71caad0f412fb9c5e2afade84 |
Hashes for fastrlock-0.6-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e7a163aa1d78019ee377774ceec6a6037dd7855aa72451bbd5d9f6d38f6ab02 |
|
MD5 | bfc2a664ca880de0363a27918835fcb5 |
|
BLAKE2b-256 | 4f4e4c2556e88815ea020aeefc3cb5fc55356a6125ec23fc54dbaac95fb14fe2 |
Hashes for fastrlock-0.6-cp39-cp39-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5955a03b2a458f8d14cfb352610e88c4277e61e21ef2463ff998dce7d3ffd64 |
|
MD5 | 3769d962d14e57b15d1cf5ea677b8e50 |
|
BLAKE2b-256 | e1b09ccd0f256fd28924a1cd149f1edd0569cdb69ecd3e69f08d182d28e876b4 |
Hashes for fastrlock-0.6-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 69d791ce2024b49fc11a5411934d0b7f6d53237daeec5b69a654d0e6dd872e14 |
|
MD5 | 0d4fd6f0aa1ddbc46caab28b95c8546d |
|
BLAKE2b-256 | 4d0a00c53cabd0b90539cedd46781f0074e181033b41a5151219358a9dffc089 |
Hashes for fastrlock-0.6-cp38-cp38-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 20b4d7ae8b23ac7ac9fed2e55141bc7ed7c45ee126a76b79c100fbc2f25ca4b6 |
|
MD5 | 9df24234de5b4a3cf2a484d2c3ec7ff4 |
|
BLAKE2b-256 | ab3837985f1f8ed348ed77b599e5c89b0e12fc9339910af6caa21b9b07b7d3bc |
Hashes for fastrlock-0.6-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c78340c4d0ab84893bebd1642fa4a498d09365c607e35db5be2e7c46452fd38b |
|
MD5 | bb8c0d62832c66fae9bc04feb3748f16 |
|
BLAKE2b-256 | b89372084c0f67cb9f306d2673176cf299a4c66d3f14e3e9d9d129d9fd6ec9d2 |
Hashes for fastrlock-0.6-cp38-cp38-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9dc7fee1ac2fd443166bb13dd4a777259ead5431b844ad7ce7f696c4276d204b |
|
MD5 | 9a2f4e248c26476ca0a1e7de238070a4 |
|
BLAKE2b-256 | d25fcb3b3126873bf009273356cec8e9703771efeb5041ef4a5e89d0f6bb43cb |
Hashes for fastrlock-0.6-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d841e1c18d8c43b31eb9fc4763d272c92d3060d0bcd379a74bc29d9fe45264f |
|
MD5 | 2bbef6c4bad0cfac11ab6b8099bbb1aa |
|
BLAKE2b-256 | ab13e4ebfb2a389cf8643f1d38c35906b1db351c8e5141b1e70d9d19c15c5b9b |
Hashes for fastrlock-0.6-cp37-cp37m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2dcb0a0aa9fa9558091e1cd550747494ea39c44a54fbc89dc2bc5f64a31c308 |
|
MD5 | 2f6bf76400f3ebc1fbf7784b69ff4edb |
|
BLAKE2b-256 | 247fe344bcc27d2233bbec2bf2b83380358ec7122f16c80affe7846f224893ce |
Hashes for fastrlock-0.6-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a20fd9f2995b72ff60b86a599b247ae141be9520013e525428c62839fdf679d |
|
MD5 | 883dd1910e9a50b86f4cbdb0518d02a7 |
|
BLAKE2b-256 | f720e7301f50c438a20d27cca39edb960700c3a015b6e81f01e608a573699047 |
Hashes for fastrlock-0.6-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a8f26d885890f92f34bdba19abe861f18674bcdfc30a2972651cc072c09bbd4 |
|
MD5 | 7baedc39323c1c51ce37467f010b808b |
|
BLAKE2b-256 | 2927ebae5522e1e6195dbee5d3c2b918640c412293137c24a0983a0c451d1647 |
Hashes for fastrlock-0.6-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48309221b6df5b74ba3fb1d92e19b5e0a5b56b89773d40849062e8e868c9379c |
|
MD5 | c6c833289d00e9ea92c268c9d37c932c |
|
BLAKE2b-256 | 5c7022f960f6bdcfde21a0d2eb0eb7ae5706bed5dc8c00f400f3f8ff77d29aca |
Hashes for fastrlock-0.6-cp36-cp36m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9ef82cb3bab5f5713463ad45bf77c244f9dfb4a2974c689eeb9e2bbd0cb55278 |
|
MD5 | 76f5b8c4f023073150bac02ff391ad19 |
|
BLAKE2b-256 | e0186ec021bd2a7dba1f6de5f3bbbfcd1d0cb9d9e12f8ba4b30cceb186113eec |
Hashes for fastrlock-0.6-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a27259e4ab34a09391db2382359f55ff24713d5702fc83e051a3bf3e9e7eaf97 |
|
MD5 | a4791be41f8e8677d5d8432f69ef1f1b |
|
BLAKE2b-256 | 8ed17c7917271bebbcc88f2adff976d9f58d25648e5614e2724ca188e42f76b7 |
Hashes for fastrlock-0.6-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | df28002e7ded5749ede0cd2739a71b51767f415e458044d0beece3d3a72d3666 |
|
MD5 | 59970e41e6325b4a87143c6a60a53db1 |
|
BLAKE2b-256 | 9e899f9a2e0323a190d2da67b6796cd8c8a21bf8bb953f023f9df26c598b2a72 |
Hashes for fastrlock-0.6-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8197da446ba2182bc3e399ab9144055acb9f772f1918e1d75ca8a16da947f40a |
|
MD5 | ab9c6a31c5e0c4ac0af7c4424d9e5a9e |
|
BLAKE2b-256 | a8ec1e2bf0752a819813981d6291f07c238786cb995dfe08079dc30d991a0c05 |
Hashes for fastrlock-0.6-cp35-cp35m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87ca5b0a57d48ea8a88906126e3c2892dafb3c53d704abf4428a75aab7e4e17b |
|
MD5 | 7c39a7e7da947fb273fef654331a159d |
|
BLAKE2b-256 | b6bd39ae66da25bb40af17d6c7d1652752a072242052a4c1fa049c0e09de96e5 |
Hashes for fastrlock-0.6-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38054112b22f349f5df7a69d8823a0d35f9cf8e418df366f7f429834fd35de44 |
|
MD5 | d59b0dfd429247a0af83d79cac7e709f |
|
BLAKE2b-256 | 02df7de5902934365b6e175b965563ed4d5a26bb18888feed90868f2b7bb3368 |
Hashes for fastrlock-0.6-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a6935a4805f6b52358b7bc1e5ef9278ca5fb487748bfc5d44c5206bcdb20917 |
|
MD5 | c294a41fcbce3362ae0dc0b098c492e8 |
|
BLAKE2b-256 | e30f20ff63e2351b97e91ec03b0e585d360730129b9b7bc15b97763b77e5d070 |
Hashes for fastrlock-0.6-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cce3198c1e250c62bf9078fdaedc95c45f08420eaa7f42d423b68b3e2aa8b1f7 |
|
MD5 | afcf6a8d5df4ad5321f0cd7fb63b6505 |
|
BLAKE2b-256 | 467f562823d7e74d3fc16aa9f2223f0cd2dfa4b077d9bd5c3ef3008be6034a1a |
Hashes for fastrlock-0.6-cp27-cp27mu-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f899b54d157deb800c3cb4c8a6d20358bc6d5f5c72711ea98e721971c5cfb8b8 |
|
MD5 | 76e43b086d60e2a61c28cf1b83e10176 |
|
BLAKE2b-256 | e34ccf4d7f34fd11023c561c454c64ec45e6ce416e09edf1f3a3d23920a45d5d |
Hashes for fastrlock-0.6-cp27-cp27m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c55095309d53906c808186fd4f0cfa17da28165b7c26c4d154b078da6be690f |
|
MD5 | 5d38986551f2c9ea2208989f1f01306b |
|
BLAKE2b-256 | 9d56c5d6c58d92c9f6beaf5fae6857a50146e54f81958500c417b167358d8cb6 |
Hashes for fastrlock-0.6-cp27-cp27m-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd121ac603dc9c185a7a3e55490f579f841b7ced1008d289acdd18ee48e038f7 |
|
MD5 | 4d9b047a0c6549436fcc13c5df516157 |
|
BLAKE2b-256 | 623ce47b6793eab54ec32845ed6b4dc83f4a90be2bd426684f7d5b962bec0fb8 |
Hashes for fastrlock-0.6-cp27-cp27m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 39301f99493e060a9cb8d6e35464f3a93354b34a57c9618bfdd00694c396f21c |
|
MD5 | 8e793465c2c73a7bab88073978de78bf |
|
BLAKE2b-256 | d441dba9ddaf0a5e0947170156fe782d20cf5cbad8eb404b2076f84c702b285d |
Hashes for fastrlock-0.6-cp27-cp27m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c3c41102cd8ebdb7b2c23d59e3adfbaf2b3ee6d40478edb79dfbb8e6dc699b1 |
|
MD5 | 8549ef141add80b0707692171df1140b |
|
BLAKE2b-256 | c98c2959388ca9db4230104517d26074dfb2e1aea93d51c798def2b6ee2b357a |