Skip to content
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

Simplify push_pop_{max, min} and replace_{max, min} implementations #8

Merged
merged 3 commits into from
Dec 19, 2021
Merged

Simplify push_pop_{max, min} and replace_{max, min} implementations #8

merged 3 commits into from
Dec 19, 2021

Conversation

calebsander
Copy link
Contributor

@calebsander calebsander commented Sep 4, 2020

  • Use peek_{max, min}_mut() in push_pop_{max, min}() and replace_{max, min}() to reduce code duplication. Comparing the generated assembly, this seems to have a few performance effects (but I haven't benchmarked them):
    • push_pop_{max, min}() avoid swapping element to the heap if it is equal to max/min
    • push_pop_max() and replace_max() each avoid 2 bounds checks
    • push_pop_min() avoids a branch instruction
    • replace_{max, min}() avoid bubbling up the inserted value if the heap is empty
    • replace_max() swaps the min with element in a register instead of swapping two indices in the heap
    • The Peek{Max, Min}Mut struct is spilled to the stack
  • Avoid sifting a Peek{Max, Min}Mut if it was never mutably dereferenced (copied from Rust's BinaryHeap: Avoid useless sift_down when std::collections::binary_heap::PeekMut is never mutably dereferenced rust-lang/rust#75974).
  • Also fixes an overflow when dropping PeekMaxMut on a 1-element heap (which the quickcheck tests were now able to discover). Added a test for this.

@codecov-commenter
Copy link

codecov-commenter commented Sep 4, 2020

Codecov Report

Merging #8 into master will decrease coverage by 0.15%.
The diff coverage is 94.28%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master       #8      +/-   ##
==========================================
- Coverage   98.70%   98.55%   -0.16%     
==========================================
  Files           3        3              
  Lines         541      552      +11     
==========================================
+ Hits          534      544      +10     
- Misses          7        8       +1     
Impacted Files Coverage Δ
src/lib.rs 97.69% <94.28%> (-0.26%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9aba91a...25096db. Read the comment docs.

@tov
Copy link
Owner

tov commented Sep 12, 2020

Hey, just saw this! Sorry for being slow. I have very limited time right now, but from a cursory glance, it looks excellent!

@fbernier
Copy link

This PR, along with #11, look awesome. Great work @calebsander !

@codecov-io
Copy link

Codecov Report

Merging #8 (25096db) into master (9aba91a) will decrease coverage by 0.15%.
The diff coverage is 94.28%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master       #8      +/-   ##
==========================================
- Coverage   98.70%   98.55%   -0.16%     
==========================================
  Files           3        3              
  Lines         541      552      +11     
==========================================
+ Hits          534      544      +10     
- Misses          7        8       +1     
Impacted Files Coverage Δ
src/lib.rs 97.69% <94.28%> (-0.26%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9aba91a...22a4561. Read the comment docs.

Also fixes a panic when dropping PeekMaxMut on a 1-element heap
Hopefully increases coverage
@calebsander calebsander changed the title Simplify pop_max, push_pop_{max, min} and replace_{max, min} implementations Simplify push_pop_{max, min} and replace_{max, min} implementations Sep 13, 2021
@codecov-commenter
Copy link

Codecov Report

Merging #8 (5f6dd4f) into master (9aba91a) will increase coverage by 0.44%.
The diff coverage is 100.00%.

❗ Current head 5f6dd4f differs from pull request most recent head 0ba722c. Consider uploading reports for the commit 0ba722c to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master       #8      +/-   ##
==========================================
+ Coverage   98.70%   99.15%   +0.44%     
==========================================
  Files           3        3              
  Lines         541      589      +48     
==========================================
+ Hits          534      584      +50     
+ Misses          7        5       -2     
Impacted Files Coverage Δ
src/lib.rs 98.82% <100.00%> (+0.87%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d287f92...0ba722c. Read the comment docs.

@tov tov merged commit 4498b8d into tov:master Dec 19, 2021
@calebsander calebsander deleted the refactor/replace branch December 20, 2021 02:59
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.

5 participants