This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: GIL Improvement
Type: performance Stage: resolved
Components: C API Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, eric.smith, pitrou, shreyanavigyan, vstinner
Priority: normal Keywords:

Created on 2021-05-14 12:03 by shreyanavigyan, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
benchmark.py shreyanavigyan, 2021-05-14 12:42 normal_benchmark
pyperf_benchmark.py shreyanavigyan, 2021-05-14 13:26 pyperf benchmark
Messages (17)
msg393642 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-14 12:03
Today while working on an attempt to improve the GIL (either by modifying it or removing it) I noticed that in destroy_gil function first we unlock the mutex and then we set the gil->locked to -1 using _Py_atomic_store_explicit. Therefore the cycle is, "Unlock -> Atomic_Set_Value" which closely evaluates to, "Unlock -> Lock -> Set_Value -> Unlock"


I tweaked around a little and when I changed the cycle to, "Atomic_Set_Value -> Unlock" I noticed by running David Beazley's famous benchmarks that there's an increase in speed by 0.22 or something like that. I don't know if it's because of CPU instability or because of the tweak but it looks like an improvement. Should this patch be applied to cpython?
msg393644 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-14 12:17
Instability is more likely. But I'll run the benchmark once more and see what is happening.
msg393646 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2021-05-14 12:21
Does Python's unit test suite pass?
msg393649 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-14 12:22
Forgot to do that. I'll report back with the test suite result.
msg393651 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-05-14 12:33
"there's an increase in speed by 0.22 or something like that"

0.22 what? Seconds? Percent? A factor of 0.22?

What are you measuring, and how are you measuring it?
msg393652 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-14 12:38
I took the measurement like this. If it ran in 1.987777 secs then the new patch would theoretically measure 1.967777 (original - 0.22). The patch isn't very useful but it can sometimes be faster than the present code especially in debug mode.
msg393653 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2021-05-14 12:42
The information is not very helpful to understand what you are doing. Please provide your system specs (arch, platform, CPU spec, memory), the commands you used to compile Python and the commands you used to get the numbers
msg393654 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-14 12:42
Test suite passed and also I'm attaching the benchmark I used to measure. (This is the benchmark used by David Beazley in one of his famous GIL blog posts and talks.)
msg393655 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-05-14 12:42
"If it ran in 1.987777 secs then the new patch would theoretically measure 1.967777 (original - 0.22)"

That difference is 0.02 seconds, or about 1%, correct?

I'm just trying to understand what we're measuring here.
msg393656 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-14 12:46
> "That difference is 0.02 seconds, or about 1%, correct?"

I mistook 1.987777 as 1.88 in my calculation. Sorry for that.

> The information is not very helpful to understand what you are doing. Please provide your system specs (arch, platform, CPU spec, memory), the commands you used to compile Python and the commands you used to get the numbers

My System is x86_64 Windows with 4 cores and 8 gigs of memory. I also tried it with my WSL1.
msg393657 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2021-05-14 12:47
The benchmark code has multiple issues. For example it uses a bad clock sources, time.time(). Could you please rewrite the benchmark to use https://pypi.org/project/pyperf/ ?
msg393658 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-14 12:57
Sure
msg393660 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-14 13:26
I attached the pyperf benchmark also.
msg393663 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-05-14 14:29
> I attached the pyperf benchmark also.

Can you please provide pyperf_benchmark.py results?
msg393664 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-14 14:49
pyperf results -

Mean +- std dev: 455 ms +- 13 ms

Looks a little bit faster than current 3.11, 3.10 and 3.9 versions.
msg393665 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-05-14 14:53
> Mean +- std dev: 455 ms +- 13 ms

A single benchmark is useless without a reference. Please run the benchmark on unpatched python, then run it again on a patched Python, and compare results. You can store benchmark results using -o option. Example:

# Unpatched python
python pyperf_benchmark.py -o ref.json
# Patched python
python pyperf_benchmark.py -o new_gil.json
# Compare
python -m pyperf compare_to ref.json new_gil.json
msg393670 - (view) Author: Shreyan Avigyan (shreyanavigyan) * Date: 2021-05-14 15:52
I suspected right. This was just CPU instability. In fact the patch is 1x-2x slower as reported by pyperf. Therefore I'm closing this issue. 

Though I'm working on another "yet to be failed attempt" to remove or rewrite the GIL to improve performance. Of course, any help would be appreciated there.

Repo - https://github.com/shreyanavigyan/pygilremove
History
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88296
2021-05-14 15:53:17shreyanavigyansetresolution: wont fix
2021-05-14 15:52:49shreyanavigyansetstatus: open -> closed

messages: + msg393670
stage: resolved
2021-05-14 14:53:53vstinnersetmessages: + msg393665
2021-05-14 14:49:35shreyanavigyansetmessages: + msg393664
2021-05-14 14:29:52vstinnersetmessages: + msg393663
2021-05-14 13:26:59shreyanavigyansetfiles: + pyperf_benchmark.py

messages: + msg393660
2021-05-14 12:57:22shreyanavigyansetmessages: + msg393658
2021-05-14 12:47:00christian.heimessetmessages: + msg393657
2021-05-14 12:46:28shreyanavigyansetmessages: + msg393656
2021-05-14 12:42:17eric.smithsetmessages: + msg393655
2021-05-14 12:42:17shreyanavigyansetfiles: + benchmark.py

messages: + msg393654
2021-05-14 12:42:12christian.heimessetmessages: + msg393653
2021-05-14 12:38:48shreyanavigyansetmessages: + msg393652
2021-05-14 12:33:49eric.smithsetnosy: + eric.smith
messages: + msg393651
2021-05-14 12:22:49shreyanavigyansetmessages: + msg393649
2021-05-14 12:21:16christian.heimessetnosy: + christian.heimes, vstinner, pitrou
messages: + msg393646
2021-05-14 12:17:17shreyanavigyansetmessages: + msg393644
2021-05-14 12:03:46shreyanavigyancreate