Skip to content

Conversation

@rhettinger
Copy link
Contributor

@rhettinger rhettinger commented Jul 27, 2019

double diffs_on_stack[NUM_STACK_ELEMS];
double *diffs = diffs_on_stack;

if (!PyTuple_Check(p)) {
Copy link
Member

Choose a reason for hiding this comment

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

Why not call just PySequence_Tuple() (which is a no-op for tuples)? This would simplify the setup and the cleanup code.

It may be also worth to use PySequence_Fast() and PySequence_Fast_GET_ITEM() in the loop. This will save you allocating a new tuple. For general iterable PySequence_Tuple() creates a list and a tuple, but PySequence_Fast() creates just a list.

But you will need to call PySequence_Fast_GET_SIZE() in a loop because list's size can be changed when you convert items to the C double.

Copy link
Contributor Author

@rhettinger rhettinger Jul 27, 2019

Choose a reason for hiding this comment

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

I didn't want any external function calls on the fast path for the common case.

I did try it out. The patch was shorter but it caused a 15% performance degradation:

# baseline
$ pytime -r11 -s 'from math import dist' 'dist((10.5, 12.5), (13.5, 8.5))'
5000000 loops, best of 11: 57.3 nsec per loop

# patched to always call PySequence_Tuple() and always Py_DECREF on exit
$ pytime -r11 -s 'from math import dist' -s 'p, q = (10.5, 12.5), (13.5, 8.5)' 'dist(p, q)'
5000000 loops, best of 11: 64.7 nsec per loop

@rhettinger rhettinger merged commit 6b5f1b4 into python:master Jul 27, 2019
@miss-islington
Copy link
Contributor

Thanks @rhettinger for the PR 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-14984 is a backport of this pull request to the 3.8 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jul 27, 2019
…ates (pythonGH-14975)

(cherry picked from commit 6b5f1b4)

Co-authored-by: Raymond Hettinger <[email protected]>
rhettinger added a commit that referenced this pull request Jul 27, 2019
…ates (GH-14975) (GH-14984)

(cherry picked from commit 6b5f1b4)

Co-authored-by: Raymond Hettinger <[email protected]>
@python python deleted a comment from bedevere-bot Jul 29, 2019
lisroach pushed a commit to lisroach/cpython that referenced this pull request Sep 10, 2019
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
websurfer5 pushed a commit to websurfer5/cpython that referenced this pull request Jul 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants