Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-119109: improvefunctools.partial vectorcall with keywords#124584

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
dg-pb wants to merge29 commits intopython:main
base:main
Choose a base branch
Loading
fromdg-pb:gh-119109-partial_vectorcall_kw

Conversation

dg-pb
Copy link
Contributor

@dg-pbdg-pb commentedSep 26, 2024
edited
Loading

(Potentiallycloses#128050)

This IMO is the best approach to resolve fallback "issue". It:
a) Eliminates the need for the fallback or any need to switch between implementation after initial construction
b) Delivers performance benefits for vectorcall whenpartial has keywords

Benchmark:

# BENCH 2 ARGS# ------------S="from functools import partialf=lambda a, b: a - bp1 = partial(f)p2 = partial(f, b=2)l = lambda a: f(a, b=2)"$PYCMD -c"${S}; print(p1(1, 2))"# -1     | -1     |$PYCMD -c"${S}; print(p2(1))"# -1     | -1     |# BEFORE | AFTER  | %CHN | LAMBDA LB$PYCMD -m timeit -s$S'p1(1, 2)'#  87 ns |  85 ns |      |$PYCMD -m timeit -s$S'p1(1, b=2)'# 100 ns |  96 ns |      |$PYCMD -m timeit -s$S'p2(1)'# 240 ns | 135 ns | -45% |  94 ns$PYCMD -m timeit -s$S'p2(a=1)'# 350 ns | 160 ns | -55% | 110 ns# BENCH 10 ARGS# -------------S="from functools import partialfunc = lambda a, b, c, d, e, f, g, h, i, j: (a + b + c + d + e + f + g + h + i + j)p = partial(func, f=5, g=6, h=7, i=8, j=9)l = lambda a, b, c, d, e, f=5, g=6, h=7, i=8, j=9: func(a, b, c, d, e, f=f, g=g, h=h, i=i, j=j)"C0="${S}; print(p(0, 1, 2, 3, 4))"C1='p(0, 1, 2, 3, 4)'C2='p(a=0, b=1, c=2, d=3, e=4)'# disjoint kw and pto_kwC3='p(a=0, b=1, c=2, d=3, e=4, f=5, g=6)'# kw partially overlaps pto_kwC4='p(a=0, b=1, c=2, d=3, e=4, f=5, g=6, h=7, i=8, j=9)'# kw overrides pto_kw$PYCMD -c$C0#  45     | 45     |#  BEFORE | AFTER  | %CHN | LAMBDA LB$PYCMD -m timeit -s$S$C1#  440 ns | 320 ns | -28% | 240 ns$PYCMD -m timeit -s$S$C2#  890 ns | 440 ns | -50% | 260 ns$PYCMD -m timeit -s$S$C3# 1000 ns | 600 ns | -40% | 270 ns$PYCMD -m timeit -s$S$C4# 1250 ns | 700 ns | -44% | 300 ns# FUNCTION CALL - 210 ms$PYCMD -m timeit -s$S'f(a=0, b=1, c=2, d=3, e=4, f=5, g=6, h=7, i=8, j=9)'

No penalty for calls withoutpto_kwds.
Non negligible speed improvement for calls withpto_kwds: 27 - 55%

@rhettingerrhettinger requested review fromvstinner and removed request forrhettingerSeptember 26, 2024 17:33
@rhettinger
Copy link
Contributor

Perhaps@vstinner has the time and interest in looking at this.

@dg-pbdg-pb marked this pull request as draftSeptember 27, 2024 06:31
@dg-pbdg-pb marked this pull request as ready for reviewSeptember 27, 2024 14:54
@dg-pb
Copy link
ContributorAuthor

I think it is a good compromise between simplicity and performance now.

One micro-optimization that I couldn't figure out how to do simply is pre-storingkwnames tuple so it doesn't need to be created on every call. It would drop another ~50 ns.

Not sure how much sense it makes yet, but I postedfaster-cpython/ideas#699 in relation to this.

Ready for review now.

@dg-pb
Copy link
ContributorAuthor

Was wandering if it might be worth factoring out macros for private use.

@colesburycolesbury self-requested a reviewDecember 21, 2024 20:32
@picnixzpicnixz changed the titlegh-119109: functools.partial vectorcall with keywordsgh-119109: improve performance forfunctools.partial vectorcall with keywordsJan 5, 2025
@picnixzpicnixz added the performancePerformance or resource usage labelJan 5, 2025
@picnixz
Copy link
Member

picnixz commentedJan 5, 2025
edited
Loading

Is the performance change a direct effect of the simplification or is this the other way around? (namely, can we decouple the performance gains from the simplification?) (not that the PR is too big but for bisecting commits, it's easier when we have atomic changes)

@dg-pb
Copy link
ContributorAuthor

dg-pb commentedJan 5, 2025
edited
Loading

Your title change is incorrect, this has wider implications.

It removes dynamic switching, which causes issues with free threading. And generally results in more linear straight forward flow, so it is a design improvement as well.

The fact thatvectorcall will be used in more cases, which is faster is only a benefit. Although from users perspective the performance gain is the only thing worth mentioning.

@picnixzpicnixz changed the titlegh-119109: improve performance forfunctools.partial vectorcall with keywordsgh-119109: improvefunctools.partial vectorcall with keywordsJan 5, 2025
@dg-pb
Copy link
ContributorAuthor

Is the performance change a direct effect of the simplification

yes, it is one atomic change. There is no way to split it.

picnixz reacted with thumbs up emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@picnixzpicnixzpicnixz left review comments

@erlend-aaslanderlend-aaslanderlend-aasland left review comments

@ZeroIntensityZeroIntensityZeroIntensity left review comments

@rruuaanngrruuaanngrruuaanng left review comments

@vstinnervstinnerAwaiting requested review from vstinner

@colesburycolesburyAwaiting requested review from colesbury

@serhiy-storchakaserhiy-storchakaAwaiting requested review from serhiy-storchaka

Assignees
No one assigned
Labels
awaiting reviewperformancePerformance or resource usage
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Race betweenpartial_vectorcall_fallback and_PyVectorcall_FunctionInline under free-threading
7 participants
@dg-pb@rhettinger@picnixz@erlend-aasland@ZeroIntensity@rruuaanng@kumaraditya303

[8]ページ先頭

©2009-2025 Movatter.jp