From 5d1644cac7125b728d3157e9bb1cfdb09de46e9a Mon Sep 17 00:00:00 2001 From: Michael Recachinas Date: Sat, 30 Nov 2024 10:44:28 -0500 Subject: [PATCH 1/2] Free GIL when running check_bytes_arrays_within_dist Fixes #23 Add GIL release in `check_bytes_arrays_within_dist` function. - Add `Py_BEGIN_ALLOW_THREADS` macro before the loop in `check_bytes_arrays_within_dist_wrapper` function in `hexhamming/python_hexhamming.cc`. - Add `Py_END_ALLOW_THREADS` macro after the loop in `check_bytes_arrays_within_dist_wrapper` function in `hexhamming/python_hexhamming.cc`. - Remove the in-loop return statement in `check_bytes_arrays_within_dist_wrapper` function in `hexhamming/python_hexhamming.cc`. - Add unit test `test_check_bytes_arrays_within_dist_gil` in `test/test_hexhamming.py` to verify the functionality of `check_bytes_arrays_within_dist` function with GIL released. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/mrecachinas/hexhamming/issues/23?shareId=XXXX-XXXX-XXXX-XXXX). --- hexhamming/python_hexhamming.cc | 15 ++++++++++++--- test/test_hexhamming.py | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/hexhamming/python_hexhamming.cc b/hexhamming/python_hexhamming.cc index 89d1e6a..babd462 100644 --- a/hexhamming/python_hexhamming.cc +++ b/hexhamming/python_hexhamming.cc @@ -272,15 +272,24 @@ static PyObject * check_bytes_arrays_within_dist_wrapper(PyObject *self, PyObjec return NULL; } + int return_value = -1; + + Py_BEGIN_ALLOW_THREADS + int res; uint64_t number_of_elements = big_array_size / small_array_size; uint8_t* pBig = big_array; for (uint64_t i = 0; i < number_of_elements; i++, pBig += small_array_size) { res = (int)ptr__hamming_distance_bytes(pBig, small_array, small_array_size, max_dist); - if (res == 1) - return Py_BuildValue("i", i); + if (res == 1) { + return_value = i; + break; } - return Py_BuildValue("i", -1); + } + + Py_END_ALLOW_THREADS + + return Py_BuildValue("i", return_value); } /** diff --git a/test/test_hexhamming.py b/test/test_hexhamming.py index e9cd428..d41be6b 100644 --- a/test/test_hexhamming.py +++ b/test/test_hexhamming.py @@ -309,3 +309,29 @@ def test_check_hexstrings_within_dist_bench(benchmark): ) def test_check_bytes_arrays_within_dist_bench(benchmark, bytes1, bytes2, max_dist): benchmark(check_bytes_arrays_within_dist, bytes1, bytes2, max_dist) + + +def test_check_bytes_arrays_within_dist_gil(): + import threading + import time + + def run_check(): + big_array = b"\x00" * 16 * 1000000 + small_array = b"\x00" * 16 + max_dist = 0 + result = check_bytes_arrays_within_dist(big_array, small_array, max_dist) + assert result == 0 + + def run_other_task(): + for _ in range(5): + print("Running other task") + time.sleep(0.1) + + check_thread = threading.Thread(target=run_check) + other_task_thread = threading.Thread(target=run_other_task) + + check_thread.start() + other_task_thread.start() + + check_thread.join() + other_task_thread.join() From fdfc9ff15ac2893782851e0c179cf6caf6c6fa5e Mon Sep 17 00:00:00 2001 From: Michael Recachinas Date: Sat, 6 Sep 2025 16:20:52 -0400 Subject: [PATCH 2/2] Update test/test_hexhamming.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- test/test_hexhamming.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/test_hexhamming.py b/test/test_hexhamming.py index d41be6b..46158bb 100644 --- a/test/test_hexhamming.py +++ b/test/test_hexhamming.py @@ -324,7 +324,8 @@ def run_check(): def run_other_task(): for _ in range(5): - print("Running other task") + for _ in range(5): + time.sleep(0.1) time.sleep(0.1) check_thread = threading.Thread(target=run_check)