GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-141498: Change backoff counter to use prime numbers instead of powers of 2 by efimov-mikhail · Pull Request #141591 · python/cpython

Conversation

@efimov-mikhail

@efimov-mikhail efimov-mikhail commented Nov 15, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • Issue: Change backoff counter to have larger range and backoff more quickly. #141498

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
efimov-mikhail added 4 commits November 15, 2025 13:34
@efimov-mikhail
Some refactor
86de63a
@efimov-mikhail
Changes in backoff bit pattern
4eb3231
@efimov-mikhail
Add NEWS
2581629
@efimov-mikhail
NEWS fix
96de415
@bedevere-app bedevere-app Bot added the awaiting review label Nov 15, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Nov 15, 2025
Change backoff counter to have larger range and backoff more quickly. #141498
Closed
@efimov-mikhail
efimov-mikhail requested review from brandtbucher and markshannon November 15, 2025 11:14
@efimov-mikhail
efimov-mikhail marked this pull request as draft November 15, 2025 11:30
@bedevere-app bedevere-app Bot removed the awaiting review label Nov 15, 2025
@efimov-mikhail
Change ITEMS count in test
ce3e14f
efimov-mikhail
efimov-mikhail commented Nov 15, 2025
View reviewed changes
Comment thread Lib/test/test_opcache.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@efimov-mikhail
efimov-mikhail marked this pull request as ready for review November 15, 2025 12:45
@bedevere-app bedevere-app Bot added the awaiting review label Nov 15, 2025
markshannon
markshannon reviewed Nov 19, 2025
View reviewed changes

@markshannon markshannon left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Include/internal/pycore_backoff.h Outdated
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);

@markshannon markshannon Nov 19, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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);

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

efimov-mikhail reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@markshannon markshannon Nov 19, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@efimov-mikhail efimov-mikhail Nov 19, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@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
efimov-mikhail added 14 commits November 19, 2025 16:28
@efimov-mikhail
Refactor restart_backoff_counter
e17b195
@efimov-mikhail
Remove unneed assertion and function
44b4eb8
@efimov-mikhail
Use lookup_table
6d44333
@efimov-mikhail
Use MAKE_BACKOFF_COUNTER macro
0e05712
@efimov-mikhail
Comment change
f3c9287
@efimov-mikhail
Fix Windows build: remove const keyword
e16b368
@efimov-mikhail
Simplify obtaining next backoff
6b1fe66
@efimov-mikhail
Remove forge_backoff_counter
2600236
@efimov-mikhail
Another try to fix Windows build
ec27072
@efimov-mikhail
Revert make_backoff_counter
401b6da
@efimov-mikhail
Revert changes to pycore_code.h
3418be1
@efimov-mikhail
Comment fix
ceddb6a
@efimov-mikhail
Temporarily disable assert
a89d6de
@efimov-mikhail
Revert forge_backoff_counter
c7873c2
@efimov-mikhail
Fix CI, NEWS updated
5b7bfd9
@efimov-mikhail
efimov-mikhail requested a review from markshannon November 19, 2025 17:54
sergey-miryanov
sergey-miryanov reviewed Nov 19, 2025
View reviewed changes
Comment thread Include/internal/pycore_backoff.h Resolved

Uh oh!

There was an error while loading. Please reload this page.

sergey-miryanov
sergey-miryanov approved these changes Nov 20, 2025
View reviewed changes

@sergey-miryanov sergey-miryanov left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

efimov-mikhail reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@bedevere-app bedevere-app Bot added awaiting core review and removed awaiting review labels Nov 20, 2025
markshannon
markshannon requested changes Nov 20, 2025
View reviewed changes

@markshannon markshannon left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this 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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Include/internal/pycore_backoff.h Outdated
// 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),

@markshannon markshannon Nov 20, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

efimov-mikhail reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bedevere-app bedevere-app Bot added awaiting changes and removed awaiting core review labels Nov 20, 2025
@bedevere-app

bedevere-app Bot commented Nov 20, 2025

Copy link
Copy Markdown

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@efimov-mikhail
Review addressed: change values and comment
dda4cdb
@efimov-mikhail

efimov-mikhail commented Nov 20, 2025

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

markshannon
markshannon approved these changes Nov 20, 2025
View reviewed changes

@markshannon markshannon left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

efimov-mikhail reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting changes labels Nov 20, 2025
@efimov-mikhail

efimov-mikhail commented Nov 20, 2025

Copy link
Copy Markdown
Member Author

CC @Fidget-Spinner

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@markshannon
markshannon merged commit a3b78a3 into python:main Nov 21, 2025
49 of 50 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Nov 21, 2025
StanFromIreland pushed a commit to StanFromIreland/cpython that referenced this pull request Dec 6, 2025
@efimov-mikhail @StanFromIreland
pythongh-141498: Change backoff counter to use prime numbers instead …
0fcedd2
…of powers of 2 (pythonGH-141591)
ashm-dev pushed a commit to ashm-dev/cpython that referenced this pull request Dec 8, 2025
@efimov-mikhail @ashm-dev
pythongh-141498: Change backoff counter to use prime numbers instead …
6387a5e
…of powers of 2 (pythonGH-141591)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@sergey-miryanov sergey-miryanov sergey-miryanov approved these changes
@markshannon markshannon markshannon approved these changes
@brandtbucher brandtbucher Awaiting requested review from brandtbucher

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@efimov-mikhail @sergey-miryanov @markshannon
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.