Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bcd5cbe

Browse files
committed
Issue #4751: hashlib now releases the GIL when hashing large buffers
(with a hardwired threshold of 2048 bytes), allowing better parallelization on multi-CPU systems. Contributed by Lukas Lueg (ebfe) and Victor Stinner.
1 parent 5bad41e commit bcd5cbe

4 files changed

Lines changed: 176 additions & 91 deletions

File tree

Doc/library/hashlib.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ to the buffer interface (normally :class:`bytes` objects) using the
3535
concatenation of the data fed to it so far using the :meth:`digest` or
3636
:meth:`hexdigest` methods.
3737

38+
.. note::
39+
40+
For better multithreading performance, the Python GIL is released for
41+
strings of more than 2047 bytes at object creation or on update.
42+
3843
.. note::
3944

4045
Feeding string objects is to :meth:`update` is not supported, as hashes work

Lib/test/test_hashlib.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,19 @@ def test_case_sha512_3(self):
198198
"e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"+
199199
"de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b")
200200

201+
def test_gil(self):
202+
# Check things work fine with an input larger than the size required
203+
# for multithreaded operation (which is hardwired to 2048).
204+
gil_minsize = 2048
205+
206+
m = hashlib.md5()
207+
m.update(b'1')
208+
m.update(b'#' * gil_minsize)
209+
m.update(b'1')
210+
self.assertEquals(m.hexdigest(), 'cb1e1a2cbc80be75e19935d621fb9b21')
211+
212+
m = hashlib.md5(b'x' * gil_minsize)
213+
self.assertEquals(m.hexdigest(), 'cfb767f225d58469c5de3632a8803958')
201214

202215
def test_main():
203216
support.run_unittest(HashLibTestCase)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ C-API
256256
Extension Modules
257257
-----------------
258258

259+
- Issue #4751: hashlib now releases the GIL when hashing large buffers
260+
(with a hardwired threshold of 2048 bytes), allowing better parallelization
261+
on multi-CPU systems. Contributed by Lukas Lueg (ebfe) and Victor Stinner.
262+
259263
- Issue #4051: Prevent conflict of UNICODE macros in cPickle.
260264

261265
- Issue #4738: Each zlib object now has a separate lock, allowing to compress

0 commit comments

Comments
 (0)