Skip to content

move nextAvail back as much as possible #199

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

symious
Copy link
Contributor

@symious symious commented Mar 20, 2025

The original code won't move the nextAvail.

Say we have 5 slots, and all allocated.
T1. nextAvail = 5, free = {}
T2. dealloc(2), nextAvail = 5, free = {2}
T3. dealloc(3), nextAvail = 5, free = {2, 3}
T4. dealloc(4)

  1. 4 == nextAvail - 1, so check while
  2. free.find(--nextAvail), which is free.find(4) is free.end(), so returned directly.

Or is it because of performance issue to remove the free.erase logic here? If so we don't need to while check here.

while (free.find(--nextAvail) != free.end()) {
// move back next avail as much as possible
;
nextAvail--;
Copy link
Collaborator

Choose a reason for hiding this comment

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

We prefer the prefix operator here when the result is not needed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You're right. But for a tiny style issue, I think the fix is good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@echaozh Thank you for the review, updated the PR, PTAL.

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.

2 participants