Skip to content
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

gh-101123: Add signature for the math.hypot #101124

Closed
wants to merge 3 commits into from
Closed

gh-101123: Add signature for the math.hypot #101124

wants to merge 3 commits into from

Conversation

skirpichev
Copy link
Member

@skirpichev skirpichev commented Jan 18, 2023

Note: this does conversion of the math.hypot to the Argument Clinic. Alternative solution mentioned in the issue description.

if (nargs > NUM_STACK_ELEMS) {
coordinates = (double *) PyObject_Malloc(nargs * sizeof(double));
if (coordinates == NULL) {
return PyErr_NoMemory();
}
}
for (i = 0; i < nargs; i++) {
item = args[i];
item = PyTuple_GetItem(args, i);
Copy link
Member

Choose a reason for hiding this comment

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

This might affect performance. Can you please measure it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, there is a difference.

$ ./python -m timeit -s 'from math import hypot as h' -s 'a = list(range(1, 10))' 'h(*a)'
500000 loops, best of 5: 650 nsec per loop
$ ./python -m timeit -s 'from math import hypot as h' -s 'a = list(range(1, 100))' 'h(*a)'
50000 loops, best of 5: 4.54 usec per loop
$ ./python -m timeit -s 'from math import hypot as h' -s 'a = list(range(1, 1000))' 'h(*a)'
5000 loops, best of 5: 45.1 usec per loop
$ git checkout fix-101123 
Switched to branch 'fix-101123'
Your branch is up to date with 'github/fix-101123'.
$ make -s
Checked 111 modules (30 built-in, 80 shared, 1 n/a on linux-x86_64, 0 disabled, 0 missing, 0 failed on import)
$ ./python -m timeit -s 'from math import hypot as h' -s 'a = list(range(1, 10))' 'h(*a)'
500000 loops, best of 5: 950 nsec per loop
$ ./python -m timeit -s 'from math import hypot as h' -s 'a = list(range(1, 100))' 'h(*a)'
50000 loops, best of 5: 6.43 usec per loop
$ ./python -m timeit -s 'from math import hypot as h' -s 'a = list(range(1, 1000))' 'h(*a)'
5000 loops, best of 5: 65.6 usec per loop

So, probably we should use the patch from the issue. On another hand, the comment ("AC: cannot convert yet, waiting for *args support") - seems to be misleading and must be removed.

Copy link
Member

Choose a reason for hiding this comment

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

You can use string-based docs to populate __text_signature__. It will fix your problem with inspect.signature with 0 runtime cost. Example from dict.get:

PyDoc_STRVAR(dict_get__doc__,
"get($self, key, default=None, /)\n"
"--\n"
"\n"
"Return the value for key if key is in the dictionary, else default.");

Copy link
Member Author

Choose a reason for hiding this comment

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

You can use string-based docs to populate text_signature.

Yes, I understand. That seems to be the solution, presented in the issue description, isn't?

The question was: should we keep the mentioned comment for math_hypot? It seems to be misleading for me (issue reference would be more helpful here, if it is an open problem), as there is no principal problems to convert this function to the Argument Clinic.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think it should be changed to: Not converted to AC because it affects performance

Copy link
Member Author

@skirpichev skirpichev Jan 18, 2023

Choose a reason for hiding this comment

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

Could @rhettinger bless this change? The comment is coming from #8474 and I'm not sure he meant performance issues with AC (rather something like #64490).
PS: AC stuff reverted.
Timings with PyTuple_GET_ITEM:

$ ./python -m timeit -s 'from math import hypot as h' -s 'a = list(range(1, 10))' 'h(*a)'
500000 loops, best of 5: 814 nsec per loop
$ ./python -m timeit -s 'from math import hypot as h' -s 'a = list(range(1, 100))' 'h(*a)'
50000 loops, best of 5: 5.16 usec per loop
$ ./python -m timeit -s 'from math import hypot as h' -s 'a = list(range(1, 1000))' 'h(*a)'
5000 loops, best of 5: 53.2 usec per loop

@rhettinger rhettinger closed this Jan 19, 2023
@skirpichev skirpichev deleted the fix-101123 branch January 19, 2023 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants