- // We just need any empty iterator. We don't even want to
- // yield the element that surrounds this position, nor do
- // any splitting.
- &mut []
- } else {
- // Make sure we got a clear beginning
- let mut first_idx = self.find_offset(offset);
- if self.split_index(first_idx, offset) {
- // The newly created 2nd element is ours
- first_idx += 1;
- }
- let first_idx = first_idx; // no more mutation
- // Find our end. Linear scan, but that's okay because the iteration
- // is doing the same linear scan anyway -- no increase in complexity.
- // We combine this scan with a scan for duplicates that we can merge, to reduce
- // the number of elements.
- let mut equal_since_idx = first_idx;
- let mut end_idx = first_idx; // when the loop is done, this is the first excluded element.
- loop {
- // Compute if `end` is the last element we need to look at.
- let done = (self.v[end_idx].range.end >= offset+len);
- // We definitely need to include `end`, so move the index.
- end_idx += 1;
- debug_assert!(done || end_idx < self.v.len(), "iter_mut: end-offset {} is out-of-bounds", offset+len);
- // see if we want to merge everything in `equal_since..end` (exclusive at the end!)
+ // We just need any empty iterator. We don't even want to
+ // yield the element that surrounds this position, nor do
+ // any splitting.
+ &mut []
+ } else {
+ // Make sure we got a clear beginning
+ let mut first_idx = self.find_offset(offset);
+ if self.split_index(first_idx, offset) {
+ // The newly created 2nd element is ours
+ first_idx += 1;
+ }
+ // No more mutation.
+ let first_idx = first_idx;
+ // Find our end. Linear scan, but that's ok because the iteration
+ // is doing the same linear scan anyway -- no increase in complexity.
+ // We combine this scan with a scan for duplicates that we can merge, to reduce
+ // the number of elements.
+ // We stop searching after the first "block" of size 1, to avoid spending excessive
+ // amounts of time on the merging.
+ let mut equal_since_idx = first_idx;
+ // Once we see too many non-mergeable blocks, we stop.
+ // The initial value is chosen via... magic. Benchmarking and magic.
+ let mut successful_merge_count = 3usize;
+ // When the loop is done, this is the first excluded element.
+ let mut end_idx = first_idx;
+ loop {
+ // Compute if `end` is the last element we need to look at.
+ let done = self.v[end_idx].range.end >= offset + len;
+ // We definitely need to include `end`, so move the index.
+ end_idx += 1;
+ debug_assert!(
+ done || end_idx < self.v.len(),
+ "iter_mut: end-offset {} is out-of-bounds",
+ offset + len
+ );
+ // see if we want to merge everything in `equal_since..end` (exclusive at the end!)
+ if successful_merge_count > 0 {