Skip to content

gh-126220: Adapt _lsprof to Argument Clinic - #126233

Merged
sobolevn merged 10 commits into
python:mainfrom
sobolevn:issue-126220
Nov 4, 2024
Merged

gh-126220: Adapt _lsprof to Argument Clinic#126233
sobolevn merged 10 commits into
python:mainfrom
sobolevn:issue-126220

Conversation

@sobolevn

@sobolevnsobolevn commented Oct 31, 2024

Copy link
Copy Markdown
Member

I decided to go with the AC, because:

  1. It is supported in this module
  2. It solves the problem
  3. It fixes multiple signatures

But, I believe it will be slower. If we really want to preserve speed in this case, I can add manual size checks for args.

Refs #103534

@ZeroIntensityZeroIntensity left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. I'm always happy with fixes via AC, because they're much more scalable.

@gaogaotiantian

Copy link
Copy Markdown
Member

What's the perf impact here?

@sobolevn

Copy link
Copy Markdown
MemberAuthor

@gaogaotiantian I've never had experience with profiling cProfile. Do you have any links / suggestions on how to do that?

@gaogaotiantian

Copy link
Copy Markdown
Member

Just do a quick fib() with cprofile and compare it with the previous implementation would do. Maybe also with one without cprofile.

@sobolevn

Copy link
Copy Markdown
MemberAuthor

@gaogaotiantian maybe I did something wrong, but looks like we also got a speedup from my numbers 😅 (I have little idea about how cProfile works).

# Test code: ex.pydeftest():
deffib():
start, nextn=0, 1whileTrue:
yieldstartstart, nextn=nextn, start+nextngen=fib()
for_inrange(100000):
next(gen)
importcProfilecProfile.run(test.__code__)

Running it on main:

» ./python.exe ex.py
200003 function calls in 0.188 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.017 0.017 0.188 0.188 ex.py:1(test)
100000 0.147 0.000 0.147 0.000 ex.py:2(fib)
1 0.000 0.000 0.188 0.188 {built-in method builtins.exec}
100000 0.024 0.000 0.171 0.000 {built-in method builtins.next}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

Running on this PR:

» ./python.exe ex.py 200003 function calls in 0.185 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.016 0.016 0.185 0.185 ex.py:1(test)
100000 0.145 0.000 0.145 0.000 ex.py:2(fib)
1 0.000 0.000 0.185 0.185 {built-in method builtins.exec}
100000 0.024 0.000 0.168 0.000 {built-in method builtins.next}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

Am I missing something?

@gaogaotiantian

gaogaotiantian commented Oct 31, 2024

Copy link
Copy Markdown
Member

Sorry I meant a recursive fib().

importtimedeffib(n):
ifn<=1:
return1returnfib(n-1) +fib(n-2)
start=time.time()
# enable profilerfib(24)
# disable profilerprint(time.time() -start)

We need to consider the worst case for cprofile, which is when the code has a lot of function calls. We need to know the overhead of the profiler, and how that is compared with before.

You can also refer to #103533, there's a profiling code listed. That might give some extra information.

Moving to sys.monitoring gave us a 20%+ improvement on overhead, just want to check what's the regression with clinic and whether it's acceptable. Profiler is a bit sensitive to performance because the more overhead there is, the less useful it is.

@sobolevn

Copy link
Copy Markdown
MemberAuthor

It still gives me a perf boost for some reason 🤔

importtimeimportcProfileprofiler=cProfile.Profile()
deffib(n):
ifn<=1:
return1returnfib(n-1) +fib(n-2)
start=time.perf_counter()
profiler.enable()
fib(30) # 100thprofiler.disable()
print(time.perf_counter() -start)
# Old: 0.9349823750017094# New: 0.9253051670020795# 1% faster 

@gaogaotiantian

Copy link
Copy Markdown
Member

Is the boost stable(reproducible)?

@sobolevn

sobolevn commented Oct 31, 2024

Copy link
Copy Markdown
MemberAuthor

Yes, it is pretty much the same on m2 macos:

