3 ms·
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!
by moosingin3space 10y ago
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!
- AsyncAwait 10y agoRust enthusiast here - thanks for contributing!
- exDM69 10y agoNice contribution! I was just browsing the apidocs of peek_mut. Can you share a motivating use case? It wasn't immediately obvious to me.
- moosingin3space 10y agoThis 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.
- kzrdude 10y agoI 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.
- CUViper 10y agoThe 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.
- carols10cents 10y agoI agree-- you should send in a PR to the docs!
- mastax 10y agohttps://github.com/rust-lang/rfcs/issues/1626 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