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-144650: support Vectorcall for _decimal.Decimal's by skirpichev · Pull Request #158207 · python/cpython

Conversation

@skirpichev

@skirpichev skirpichev commented Sep 26, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

This shows almost no effect on "official" pyperformance benchmarks (see python/pyperformance#473):

decimal_pi: Mean +- std dev: [ref] 1.92 sec +- 0.02 sec -> [patch] 1.89 sec +- 0.02 sec: 1.02x faster

Benchmark hidden because not significant (2): decimal_factorial, telco

Geometric mean: 1.01x faster

But on micro-benchmarks it show some speedup:

Decimal(0): Mean +- std dev: [ref-micro] 591 ns +- 10 ns -> [patch-micro] 513 ns +- 12 ns: 1.15x faster
Decimal(123): Mean +- std dev: [ref-micro] 596 ns +- 22 ns -> [patch-micro] 515 ns +- 26 ns: 1.16x faster
Decimal('123'): Mean +- std dev: [ref-micro] 764 ns +- 16 ns -> [patch-micro] 677 ns +- 24 ns: 1.13x faster

Geometric mean: 1.15x faster

(Default ./configure options, gcc 14.2)

Details
# bench.py
import pyperf
from decimal import Decimal

runner = pyperf.Runner()
for x in [0, 123, '123']:
    h = f"Decimal({x!r})"
    runner.bench_func(h, Decimal, x)
  • Issue: Speed regression in the decimal module due to using heap types #144650

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added the awaiting review label Sep 26, 2026
@skirpichev
pythongh-144650: support Vectorcall for _decimal.Decimal's
a0723f1
@skirpichev
skirpichev force-pushed the decimal-ac/144650 branch from 325c126 to a0723f1 Compare September 26, 2026 02:28
@skirpichev skirpichev changed the title Support Vectorcall for _decimal.Decimal's gh-144650: support Vectorcall for _decimal.Decimal's Sep 26, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Sep 26, 2026
Speed regression in the decimal module due to using heap types #144650
Open
@cmaloney

cmaloney commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

#157968 may help with the build failures (changes how the assert works which should make it better across module boundaries)

All reactions

Sorry, something went wrong.

Uh oh!

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

@skirpichev

skirpichev commented Sep 26, 2026

Copy link
Copy Markdown
Member Author

Thanks. The Decimal is only possible candidate being a heap type in the stdlib. That was worked before in skirpichev#18

cmaloney reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@skirpichev
skirpichev marked this pull request as draft September 26, 2026 03:02
@bedevere-app bedevere-app Bot removed the awaiting review label Sep 26, 2026
@skirpichev skirpichev self-assigned this Sep 26, 2026
@cmaloney

cmaloney commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

👍 hopefully will get more of these landed soon :). str and list still need some special work for stable performance over the change which I'm still root causing (likely compiler inlining vs. existing hand inlining). If there are ways I can help enable / particular edge cases you find let me know. Otherwise planning to make PRs as I have time + performance results for existing cases across the codebase (int, float, str, list, bytes, bytearray, bool, filter, set, frozenset are on my radar).

All reactions

Sorry, something went wrong.

Uh oh!

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

cmaloney and others added 3 commits September 26, 2026 06:31
@cmaloney @skirpichev
pythongh-150097: Move _testclinic to multi-phase init
6c3d922
Convert the static types to heap types. Move empty init vectorcall to
share with existing no-arg __init__ case. Add support for no-argument
vectorcall so the no-arg __init__ cases actually work (rather than
adding an arg). Produce the same error string as the PyArg helpers do.

The vectorcall types no longer have a C address for the generated
`Py_Is` so instead compare the slot function instead. This drops the
requirement that a class using vectorcall declare a type object.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

