Skip to content

Commit

Permalink
fix(sdk): Fix a bug in an optimisation of UpdatetoVectorDiff.
Browse files Browse the repository at this point in the history
This patch fixes a bug in an optimisation inside `UpdateToVectorDiff`
when an `Update::PushItems` is handled. It can sometimes create
`VectorDiff::Append` instead of a `VectorDiff::Insert`. The tests will
be part of the next patch.
  • Loading branch information
Hywan committed Oct 28, 2024
1 parent c23c3b9 commit e0be1e8
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions crates/matrix-sdk/src/event_cache/linked_chunk/as_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::{
collections::VecDeque,
ops::ControlFlow,
ops::{ControlFlow, Not},
sync::{Arc, RwLock},
};

Expand Down Expand Up @@ -254,6 +254,7 @@ impl UpdateToVectorDiff {
// From the `VectorDiff` “point of view”, this optimisation aims at avoiding
// removing items to push them again later.
let mut reattaching = false;
let mut detaching = false;

for update in updates {
match update {
Expand Down Expand Up @@ -344,7 +345,7 @@ impl UpdateToVectorDiff {
}

// Optimisation: we can emit a `VectorDiff::Append` in this particular case.
if is_pushing_back {
if is_pushing_back && detaching.not() {
diffs.push(VectorDiff::Append { values: items.into() });
}
// No optimisation: let's emit `VectorDiff::Insert`.
Expand Down Expand Up @@ -376,6 +377,9 @@ impl UpdateToVectorDiff {
.expect("Detach last items: The chunk is not found");

*chunk_length = new_length;

// Entering the _detaching_ mode.
detaching = true;
}

Update::StartReattachItems => {
Expand All @@ -386,6 +390,9 @@ impl UpdateToVectorDiff {
Update::EndReattachItems => {
// Exiting the _reattaching_ mode.
reattaching = false;

// Exiting the _detaching_ mode.
detaching = false;
}
}
}
Expand Down

0 comments on commit e0be1e8

Please sign in to comment.