GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-90350: Optimize builtin functions min() and max() by colorfulappl · Pull Request #30286 · python/cpython

Conversation

@colorfulappl

@colorfulappl colorfulappl commented Dec 29, 2021 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Builtin functions min() and max() are labeled as METH_VARARGS | METH_KEYWORDS, which use tp_call calling convention.
After changing their label to METH_FASTCALL | METH_KEYWORDS, they can be invoked by vectorcall.

This optimization simplifies parameter passing and avoids creation of temporary tuple while parsing arguments, brings about up to 200%+ speed up on microbenchmarks.

faster-cpython/ideas#199

https://bugs.python.org/issue46192

  • Issue: Optimize builtin functions min() and max() #90350

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@colorfulappl
Builtin functions min() and max() now use METH_FASTCALL
a9413ab
@the-knights-who-say-ni

This comment has been minimized.

Sign in to view
@the-knights-who-say-ni the-knights-who-say-ni added the CLA not signed label Dec 29, 2021
@bedevere-bot bedevere-bot added the awaiting review label Dec 29, 2021
@colorfulappl colorfulappl changed the title bpo-46192: Builtin functions min() and max() now use METH_FASTCALL bpo-46192: Optimize builtin functions min() and max() Dec 29, 2021
@AlexWaygood AlexWaygood added the performance Performance or resource usage label Dec 29, 2021
@AlexWaygood
AlexWaygood requested a review from serhiy-storchaka December 29, 2021 10:14
@erlend-aasland

erlend-aasland commented Dec 29, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

If we're touching these, it would make sense to convert them to Argument Clinic, now that *args support is implemented (9af34c9).

cpython/Python/bltinmodule.c

Lines 1818 to 1823 in 77195cd

/* AC: cannot convert yet, waiting for *args support */
static PyObject *
builtin_min(PyObject *self, PyObject *args, PyObject *kwds)
{
return min_max(args, kwds, Py_LT);
}

cpython/Python/bltinmodule.c

Lines 1835 to 1840 in 77195cd

/* AC: cannot convert yet, waiting for *args support */
static PyObject *
builtin_max(PyObject *self, PyObject *args, PyObject *kwds)
{
return min_max(args, kwds, Py_GT);
}

isidentical, sweeneyde, and oraluben reacted with thumbs up emoji
All reactions
  • 👍 3 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@colorfulappl

colorfulappl commented Dec 30, 2021

Copy link
Copy Markdown
Contributor Author

I wrote an "Argument Clinic" version here:
colorfulappl@29b9559

But seems it is not as fast as current "without Argument Clinic" version:
colorfulappl@a9413ab

Result of microbench:

code snippet with AC w/o AC
max(1, 2) 1.11x faster 3.25x faster
max([1, 2]) 1.14x faster 1.41x faster
max((1, ), (2, ), key=lambda x: x[0]) 1.89x faster 1.85x faster
max([(1, ), (2, )], key=lambda x: x[0]) 1.73x faster 1.52x faster
max([], default=-1) 2.39x faster 2.61x faster
max(1, 2, 3, 4, 5) 1.02x faster 2.46x faster

On the most commonly used case, "max(a, b [, ...])", there is not noticeable speed up when we use AC, especially when multiple arguments are passed (the last case).

I noticed that in 9af34c9 , the varargs on stack are packed to tuple before passed to callee, and callee obtains each argument by access the tuple's elements.
This slows down the function invocation.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@colorfulappl

colorfulappl commented Dec 30, 2021

Copy link
Copy Markdown
Contributor Author

