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 written in Cython. Under normal conditions, it is about 10x faster than threading.RLock 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.
It was initially published as a code recipe here: https://code.activestate.com/recipes/577336-fast-re-entrant-optimistic-lock-implemented-in-cyt/
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:
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’) and
five acquire-release cycles that do not block.
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. 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.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.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8025d177f1c427e6655c42a45b273c91d925f845f46196f5e5e6bbd2fb0f2520 |
|
MD5 | b55ebb639e704b2e6c40e373600207d9 |
|
BLAKE2b-256 | 43bf20b430784acf71623e1aa41ab1543f41840aab195e999fefe25df6dea62a |
Hashes for fastrlock-0.1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a02626874581090d18a58fa8cf84b1974890179d9da4463e1e12855d2faf529 |
|
MD5 | 01027cae7b25fc89654feac9bf8bbc63 |
|
BLAKE2b-256 | 1a5c37311e3ee883695b6c315a76b8b7bcc86ab2da85c21b103607715884737e |
Hashes for fastrlock-0.1-cp34-cp34m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3558adb47a0829773404b5632e89a08d7f1d4af6c004317a54ee02ca3561359a |
|
MD5 | 49b7ae1a2c8e543976d6a4fea4528cf8 |
|
BLAKE2b-256 | b20aea044cbdba6e3fd7e7d630d91078bffaa6bad3205adaedb40f36a73203ab |
Hashes for fastrlock-0.1-cp33-cp33m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 547dd178d4e3b3bc326ccb83688ffaa6313286b1010683bc6824e486121e305e |
|
MD5 | 7ba8b707609aaf530f35bf7e47062065 |
|
BLAKE2b-256 | 60988e37aa1517a269a52332ce46a5fde86ea65fdcc544414cf18c08a09aa3c6 |
Hashes for fastrlock-0.1-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8909354c952c90ecab88f1cd06541a28d9e886b21b439a866bc81ae1322d8de |
|
MD5 | 8bd35c25d82fcad7e02107c0cceff168 |
|
BLAKE2b-256 | af4c8f380ba848a9be13c129046af76229f917451f2670496e7132003fe7063e |
Hashes for fastrlock-0.1-cp27-cp27m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9a7a5260a54a9f8f26ebb8b8c9bd7c76332149d481d0f5bb1c2833572e56ffb |
|
MD5 | 6e32f77f36e72c294f0eb94a2c69b155 |
|
BLAKE2b-256 | b51a55daec029243494ce12de8fa75014fd37f168f5bd0d1f77738df52f4bdb7 |