Skip to content

Replace iterative capacity growth with next_power_of_two#1216

Open
SaiCharanMarrivada wants to merge 3 commits intocrossbeam-rs:masterfrom
SaiCharanMarrivada:master
Open

Replace iterative capacity growth with next_power_of_two#1216
SaiCharanMarrivada wants to merge 3 commits intocrossbeam-rs:masterfrom
SaiCharanMarrivada:master

Conversation

@SaiCharanMarrivada
Copy link
Copy Markdown

Replace the capacity-doubling loop with (reserve_cap + len).next_power_of_two(). Given the existing invariant that capacity is a power of two, the result is identical.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR replaces an iterative capacity-doubling loop with a direct calculation using next_power_of_two() in the deque's reserve function. This is a straightforward optimization that maintains the same behavior while being more efficient and readable.

Changes:

  • Replaced the capacity-doubling loop (lines 337-340 in the original code) with a single call to (reserve_cap + len).next_power_of_two()
  • Updated the inline comment to reflect the new approach

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@SaiCharanMarrivada
Copy link
Copy Markdown
Author

To ensure that replacing the iterative capacity growth with next_power_of_two() maintains exact semantic parity and handles edge cases correctly, I have verified the transformation using Alive2.

  • LLVM-IR Generation: I’ve isolated the logic in Compiler Explorer to generate the IR for both the original loop and the new implementation.
  • Alive2 Proof: You can view the formal equivalence proof here: Alive2 Verification.

Note on Verification Scope:
I used u32 for the proof to ensure the solver completes in a reasonable time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants