Skip to content

gh-138912: Improve MATCH_CLASS opcode performance - #138915

Merged
gpshead merged 15 commits into
python:mainfrom
cdce8p:match-class-performance
Feb 14, 2026
Merged

gh-138912: Improve MATCH_CLASS opcode performance#138915
gpshead merged 15 commits into
python:mainfrom
cdce8p:match-class-performance

Conversation

@cdce8p

@cdce8pcdce8p commented Sep 15, 2025

Copy link
Copy Markdown
Contributor

Comment threadPython/ceval.c Outdated
if (seen == NULL) {
return NULL;
PyObject *seen = NULL;
if (nattrs > 1) {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Suggested change
if (nattrs>1) {
if (nargs>1|| (nargs==1&&nkwargs>0)) {

As mentioned in #138912, I'd also like to change this. If there are only keyword arguments, it's easy to see if two are the same name. Furthermore linters are able to detect these based on the AST alone.

It would also be great to say if we could suggest users to prefer keyword arguments. Not only are they more explicit, they are also faster. (No lookup of __match_args__ and, with the change, no expensive duplicate name detection.)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Added this in f585253 with if (nargs > 0 && nattrs > 1). I can revert it if necessary.

@cdce8pcdce8pDec 21, 2025

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Only mentioned it on the issue a while ago but the keyword attributes are validated already during the compile stage at which point a SyntaxError is raised. Just added an explicit test case for it in 9375e2b.

Comment threadPython/ceval.c
Comment threadPython/ceval.c Outdated
Comment threadPython/ceval.c Outdated
Comment threadPython/ceval.c
Comment threadPython/ceval.c
Comment threadPython/ceval.c Outdated
Comment threadPython/ceval.c Outdated
@eendebakpt

Copy link
Copy Markdown
Contributor

The CI failure seems unrelated. Updating the branch to rerun the CI just to be sure.

@cdce8p
cdce8pforce-pushed the match-class-performance branch from 359df88 to e0c2c1eCompareNovember 20, 2025 10:39

@sergey-miryanovsergey-miryanov left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@gpsheadgpshead 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.

nice!

@gpsheadgpshead added the performance Performance or resource usage label Feb 14, 2026
@gpshead
gpshead merged commit 75d4839 into python:mainFeb 14, 2026
59 checks passed
@cdce8p
cdce8p deleted the match-class-performance branch February 14, 2026 09:59
@cdce8p

Copy link
Copy Markdown
ContributorAuthor

Thanks for the review @gpshead. Is this change something which could be backported to 3.14 and maybe 3.13? Would be great to not have to wait until 3.15 for the performance improvements here.

@gpshead

Copy link
Copy Markdown
Member

Sorry, we only backport bugfixes and documentation updates.

brijkapadia pushed a commit to brijkapadia/cpython that referenced this pull request Feb 28, 2026
)
Only check for duplicates if there is at least one positional pattern.
With a test case for duplicate keyword attributes.
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
)
Only check for duplicates if there is at least one positional pattern.
With a test case for duplicate keyword attributes.
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performancePerformance or resource usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@cdce8p@eendebakpt@gpshead@sergey-miryanov@ZeroIntensity@nazeerali4325-commits