Repository navigation
Avoid quadratic container insertions in unions #376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| use core::ops::{Bound, RangeBounds, RangeInclusive}; | ||
|
|
||
| #[cfg(not(feature = "std"))] | ||
| use alloc::vec::Vec; | ||
|
|
||
| /// Returns the container key and the index | ||
| /// in this container for a given integer. | ||
| #[inline] | ||
|
|
@@ -61,11 +64,66 @@ where | |
| } | ||
| } | ||
|
|
||
| // Thresholds of `SortedInserts`, picked from benchmarks. | ||
| const FEW_INSERTS: usize = 8; | ||
| const SHORT_SHIFT: usize = 64; | ||
|
|
||
| /// Inserts values into a sorted `Vec` at the positions given by failed binary searches. | ||
| /// | ||
| /// Every insertion shifts the values after it, which is quadratic when many values are | ||
| /// interleaved with the existing ones. So a value is only inserted directly when that is | ||
| /// cheap: when it shifts fewer than `SHORT_SHIFT` values, or for the first `FEW_INSERTS` | ||
| /// values that shift more. The other ones are deferred and merged into the vector by | ||
| /// `finish` in a single pass. | ||
| pub struct SortedInserts<T> { | ||
| long_inserts: usize, | ||
| deferred: Vec<(usize, T)>, | ||
| } | ||
|
|
||
| impl<T> SortedInserts<T> { | ||
| pub fn new() -> Self { | ||
| SortedInserts { long_inserts: 0, deferred: Vec::new() } | ||
| } | ||
|
|
||
| /// Inserts `value` at `index` in `vec`, or defers it until `finish` is called. | ||
| /// `index` must come from a failed binary search in `vec`, and values must be | ||
| /// given in ascending order. | ||
| pub fn insert(&mut self, vec: &mut Vec<T>, index: usize, value: T) { | ||
| let short = vec.len() - index < SHORT_SHIFT; | ||
| if short || self.long_inserts < FEW_INSERTS { | ||
| // The deferred indexes stay valid: the values deferred so far belong at or | ||
| // before `index`, so before `value`. | ||
| vec.insert(index, value); | ||
| self.long_inserts += usize::from(!short); | ||
| } else { | ||
| self.deferred.push((index, value)); | ||
| } | ||
| } | ||
|
|
||
| /// Merges the deferred values into `vec`, moving the values that follow the first | ||
| /// deferred index once. | ||
| pub fn finish(self, vec: &mut Vec<T>) { | ||
| let Some(&(start, _)) = self.deferred.first() else { return }; | ||
| vec.reserve(self.deferred.len()); | ||
| let mut tail = vec.split_off(start).into_iter(); | ||
| let mut prev = start; | ||
| for (index, value) in self.deferred { | ||
| vec.extend(tail.by_ref().take(index - prev)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to depend on deferred being in sorted order: otherwise, index could be less than prev (and would panic in debug mode, do weird stuff in release mode).
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, @Dr-Emann is right here. In the I ran the fuzzer for ten minutes but didn't find anything, yet... |
||
| vec.push(value); | ||
| prev = index; | ||
| } | ||
| vec.extend(tail); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod test { | ||
| use super::{convert_range_to_inclusive, join, split, ConvertRangeError}; | ||
| use super::{convert_range_to_inclusive, join, split, ConvertRangeError, SortedInserts}; | ||
| use core::ops::Bound; | ||
|
|
||
| #[cfg(not(feature = "std"))] | ||
| use alloc::vec::Vec; | ||
|
|
||
| #[test] | ||
| fn test_split_u32() { | ||
| assert_eq!((0x0000u16, 0x0000u16), split(0x0000_0000u32)); | ||
|
|
@@ -119,4 +177,31 @@ mod test { | |
| convert_range_to_inclusive((Bound::Excluded(0), Bound::Included(0))) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_sorted_inserts() { | ||
| fn check(mut vec: Vec<u32>, values: Vec<u32>) { | ||
| let mut expected = [vec.as_slice(), values.as_slice()].concat(); | ||
| expected.sort_unstable(); | ||
|
|
||
| let mut inserts = SortedInserts::new(); | ||
| for value in values { | ||
| let index = vec.binary_search(&value).unwrap_err(); | ||
| inserts.insert(&mut vec, index, value); | ||
| } | ||
| inserts.finish(&mut vec); | ||
| assert_eq!(vec, expected); | ||
| } | ||
|
|
||
| let even: Vec<u32> = (0..1000).map(|i| 2 * i).collect(); | ||
| let odd: Vec<u32> = (0..1000).map(|i| 2 * i + 1).collect(); | ||
| check(even.clone(), odd.clone()); | ||
| check(odd.clone(), even.clone()); | ||
| check(even.clone(), (0..5).map(|i| 400 * i + 1).collect()); | ||
| check(even.clone(), (2000..3000).collect()); | ||
| check(even[500..600].to_vec(), odd[..100].iter().chain(&odd[900..]).copied().collect()); | ||
| check(even[..10].to_vec(), odd[..100].to_vec()); | ||
| check(Vec::new(), odd.clone()); | ||
| check(even, Vec::new()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not even sure to understand how it can work because a container that is not found during the first loop run must be inserted and the next loop will be able to find it and will therefore enter the
Okbranch hand this time, triggering theopfunction to merge both containers.This currently works because you use the
FEW_INSERTSandSHORT_SHIFTparameters to still insert in the target containers (and find the freshly inserted container) but it will no longer work if you inserted too many containers already or if you happen to insert too far on the left. I thought these container mergings could have been implement in theSortedInserts::finishmethod but as far as I read it I don't see anything like that.I find the idea to avoid shifts really good but the implementation doesn't seems correct to me.