Skip to content

Tracking issue for map_first_last: first/last methods on BTreeSet and BTreeMap #62924

Closed

Description

@nexplor

View all comments

Feature gate: #![feature(map_first_last)]

Public API

impl<T> BTreeSet<T> {
    pub fn first(&self) -> Option<&T> where T: Ord;
    pub fn last(&self) -> Option<&T> where T: Ord;
    pub fn pop_first(&mut self) -> Option<T> where T: Ord;
    pub fn pop_last(&mut self) -> Option<T> where T: Ord;
}

impl<K, V> BTreeMap<K, V> {
    pub fn first_key_value(&self) -> Option<(&K, &V)> where K: Ord;
    pub fn last_key_value(&self) -> Option<(&K, &V)> where K: Ord;
    pub fn first_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> where K: Ord;
    pub fn last_entry(&mut self) -> Option<OccupiedEntry<'_, K, V>> where K: Ord;
    pub fn pop_first(&mut self) -> Option<(K, V)> where K: Ord;
    pub fn pop_last(&mut self) -> Option<(K, V)> where K: Ord;
}

Steps / History

Unresolved Questions

  • Naming (min/max, first/last, front/back, .. ?)
  • For BTreeMap: Separate entry and pair (and just-value) functions?

Original issue title: Finding minimum/maximum element in BTreeMap does twice the required amount of computation

Original issue text:

The standard way of finding the minimum element in BTreeMap is tree_map.iter().next()

The iter() function creates a btree_map::Iter, which finds both the minimum and maximum element to create its internal instance of btree_map::Range.

This is unnecessary, and a BTreeMap::get_min or similar function could be provided, which would just internally call the first_leaf_edge private function, without needing to call last_leaf_edge.

Ditto for finding the maximum element.