One goal of fastcall is to avoid the creation of a temporary tuple to pass positional arguments (https://bugs.python.org/issue29259).

IMHO, the process of pack/unpack arguments to a tuple is unnecessary in 9af34c9 .
Perhaps it's better to pass an Object * const * pointer which points to the first argument and a nargs integer to indicate how many positional arguments are passed when we are processing varargs.

I would have a try, then may open another issue if necessary.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland

erlend-aasland commented Dec 30, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

IMHO, the process of pack/unpack arguments to a tuple is unnecessary in 9af34c9 . Perhaps it's better to pass an Object * const * pointer which points to the first argument and a nargs integer to indicate how many positional arguments are passed when we are processing varargs.

I would have a try, then may open another issue if necessary.

Yes, such a change demands a separate issue / PR. It would be nice if you could add proof-of-concept benchmark results when you present the idea in the new bpo issue. I can add the relevant core devs on the nosy list, if you want.

Keep in mind that readability and maintainability weigh very heavy when considering a PR; that also goes for optimisation changes.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland

erlend-aasland commented Dec 30, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

I wrote an "Argument Clinic" version here: colorfulappl@29b9559

While that change certainly looks better, it is buggy; it does not heed the key function. With that in mind, the benchmarks you posted in #30286 (comment) are obviously wrong; the benchmarks with the key keyword are not correct.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sweeneyde

sweeneyde commented Dec 30, 2021

Copy link
Copy Markdown
Member

I wonder if Argument Clinic Itself could be updated to convert *args to vectorcall/fastcall if some kind of flag appears in the AC declaration

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sweeneyde

sweeneyde commented Dec 31, 2021

Copy link
Copy Markdown
Member

*args support was added to AC in #18609

@isidentical any thoughts on removing the tuple creation in some cases, or with some flag?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland

This comment was marked as off-topic.

Sign in to view
@colorfulappl

colorfulappl commented Dec 31, 2021 •
edited by erlend-aasland
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

I would have a try, then may open another issue if necessary.

I opened a new issue (gh-903701),
and made a PR (#30312).

Footnotes

  1. edit by @erlend-aasland: convert bpo link to gh link ↩

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@colorfulappl

colorfulappl commented Dec 31, 2021

Copy link
Copy Markdown
Contributor Author

the benchmarks you posted in #30286 (comment) are obviously wrong; the benchmarks with the key keyword are not correct.

Thanks for revising my mistake. I'v seen the new results after your amend.
Actually, when the key function is empty, the "AC version" runs faster.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@the-knights-who-say-ni the-knights-who-say-ni added CLA signed and removed CLA not signed labels Jan 20, 2022
MaxwellDupre

This comment was marked as outdated.

Sign in to view

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@colorfulappl colorfulappl mannequin mentioned this pull request Apr 30, 2022
Optimize builtin functions min() and max() #90350
Closed
@serhiy-storchaka serhiy-storchaka added the topic-argument-clinic label Apr 30, 2022
@ezio-melotti ezio-melotti removed the CLA signed label Jul 13, 2022
@larryhastings larryhastings mentioned this pull request Apr 10, 2022
Argument Clinic should understand *args and **kwargs parameters #64490
Closed
@hauntsaninja
Merge branch 'main' into opt_minmax
34142bf
@hauntsaninja hauntsaninja changed the title bpo-46192: Optimize builtin functions min() and max() gh-90350: Optimize builtin functions min() and max() Feb 26, 2023
@eendebakpt

eendebakpt commented Jul 16, 2023

Copy link
Copy Markdown
Contributor

@colorfulappl Is this PR still blocked by #90370?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka added 3 commits December 11, 2023 19:19
@serhiy-storchaka
Merge branch 'main' into opt_minmax
4dae576
@serhiy-storchaka
Fix compiler warnings.
9852e27
@serhiy-storchaka
Use _PyCFunction_CAST.
610c3d5
@serhiy-storchaka
Add a NEWS entry.
3a0413e
serhiy-storchaka
serhiy-storchaka approved these changes Dec 11, 2023
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Dec 11, 2023
@serhiy-storchaka
serhiy-storchaka merged commit 0066ab5 into python:main Dec 11, 2023
@bedevere-app bedevere-app Bot removed the awaiting merge label Dec 11, 2023
facebook-github-bot pushed a commit to facebookincubator/MetaPython that referenced this pull request Jan 16, 2024
@itamaro @facebook-github-bot
Backport gh-90350: Optimize builtin functions min() and max() (GH-30286)
8476e31
Summary:
Builtin functions min() and max() now use METH_FASTCALL

upstream issue: python/cpython#90350
upstream PR: python/cpython#30286
upstream commit: python/cpython@0066ab5

Reviewed By: carljm

Differential Revision: D52650071

fbshipit-source-id: 93971e865ab9515efc9771c58582f63d15e0342d
aisk pushed a commit to aisk/cpython that referenced this pull request Feb 11, 2024
@colorfulappl @aisk
pythongh-90350: Optimize builtin functions min() and max() (pythonGH-…
b69fd78
…30286)

Builtin functions min() and max() now use METH_FASTCALL
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
@colorfulappl @Glyphack
pythongh-90350: Optimize builtin functions min() and max() (pythonGH-…
c292b70
…30286)

Builtin functions min() and max() now use METH_FASTCALL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes
+1 more reviewer
@MaxwellDupre MaxwellDupre MaxwellDupre left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

performance Performance or resource usage topic-argument-clinic

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

11 participants

@colorfulappl @the-knights-who-say-ni @erlend-aasland @sweeneyde @eendebakpt @serhiy-storchaka @MaxwellDupre @ezio-melotti @bedevere-bot @AlexWaygood @hauntsaninja
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.