Hacker News new | past | comments | ask | show | jobs | submit login

Thank you, Rust standard library maintainers, for making it very easy for me to contribute `BinaryHeap::peek_mut` to the stdlib, which landed in stable today!



Nice contribution!

I was just browsing the apidocs of peek_mut. Can you share a motivating use case? It wasn't immediately obvious to me.


This summer I was working on a userspace network I/O scheduler in Rust, and to avoid allocations, modifying the top element in the heap in-place was necessary. It dramatically improved performance of my scheduler.


I imagine if every item in the heap is a queue itself, you could use peek mut to get an item off the current max queue. After the modification, the former max element is sifted down to account for it's (probably) changed key.


The documentation really should mention the resifting, and why this is useful. It only mentions that the heap may be inconsistent if the `PeekMut` isn't properly dropped, without explaining why.

It's basically replacing the former (never stabilized) `push_pop` and `replace`, which Python folks can relate to similar methods in their `heapq`, for instance. With separate `push` and `pop` calls, the heap must be sifted twice, but with `peek_mut` you can update the head in-place and sift only once.


I agree-- you should send in a PR to the docs!


https://github.com/rust-lang/rfcs/issues/1626

Looks like it's to do something with the top data without the overhead of pop()+push()

Edit: ninja'd


Rust enthusiast here - thanks for contributing!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: