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-136681: optimize builtin calls if kwargs allowed by skirpichev · Pull Request #137202 · python/cpython

Conversation

@skirpichev

@skirpichev skirpichev commented Jul 29, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

This is a first part, that should address #136681. With this patch there should be no overhead for function calls if it's signature allows keyword arguments.

Below are micro-benchmarks for math.fmin(), when only positional arguments are allowed (as in the main) vs positional-or-keyword.

The missing part is specialization for 1-arg functions, like cmath.sin(): #136681 (comment)

In the main:

Benchmark posonly-patch posorkw-patch
fmin(1.0, 2.0) 169 ns 170 ns: 1.01x slower
fmin(1.0, 2.0) x 2 times 913 ns 890 ns: 1.03x faster
fmin(1.0, 2.0) x 100 times 14.7 us 14.8 us: 1.01x slower
Geometric mean (ref) 1.00x faster

Benchmark hidden because not significant (1): fmin(1.0, 2.0) x 10 times

With the patch:

Benchmark posonly-ref posorkw-ref
fmin(1.0, 2.0) 177 ns 199 ns: 1.12x slower
fmin(1.0, 2.0) x 2 times 937 ns 980 ns: 1.05x slower
fmin(1.0, 2.0) x 10 times 2.08 us 2.25 us: 1.08x slower
fmin(1.0, 2.0) x 100 times 14.8 us 16.5 us: 1.12x slower
Geometric mean (ref) 1.09x slower

Benchmark code:

import pyperf
from math import fmin

def f(n):
    for _ in range(n):
        fmin(1.0, 2.0)

runner = pyperf.Runner()
runner.bench_func('fmin(1.0, 2.0)', fmin, 1.0, 2.0)
for n in [2, 10, 100]:
    s = f'fmin(1.0, 2.0) x {n:3} times'
    runner.bench_func(s, f, n)
  • Issue: Slow functions calls for C-coded functions, accepting positional-or-kwarg argument vs positional-only #136681

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot mentioned this pull request Jul 29, 2025
Slow functions calls for C-coded functions, accepting positional-or-kwarg argument vs positional-only #136681
Open
@skirpichev
pythongh-136681: optimize builtin calls if kwargs allowed
18632fc
This is a first part, that should address python#136681.  With this patch
there should be no overhead for function calls if it's signature allows
keyword arguments.

Below are micro-benchmarks for math.fmin(), when only positional
arguments are allowed (as in the main) vs positional-or-keyword.

The missing part is specialization for 1-arg functions, like cmath.sin():
python#136681 (comment)

In the main:

| Benchmark                  | posonly-patch | posorkw-patch         |
|----------------------------|:-------------:|:---------------------:|
| fmin(1.0, 2.0)             | 169 ns        | 170 ns: 1.01x slower  |
| fmin(1.0, 2.0) x   2 times | 913 ns        | 890 ns: 1.03x faster  |
| fmin(1.0, 2.0) x 100 times | 14.7 us       | 14.8 us: 1.01x slower |
| Geometric mean             | (ref)         | 1.00x faster          |

Benchmark hidden because not significant (1): fmin(1.0, 2.0) x  10 times

With the patch:

| Benchmark                  | posonly-ref | posorkw-ref           |
|----------------------------|:-----------:|:---------------------:|
| fmin(1.0, 2.0)             | 177 ns      | 199 ns: 1.12x slower  |
| fmin(1.0, 2.0) x   2 times | 937 ns      | 980 ns: 1.05x slower  |
| fmin(1.0, 2.0) x  10 times | 2.08 us     | 2.25 us: 1.08x slower |
| fmin(1.0, 2.0) x 100 times | 14.8 us     | 16.5 us: 1.12x slower |
| Geometric mean             | (ref)       | 1.09x slower          |

Benchmark code:

```py
import pyperf
from math import fmin

def f(n):
    for _ in range(n):
        fmin(1.0, 2.0)

runner = pyperf.Runner()
runner.bench_func('fmin(1.0, 2.0)', fmin, 1.0, 2.0)
for n in [2, 10, 100]:
    s = f'fmin(1.0, 2.0) x {n:3} times'
    runner.bench_func(s, f, n)
```
@skirpichev
skirpichev force-pushed the fast-posorkw/136681 branch from 51d4d9d to 18632fc Compare July 29, 2025 10:15
@skirpichev skirpichev closed this Jul 29, 2025
@skirpichev
skirpichev deleted the fast-posorkw/136681 branch July 29, 2025 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently will be requested when the pull request is marked ready for review ericsnowcurrently is a code owner
@erlend-aasland erlend-aasland Awaiting requested review from erlend-aasland erlend-aasland will be requested when the pull request is marked ready for review erlend-aasland is a code owner
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner will be requested when the pull request is marked ready for review AA-Turner is a code owner

Uh oh!

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

Assignees

No one assigned

Labels

None yet

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.

1 participant

@skirpichev
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 01402520 — 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.