Skip to content
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

feat(im-util): SortBy optimises Remove + Insert to Set in another case #49

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions eyeball-im-util/src/vector/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,28 @@ where
// Remove value at `old_index`, and insert the new value at `new_index - 1`: we need
// to subtract 1 because `old_index` has been removed before `new_insert`, which
// has shifted the indices.
//
// SAFETY: `new_index - 1` won't underflow because `new_index` is necessarily
// greater than `old_index` here. `old_index` cannot be lower than 0, so
// `new_index` cannot be lower than 1, hence `new_index - 1` cannot be lower
// than 0.
Ordering::Less => {
buffered_vector.remove(old_index);
buffered_vector.insert(new_index - 1, (new_unsorted_index, new_value.clone()));
let new_index = new_index - 1;
let new_unsorted_index_with_value = (new_unsorted_index, new_value.clone());

result.push(VectorDiff::Remove { index: old_index });
result.push(VectorDiff::Insert { index: new_index - 1, value: new_value });
// If `old_index == new_index`, we are clearly updating the same index.
// Then, let's emit a `VectorDiff::Set`.
if old_index == new_index {
buffered_vector.set(old_index, new_unsorted_index_with_value);

result.push(VectorDiff::Set { index: old_index, value: new_value });
} else {
buffered_vector.remove(old_index);
buffered_vector.insert(new_index, new_unsorted_index_with_value);

result.push(VectorDiff::Remove { index: old_index });
result.push(VectorDiff::Insert { index: new_index, value: new_value });
}
}
// `old_index` is the same as `new_index`.
Ordering::Equal => {
Expand Down
10 changes: 9 additions & 1 deletion eyeball-im-util/tests/it/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,18 @@ fn set() {
ob.set(0, 'd');
assert_next_eq!(sub, VectorDiff::Set { index: 1, value: 'd' });

// Another value, that is sorted at the same index.
// Another value, that is sorted at the same sorted index: `d` is at the sorted
// index 1, and `c` is at the sorted index 1 too. The `VectorDiff::Remove` +
// `VectorDiff::Insert` are optimised as a single `VectorDiff::Set`.
ob.set(0, 'c');
assert_next_eq!(sub, VectorDiff::Set { index: 1, value: 'c' });

// Another value, that is sorted at an adjacent sorted index: `c` is at the
// sorted index 1, but `d` is at the sorted index 2. The `VectorDiff::Remove` +
// `VectorDiff::Insert` are optimised as a single `VectorDiff::Set`.
ob.set(0, 'd');
assert_next_eq!(sub, VectorDiff::Set { index: 1, value: 'd' });

// Another value, that is moved to the left.
ob.set(0, 'a');
assert_next_eq!(sub, VectorDiff::Remove { index: 1 });
Expand Down