(cherry-picked from python#157968)
@skirpichev
run AC
6df90e5
@skirpichev
+1
bf222f6
@skirpichev
skirpichev marked this pull request as ready for review September 26, 2026 03:41
@skirpichev
skirpichev requested review from AA-Turner and erlend-aasland as code owners September 26, 2026 03:41
@bedevere-app bedevere-app Bot added the awaiting review label Sep 26, 2026
skirpichev added 2 commits September 26, 2026 07:38
@skirpichev
+1
9b709f0
@skirpichev
Merge branch 'master' into decimal-ac/144650
42c7a83
@skirpichev skirpichev removed their assignment Oct 2, 2026
@skirpichev
Merge branch 'master' into decimal-ac/144650
addce3f
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Oct 6, 2026
skirpichev added 2 commits October 6, 2026 08:07
@skirpichev
+1
842858f
@skirpichev
Merge branch 'main' into decimal-ac/144650
e29740d
@skirpichev

skirpichev commented Oct 8, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

CC @vstinner, does it make sense for you?

Edit: N.B., this depends on #157968.

All reactions

Sorry, something went wrong.

Uh oh!

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

skirpichev added 2 commits October 14, 2026 04:49
@skirpichev
Merge branch 'master' into decimal-ac/144650
40253f5
# Conflicts:
#	Modules/clinic/_testclinic.c.h
@skirpichev
+1
4168ef4
@skirpichev skirpichev closed this Oct 17, 2026
@skirpichev
skirpichev deleted the decimal-ac/144650 branch October 17, 2026 03:07
@skirpichev skirpichev mentioned this pull request Oct 22, 2026
gh-87613: Allow Argument Clinic vectorcall on heap types #160115
Merged
@skirpichev
skirpichev restored the decimal-ac/144650 branch October 23, 2026 03:16
skirpichev added 2 commits October 23, 2026 06:22
@skirpichev
Merge branch 'master' into decimal-ac/144650
9ba7b2e
# Conflicts:
#	Tools/clinic/libclinic/parse_args.py
@skirpichev
+ inline PyDecType_FromObjectExact
2192148
@skirpichev skirpichev reopened this Oct 23, 2026
vstinner
vstinner reviewed Oct 23, 2026
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Benchmark results were posted 1 month ago, but the PR changed in the meanwhile. Would it be possible to re-run benchmarks on the latest PR version?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Modules/_decimal/_decimal.c Resolved

Uh oh!

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

@vstinner

vstinner commented Oct 23, 2026

Copy link
Copy Markdown
Member

See also #73487 which converted many Decimal methods to vectorcall using Argument Clinic.

All reactions

Sorry, something went wrong.

Uh oh!

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

@skirpichev

skirpichev commented Oct 23, 2026

Copy link
Copy Markdown
Member Author

#158207 (comment) were posted 1 month ago, but the PR changed in the meanwhile. Would it be possible to re-run benchmarks on the latest PR version?

In fact, you can see that benchmarks already updated. Not so much, as before. #156034 shows better numbers.

Now included telco benchmark.

Is it useful to mark the function as "inline"? Does it impact performance?

This is something that was suggested in #160115.

You can compare my previous benchmark run. IMO, the difference looks more or less as a noise:
#160115 (comment)

Not sure if this worth reversion. That function used only once, anyway.

All reactions

Sorry, something went wrong.

Uh oh!

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

@cmaloney

cmaloney commented Oct 23, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

I don't mind the inline keyword, it's a compiler hint that is largely ignored (which is why forceinline exists..). Compilers decided people perf tested and added then never maintained ><. In this case PyDecType_FromObjectExact inlines into the _new_impl with or without it in the experimentation I've done. It doesn't inline into the vectorcall handler because there are two callers of _new_impl and the compiler decides not to proceed.

All reactions

Sorry, something went wrong.

Uh oh!

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

skirpichev added 2 commits October 25, 2026 05:10
@skirpichev
Merge branch 'master' into decimal-ac/144650
ea5cf37
# Conflicts:
#	Modules/_decimal/clinic/_decimal.c.h
@skirpichev
revert inline PyDecType_FromObjectExact
6c4d04e
@skirpichev

skirpichev commented Oct 25, 2026

Copy link
Copy Markdown
Member Author

Sorry, @cmaloney, it seems that your suggestion is not clear for me.

All reactions

Sorry, something went wrong.

Uh oh!

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

@skirpichev
skirpichev requested a review from vstinner October 25, 2026 02:14
@cmaloney

cmaloney commented Oct 25, 2026 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

I have no preference on inline keyword or not. In this case i did some assembly looking and without the keyword it does get inlined to the _new_impl.

All reactions

Sorry, something went wrong.

Uh oh!

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

cmaloney
cmaloney approved these changes Oct 25, 2026
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting core review and removed awaiting review labels Oct 25, 2026
@vstinner

vstinner commented Oct 25, 2026

Copy link
Copy Markdown
Member

In fact, you can see that benchmarks already updated. Not so much, as before. #156034 shows better numbers.

#156034 (comment) was posted in August. Python changed since August, so I prefer to not rely on old benchmark numbers.

It's unclear to me if #160115 (comment) include the "static inline" change or not.

I would be easier for me if you can post benchmark results in this PR rather than pointing to other issues/PRs.

Can you please re-run benchmarks on the latest PR version, since you just modified it?

All reactions

Sorry, something went wrong.

Uh oh!

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

@skirpichev

skirpichev commented Oct 25, 2026

Copy link
Copy Markdown
Member Author

Can you please re-run benchmarks on the latest PR version, since you just modified it?

Ok, done. (Same setup: default configure options, gcc 14.2.)

decimal_pi: Mean +- std dev: [ref] 2.00 sec +- 0.01 sec -> [patch] 1.97 sec +- 0.01 sec: 1.01x faster

Benchmark hidden because not significant (2): decimal_factorial, telco

Geometric mean: 1.00x faster
Decimal(0): Mean +- std dev: [ref-micro] 563 ns +- 11 ns -> [patch-micro] 510 ns +- 10 ns: 1.10x faster
Decimal(123): Mean +- std dev: [ref-micro] 560 ns +- 11 ns -> [patch-micro] 509 ns +- 10 ns: 1.10x faster
Decimal('123'): Mean +- std dev: [ref-micro] 793 ns +- 11 ns -> [patch-micro] 727 ns +- 13 ns: 1.09x faster

Geometric mean: 1.10x faster
All reactions

Sorry, something went wrong.

Uh oh!

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

@vstinner

vstinner commented Oct 25, 2026

Copy link
Copy Markdown
Member

decimal_pi, decimal_factorial and telco benchmark results are a little bit disappointing. (Basically, there is no impact on performance, just a minor "1.01x faster" which could also just be noise in the results).

Decimal(0): Mean +- std dev: [ref-micro] 563 ns +- 11 ns -> [patch-micro] 510 ns +- 10 ns: 1.10x faster
Decimal(123): Mean +- std dev: [ref-micro] 560 ns +- 11 ns -> [patch-micro] 509 ns +- 10 ns: 1.10x faster
Decimal('123'): Mean +- std dev: [ref-micro] 793 ns +- 11 ns -> [patch-micro] 727 ns +- 13 ns: 1.09x faster
Geometric mean: 1.10x faster

1.10x faster on a micro-benchmark is borderline interesting, but I think that adding @vectorcall has a very limited impact on maintenance, so it's worth it to make this change.

I tested a sub-type: since tp_vectorcall is not inherited by sub-types, we are good (dec_vectorcall() is not used, that's ok):

import _decimal

class MyDecimal(_decimal.Decimal):
    pass

x = MyDecimal(123)
All reactions

Sorry, something went wrong.

Uh oh!

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

vstinner
vstinner reviewed Oct 25, 2026
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2026-09-26-05-18-21.gh-issue-144650.zqHLOW.rst Resolved

Uh oh!

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

@vstinner
Update Misc/NEWS.d/next/Library/2026-09-26-05-18-21.gh-issue-144650.z…
5efe802
…qHLOW.rst
@vstinner
vstinner enabled auto-merge (squash) October 25, 2026 23:57
vstinner
vstinner approved these changes Oct 25, 2026
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Oct 25, 2026
@vstinner
vstinner merged commit 1c327a4 into python:main Oct 26, 2026
59 of 60 checks passed

Uh oh!

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

@bedevere-app bedevere-app Bot removed the awaiting merge label Oct 26, 2026
@skirpichev
skirpichev deleted the decimal-ac/144650 branch October 26, 2026 00:32
@skirpichev skirpichev mentioned this pull request Nov 6, 2026
gh-116946: remove unnecessary gc from _decimal types #156034
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes
@cmaloney cmaloney cmaloney approved these changes
@erlend-aasland erlend-aasland Awaiting requested review from erlend-aasland
@AA-Turner AA-Turner Awaiting requested review from AA-Turner

Uh oh!

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

Assignees

No one assigned

Labels

type-feature A feature request or enhancement

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.

3 participants

@skirpichev @cmaloney @vstinner
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.