Skip to content

Conversation

@efimov-mikhail
Copy link
Member

@efimov-mikhail efimov-mikhail commented Nov 15, 2025

@efimov-mikhail efimov-mikhail marked this pull request as ready for review November 15, 2025 12:45
Copy link
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

Looks good, but I think it is worth using a table.
Using a table allows us to tweak the values to get better performance profile in future, and is easier to understand.

int backoff = counter.value_and_backoff & BACKOFF_MASK;
if (backoff < MAX_BACKOFF) {
return make_backoff_counter((1 << (backoff + 1)) - 1, backoff + 1);
return make_backoff_counter((1 << (2 * backoff + 3)) - 1, backoff + 1);
Copy link
Member

Choose a reason for hiding this comment

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

Since we are changing this, we might as well use a table. It is only 8 entries of 2 bytes each.

See #141498 (comment)
We want 0 to map to 1, and MAX_BACKOFF to map to near 8k.

I think you can make this branchless as well. Something like:

assert(backoff <= MAX_BACKOFF);
value = LOOKUP_TABLE[backoff]
backoff = backoff == MAX_BACKOFF ? backoff : backoff + 1;
return make_backoff_counter(value, backoff);

Copy link
Member

Choose a reason for hiding this comment

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

In fact, if we store the counter, both value and backoff, in the table. It can be even simpler:

assert(backoff <= MAX_BACKOFF);
return LOOKUP_TABLE[backoff];

And have LOOKUP_TABLE[UNREACHABLE_BACKOFF] == LOOKUP_TABLE[MAX_BACKOFF] so non-debug builds do something sensible.

Copy link
Member Author

Choose a reason for hiding this comment

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

Of course, I'll add using LOOKUP_TABLE in this PR.

@efimov-mikhail efimov-mikhail changed the title gh-141498: Change backoff counter to use powers of 4 instead of powers of 2 gh-141498: Change backoff counter to use prime numbers instead of powers of 2 Nov 19, 2025
Copy link
Contributor

@sergey-miryanov sergey-miryanov left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

The code looks good, but the values in the table need to be moved one position:

Instead of 6, 30, ... , 8190, 8190, 8190 it should be 1, 6, 30, ... , 8190, 8190

// We only use values x and backoffs b such that
// x + 1 is near to 2**(2*b+1) and x + 1 is prime.
static const uint16_t value_and_backoff_next[] = {
MAKE_VALUE_AND_BACKOFF(6, 1),
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be MAKE_VALUE_AND_BACKOFF(1, 1) so that code is re-specialized quickly when the cooldown counter expires.
See https://github.com/python/cpython/blob/main/Include/internal/pycore_code.h#L445

@bedevere-app
Copy link

bedevere-app bot commented Nov 20, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@efimov-mikhail
Copy link
Member Author

Oops, sorry for this mistake.
I've commited fix to values and small change for comments.

Copy link
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

Looks good now.
Thanks

@efimov-mikhail
Copy link
Member Author

CC @Fidget-Spinner

@markshannon markshannon merged commit a3b78a3 into python:main Nov 21, 2025
49 of 50 checks passed
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
ashm-dev pushed a commit to ashm-dev/cpython that referenced this pull request Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants