Skip to content

Argument Clinic: improve generated code for subclass_of checks #101479

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
erlend-aasland opened this issue Jan 31, 2023 · 2 comments
Closed

Argument Clinic: improve generated code for subclass_of checks #101479

erlend-aasland opened this issue Jan 31, 2023 · 2 comments
Labels
topic-argument-clinic type-feature A feature request or enhancement

Comments

@erlend-aasland
Copy link
Contributor

erlend-aasland commented Jan 31, 2023

Feature or enhancement

Follow-up of #101409

Currently, the subclass_of clinic parameter is spelled out twice verbatim in the generated code. For heap type methods in isolated extension modules, the following pattern is often used:

/*[clinic input]
mymod.myclass.mymeth

    obj: object(subclass_of='clinic_state()->SomeType')

[clinic start generated code]*/

This may result in the following generated code:

if (!PyObject_TypeCheck(args[0], clinic_state()->SomeType)) {
    _PyArg_BadArgument("mymeth", "argument 'obj'", (clinic_state()->SomeType)->tp_name, args[0]);
}

Now, the second call is definitely a slow path, so this is not too bad. I still think it would be an improvement to generate code such as this:

PyTypeObject *obj_type = clinic_state()->SomeType;
if (!PyObject_TypeCheck(args[0], obj_type)) {
    _PyArg_BadArgument("mymeth", "argument 'obj'", obj_type->tp_name, args[0]);
}

Linked PRs

@erlend-aasland erlend-aasland added type-feature A feature request or enhancement topic-argument-clinic labels Jan 31, 2023
@erlend-aasland
Copy link
Contributor Author

cc. @colorfulappl, @serhiy-storchaka

erlend-aasland added a commit to erlend-aasland/cpython that referenced this issue Jan 31, 2023
@erlend-aasland erlend-aasland added the pending The issue will be closed if no feedback is provided label Feb 1, 2023
@erlend-aasland
Copy link
Contributor Author

erlend-aasland commented Feb 1, 2023

I agree with Serhy's comment on the PR: there is no performance gain here, so this is unneeded churn. Closing as not planned.

@erlend-aasland erlend-aasland closed this as not planned Won't fix, can't repro, duplicate, stale Feb 1, 2023
@erlend-aasland erlend-aasland removed the pending The issue will be closed if no feedback is provided label Feb 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-argument-clinic type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

1 participant