(.venv) ~/Desktop/cpython2 main ✗ » ./python.exe ex.py 0.9349823750017094
(.venv) ~/Desktop/cpython2 main ✗ » ./python.exe ex.py
0.9357958749969839
(.venv) ~/Desktop/cpython2 main ✗ » ./python.exe ex.py
0.9352227920026053
(.venv) ~/Desktop/cpython2 main ✗ » ./python.exe ex.py
0.924946416002058
(.venv) ~/Desktop/cpython2 main ✗ » ./python.exe ex.py
0.926337541997782
(.venv) ~/Desktop/cpython2 main ✗ » ./python.exe ex.py
0.9347994170000311
(.venv) ~/Desktop/cpython2 issue-126220 ✗ » ./python.exe ex.py
0.9370929580036318
(.venv) ~/Desktop/cpython2 issue-126220 ✗ » ./python.exe ex.py
0.9262957079990883
(.venv) ~/Desktop/cpython2 issue-126220 ✗ » ./python.exe ex.py
0.9328266249940498
(.venv) ~/Desktop/cpython2 issue-126220 ✗ » ./python.exe ex.py
0.9382220000011148
(.venv) ~/Desktop/cpython2 issue-126220 ✗ » ./python.exe ex.py
0.9285237919984502
(.venv) ~/Desktop/cpython2 issue-126220 ✗ » ./python.exe ex.py
0.9296539580027456

@gaogaotiantian

Copy link
Copy Markdown
Member

Yeah from the result I don't think there's an observable boost. On the other hand, that's good, because no observable regression either.

@gaogaotiantiangaogaotiantian added needs backport to 3.12 only security fixes needs backport to 3.13 bugs and security fixes labels Oct 31, 2024

@gaogaotiantiangaogaotiantian left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just a side note - this needs to be backported to 3.12 as well :) I added the tags so you should be able to just merge it.

@sobolevn

Copy link
Copy Markdown
MemberAuthor

@gaogaotiantian sorry, I forgot to highlight it initially. Please, double check params names that I introduced. Are they correct?

@gaogaotiantian

Copy link
Copy Markdown
Member

Actually, I have some doubts about the changes for functions that are not crashing. This is a bug fix, which would be backported. I don't think we should mix in the code polish to the PR. Even though I think changing this in main would be fine, I would prefer having only the crash fix (could be with AC) in one PR, and the rest in the other. One thing that bothers me immediately is that I can't quickly convince myself the changes to __init__ is purely equivalent.

As for the argument name, the obj in the callbacks should be either unused or instruction_offset(which is what it is).

@gaogaotiantian

Copy link
Copy Markdown
Member

Can we split this PR into 2? One with the changes to only the 4 callbacks, and the other with all the other methods?

@sobolevn

Copy link
Copy Markdown
MemberAuthor

Fair enough, I would split this PR later! 👍
Thank you!

@gaogaotiantiangaogaotiantian left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@sobolevn

Copy link
Copy Markdown
MemberAuthor

@erlend-aasland maybe you would be interested in double checking the AC part? :)

@sobolevnsobolevn changed the title gh-126220: Fix crash on calls to _lsprof.Profiler methods with 0 argsgh-126220: Convert _lsprof to ACNov 1, 2024
Comment threadModules/_lsprof.c Outdated
Comment threadModules/_lsprof.c Outdated
Comment threadModules/_lsprof.c Outdated
Comment threadModules/_lsprof.c Outdated
@erlend-aaslanderlend-aasland changed the title gh-126220: Convert _lsprof to ACgh-126220: Adapt _lsprof to Argument ClinicNov 1, 2024
sobolevnand others added 3 commits November 2, 2024 08:30
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Comment threadModules/_lsprof.c Outdated
Comment threadModules/_lsprof.c Outdated
@sobolevn

Copy link
Copy Markdown
MemberAuthor

Done!

@sobolevn
sobolevn merged commit c806cd5 into python:mainNov 4, 2024
@miss-islington-app

Copy link
Copy Markdown

Thanks @sobolevn for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

@sobolevn

Copy link
Copy Markdown
MemberAuthor

Thanks everyone!

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 4, 2024
(cherry picked from commit c806cd5)
Co-authored-by: sobolevn <mail@sobolevn.me>
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
@miss-islington-app

Copy link
Copy Markdown

Sorry, @sobolevn, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker c806cd5af677c385470001efc68da38a32919196 3.12

@bedevere-app

Copy link
Copy Markdown

GH-126402 is a backport of this pull request to the 3.13 branch.

@bedevere-appbedevere-appBot removed the needs backport to 3.13 bugs and security fixes label Nov 4, 2024
@sobolevn

Copy link
Copy Markdown
MemberAuthor

Oups, backports are not needed anymore. Closing them.

@sobolevnsobolevn removed the needs backport to 3.12 only security fixes label Nov 4, 2024
picnixz pushed a commit to picnixz/cpython that referenced this pull request Dec 8, 2024
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@sobolevn@gaogaotiantian@erlend-aasland@ZeroIntensity