Skip to content

Commit bcec08e

Browse files
committed
fix(sdk): Fix a bug in an optimisation of UpdatetoVectorDiff.
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.
1 parent 887f26e commit bcec08e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

crates/matrix-sdk/src/event_cache/linked_chunk/as_vector.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use std::{
1616
collections::VecDeque,
17-
ops::ControlFlow,
17+
ops::{ControlFlow, Not},
1818
sync::{Arc, RwLock},
1919
};
2020

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

258259
for update in updates {
259260
match update {
@@ -344,7 +345,7 @@ impl UpdateToVectorDiff {
344345
}
345346

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

378379
*chunk_length = new_length;
380+
381+
// Entering the _detaching_ mode.
382+
detaching = true;
379383
}
380384

381385
Update::StartReattachItems => {
@@ -386,6 +390,9 @@ impl UpdateToVectorDiff {
386390
Update::EndReattachItems => {
387391
// Exiting the _reattaching_ mode.
388392
reattaching = false;
393+
394+
// Exiting the _detaching_ mode.
395+
detaching = false;
389396
}
390397
}
391398
}

0 commit comments

Comments
 (0)