Skip to main content

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:

  1. five acquire-release cycles (‘lock_unlock’)

  2. five acquire calls followed by five release calls (nested locking, ‘reentrant_lock_unlock’)

  3. a mixed and partly nested sequence of acquire and release calls (‘mixed_lock_unlock’)

  4. 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

fastrlock-0.6.tar.gz (53.3 kB view hashes)

Uploaded Source

Built Distributions

fastrlock-0.6-cp39-cp39-win_amd64.whl (27.3 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

fastrlock-0.6-cp39-cp39-win32.whl (24.1 kB view hashes)

Uploaded CPython 3.9 Windows x86

fastrlock-0.6-cp39-cp39-manylinux1_x86_64.whl (42.4 kB view hashes)

Uploaded CPython 3.9

fastrlock-0.6-cp39-cp39-manylinux1_i686.whl (42.6 kB view hashes)

Uploaded CPython 3.9

fastrlock-0.6-cp38-cp38-win_amd64.whl (27.5 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

fastrlock-0.6-cp38-cp38-win32.whl (24.5 kB view hashes)

Uploaded CPython 3.8 Windows x86

fastrlock-0.6-cp38-cp38-manylinux1_x86_64.whl (42.3 kB view hashes)

Uploaded CPython 3.8

fastrlock-0.6-cp38-cp38-manylinux1_i686.whl (42.5 kB view hashes)

Uploaded CPython 3.8

fastrlock-0.6-cp37-cp37m-win_amd64.whl (27.1 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

fastrlock-0.6-cp37-cp37m-win32.whl (24.3 kB view hashes)

Uploaded CPython 3.7m Windows x86

fastrlock-0.6-cp37-cp37m-manylinux1_x86_64.whl (39.3 kB view hashes)

Uploaded CPython 3.7m

fastrlock-0.6-cp37-cp37m-manylinux1_i686.whl (39.4 kB view hashes)

Uploaded CPython 3.7m

fastrlock-0.6-cp36-cp36m-win_amd64.whl (27.2 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

fastrlock-0.6-cp36-cp36m-win32.whl (24.3 kB view hashes)

Uploaded CPython 3.6m Windows x86

fastrlock-0.6-cp36-cp36m-manylinux1_x86_64.whl (39.5 kB view hashes)

Uploaded CPython 3.6m

fastrlock-0.6-cp36-cp36m-manylinux1_i686.whl (39.6 kB view hashes)

Uploaded CPython 3.6m

fastrlock-0.6-cp35-cp35m-win_amd64.whl (27.2 kB view hashes)

Uploaded CPython 3.5m Windows x86-64

fastrlock-0.6-cp35-cp35m-win32.whl (24.2 kB view hashes)

Uploaded CPython 3.5m Windows x86

fastrlock-0.6-cp35-cp35m-manylinux1_x86_64.whl (39.3 kB view hashes)

Uploaded CPython 3.5m

fastrlock-0.6-cp35-cp35m-manylinux1_i686.whl (39.4 kB view hashes)

Uploaded CPython 3.5m

fastrlock-0.6-cp27-cp27mu-manylinux1_x86_64.whl (32.1 kB view hashes)

Uploaded CPython 2.7mu

fastrlock-0.6-cp27-cp27mu-manylinux1_i686.whl (30.6 kB view hashes)

Uploaded CPython 2.7mu

fastrlock-0.6-cp27-cp27m-win_amd64.whl (25.4 kB view hashes)

Uploaded CPython 2.7m Windows x86-64

fastrlock-0.6-cp27-cp27m-win32.whl (22.9 kB view hashes)

Uploaded CPython 2.7m Windows x86

fastrlock-0.6-cp27-cp27m-manylinux1_x86_64.whl (32.1 kB view hashes)

Uploaded CPython 2.7m

fastrlock-0.6-cp27-cp27m-manylinux1_i686.whl (30.6 kB view hashes)

Uploaded CPython 2.7m

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page