Activity

  1. changed the title [-]Finding inimum/maximum element in BTreeMap does twice the required computation[/-] [+]Finding minimum/maximum element in BTreeMap does twice the required amount of computation[/+] on Jul 24, 2019
  2. added
    A-collectionsArea: `std::collections` (prefer A-binaryheap, A-btree, A-hashtable, A-linkedlist, A-vecdeque)
    T-libs-api[DEPRECATED; DO NOT USE]
    I-slowIssue: Problems and improvements with respect to performance of generated code.
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    on Jul 24, 2019
  3. cuviper commented on Jul 24, 2019

    @cuviper
    Member

    Here are what the other collections call similar methods:

    • [T] slices (and Vec via deref) have first/first_mut and last/last_mut.
    • VecDeque and LinkedList have front/front_mut and back/back_mut.
    • BinaryHeap has peek and peek_mut, i.e. only from the front.
    • HashMap and HashSet are unordered, and have no such methods.
    • str and String have no such methods.
  4. cuviper commented on Jul 24, 2019

    @cuviper
    Member

    #59947 is related -- Iterator::min/max are even worse since they naively walk everything.

  5. ssomers commented on Oct 19, 2019

    @ssomers
    Contributor

    #64820 is also related - BTreeSet is_subset, intersection and difference now always do this double work (they actually want both min and max at the same time, but requesting both next and next_back from the same iterator that may only see one element is not the most straightforward code).

    I naively thought the redundant data (the other end and iterator length) and code would all get optimized away from an expression like tree_map.iter().next(), but after writing out code to avoid the double work in BTreeSet::is_subset, benchmarks report a 20 to 40% speed boost, for the cases where it can quickly bail out after getting min and max either side and comparing them. I'm going to measure the effect by adding the desired functions to map, and if successful I can put them in a pull request, but I have no idea what the procedure is for adding APIs. I vote for first and last as names.

  6. ssomers commented on Oct 20, 2019

    @ssomers
    Contributor

    According to a raw benchmark, the specialized functions are about 3 times faster than .iter().next(). By the way, .range(..).next() is a little slower for small sets, despite not bothering with iterator length, presumably because iter() has access to internals to construct its unbounded range.

    Details: benchmark code in liballoc/benches/btree/map.rs:

    fn bench_first_and_last(b: &mut Bencher, size: i32) {
        let map: BTreeMap<_, _> = (0..size).map(|i| (i, i)).collect();
        b.iter(|| {
            for _ in 0..10 {
                black_box(map.first_key_value());
                black_box(map.last_key_value());
            }
        });
    }
    
    #[bench]
    pub fn first_and_last_0(b: &mut Bencher) {
        bench_first_and_last(b, 0);
    }
    
    #[bench]
    pub fn first_and_last_100(b: &mut Bencher) {
        bench_first_and_last(b, 100);
    }
    
    #[bench]
    pub fn first_and_last_10k(b: &mut Bencher) {
        bench_first_and_last(b, 10_000);
    }
    

    Results using the new functions:

    test btree::map::first_and_last_0                        ... bench:          14 ns/iter (+/- 0)
    test btree::map::first_and_last_100                      ... bench:          37 ns/iter (+/- 2)
    test btree::map::first_and_last_10k                      ... bench:          54 ns/iter (+/- 3)
    
    test btree::map::first_and_last_0                        ... bench:          14 ns/iter (+/- 0)
    test btree::map::first_and_last_100                      ... bench:          42 ns/iter (+/- 1)
    test btree::map::first_and_last_10k                      ... bench:          56 ns/iter (+/- 2)
    
    test btree::map::first_and_last_0                        ... bench:          14 ns/iter (+/- 0)
    test btree::map::first_and_last_100                      ... bench:          37 ns/iter (+/- 1)
    test btree::map::first_and_last_10k                      ... bench:          54 ns/iter (+/- 2)
    
    test btree::map::first_and_last_0                        ... bench:          14 ns/iter (+/- 0)
    test btree::map::first_and_last_100                      ... bench:          37 ns/iter (+/- 0)
    test btree::map::first_and_last_10k                      ... bench:          55 ns/iter (+/- 1)
    

    Results using .iter().next() / .iter().next_back():

    test btree::map::first_and_last_0                        ... bench:          65 ns/iter (+/- 1)
    test btree::map::first_and_last_100                      ... bench:          97 ns/iter (+/- 3)
    test btree::map::first_and_last_10k                      ... bench:         141 ns/iter (+/- 0)
    
    test btree::map::first_and_last_0                        ... bench:          64 ns/iter (+/- 1)
    test btree::map::first_and_last_100                      ... bench:          97 ns/iter (+/- 3)
    test btree::map::first_and_last_10k                      ... bench:         131 ns/iter (+/- 4)
    
    test btree::map::first_and_last_0                        ... bench:          65 ns/iter (+/- 2)
    test btree::map::first_and_last_100                      ... bench:          96 ns/iter (+/- 3)
    test btree::map::first_and_last_10k                      ... bench:         131 ns/iter (+/- 7)
    
    test btree::map::first_and_last_0                        ... bench:          65 ns/iter (+/- 2)
    test btree::map::first_and_last_100                      ... bench:          99 ns/iter (+/- 2)
    test btree::map::first_and_last_10k                      ... bench:         131 ns/iter (+/- 15)
    

    Results using .range(..).next() / .range(..).next_back():

    test btree::map::first_and_last_0                        ... bench:          66 ns/iter (+/- 3)
    test btree::map::first_and_last_100                      ... bench:         100 ns/iter (+/- 1)
    test btree::map::first_and_last_10k                      ... bench:         123 ns/iter (+/- 2)
    
    test btree::map::first_and_last_0                        ... bench:          67 ns/iter (+/- 5)
    test btree::map::first_and_last_100                      ... bench:         100 ns/iter (+/- 4)
    test btree::map::first_and_last_10k                      ... bench:         127 ns/iter (+/- 4)
    
    test btree::map::first_and_last_0                        ... bench:          66 ns/iter (+/- 1)
    test btree::map::first_and_last_100                      ... bench:         101 ns/iter (+/- 6)
    test btree::map::first_and_last_10k                      ... bench:         123 ns/iter (+/- 5)
    
  7. added a commit that references this issue on Nov 13, 2019
  8. matklad commented on Dec 26, 2019

    @matklad
    Contributor

    I vote for min/max rather than first/last terminology. Rationale: I’ve recently had a bad bug when I used Rust’s BinaryHeap and assumed that pop yields a minimal item. If the API had used max in the method name, I wouldn’t have made the bug.

    This case is similar: first is ambiguous, min is precise (and shorter ^^).

  9. ssomers commented on Dec 26, 2019

    @ssomers
    Contributor

    I'm slightly in favour of min/max at the moment, having faced reverse iteration in (other) code with first/last names. It's a close call; min/max is ambiguous about performance, because many min/max functions out there are in fact linear searches. PS then again, that argument makes last double ambiguous, because last on an iterator can be sluggish (meticulously consume an iterator) or instant (on a well-behaved implementation of DoubleEndedIterator).

  10. matklad commented on Dec 26, 2019

    @matklad
    Contributor

    Correctness is more important than performance, and the performance ambiguity is an a safe direction (operation is faster than it might seem)

  11. jmlMetaswitch commented on Mar 16, 2020

    @jmlMetaswitch

    Can we also get pop forms that remove the entry at the same time? (I think that's probably better than take or other synonyms, especially as BTreeSet has pop_last.) They are particularly useful in algorithms that repeatedly process the max/min and may add entries during that processing (e.g A*). I think that suggests (from the above) max_key_value, max_entry, pop_max_key_value and the equivalent for min.

  12. ssomers commented on Mar 16, 2020

    @ssomers
    Contributor

    I didn't add those back then because I thought they were intended to be used through the entry API. But using that in practice seems more complicated than I would want. There's no entry concept in BTreeSet, so pop_X seemed the only option there.

    I wouldn't start mixing the naming scheme though. First/last or min/max (or something else) all the way.

    I don't remember any discussion on whether it should be pop or take (or poll, anyone?) but there are lots of similar pops in Rust's standard library, the existing takes on sets work with a particular key, and the other takes seem to grab the entire contents.

  13. 91 remaining items

  14. added a commit that references this issue on Oct 11, 2022
    9bf2e53
  15. added a commit that references this issue on Oct 11, 2022
    cadb37a
  16. added a commit that references this issue on Oct 12, 2022
    34ac279
  17. added 2 commits that reference this issue on Jan 9, 2023
    984150b
    7c2f756
  18. added a commit that references this issue on Feb 10, 2023
    4667b94
  19. added a commit that references this issue on Apr 20, 2024
    e0e31e8
  20. added a commit that references this issue on Apr 27, 2024
    78dae46
  21. added
    A-btreeArea: `std::collections::{BTreeMap, BTreeSet}`
    and removed
    A-collectionsArea: `std::collections` (prefer A-binaryheap, A-btree, A-hashtable, A-linkedlist, A-vecdeque)
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-btreeArea: `std::collections::{BTreeMap, BTreeSet}`C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCI-slowIssue: Problems and improvements with respect to performance of generated code.T-libs-api[DEPRECATED; DO NOT USE]disposition-mergeThis issue / PR is in PFCP or FCP with a disposition to merge it.finished-final-comment-periodThe final comment period is finished for this PR / Issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions