Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Free GIL when running check_bytes_arrays_within_dist #27

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions hexhamming/python_hexhamming.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
26 changes: 26 additions & 0 deletions test/test_hexhamming.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Preview

Copilot AI Nov 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The print statement is unnecessary for the test and could be removed to make the test cleaner.

Suggested change
print("Running other task")
for _ in range(5):
time.sleep(0.1)

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
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()
Loading