From ce37f0a35559fb5396881f3d9a547f161b1e822d Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Thu, 26 Aug 2021 11:45:25 -0700 Subject: [PATCH] Add comments --- compiler/rustc_index/src/bit_set.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/compiler/rustc_index/src/bit_set.rs b/compiler/rustc_index/src/bit_set.rs index 989d56b0528..610421f8060 100644 --- a/compiler/rustc_index/src/bit_set.rs +++ b/compiler/rustc_index/src/bit_set.rs @@ -247,6 +247,8 @@ impl BitRelations> for BitSet { } } +// Applies a function to mutate a bitset, and returns true if any +// of the applications return true fn sequential_update( mut self_update: impl FnMut(T) -> bool, it: impl Iterator, @@ -258,6 +260,8 @@ fn sequential_update( changed } +// Optimization of intersection for SparseBitSet that's generic +// over the RHS fn sparse_intersect( set: &mut SparseBitSet, other_contains: impl Fn(&T) -> bool, @@ -267,6 +271,10 @@ fn sparse_intersect( set.elems.len() != size } +// Optimization of dense/sparse intersection. The resulting set is +// guaranteed to be at most the size of the sparse set, and hence can be +// represented as a sparse set. Therefore the sparse set is copied and filtered, +// then returned as the new set. fn dense_sparse_intersect( dense: &BitSet, sparse: &SparseBitSet, @@ -303,6 +311,10 @@ impl BitRelations> for BitSet { match other { HybridBitSet::Sparse(sparse) => { let (updated, changed) = dense_sparse_intersect(self, sparse); + + // We can't directly assign the BitSet to the SparseBitSet, and + // doing `*self = updated.to_dense()` would cause a drop / reallocation. Instead, + // the BitSet is cleared and `updated` is copied into `self`. self.clear(); for elem in updated.iter() { self.insert(*elem);