From e0be1e8e32e68af87bf1914dfd58cd3fc387a906 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 23 Oct 2024 14:15:48 +0200 Subject: [PATCH] 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. --- .../src/event_cache/linked_chunk/as_vector.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/matrix-sdk/src/event_cache/linked_chunk/as_vector.rs b/crates/matrix-sdk/src/event_cache/linked_chunk/as_vector.rs index a7c5416ce..a42cee508 100644 --- a/crates/matrix-sdk/src/event_cache/linked_chunk/as_vector.rs +++ b/crates/matrix-sdk/src/event_cache/linked_chunk/as_vector.rs @@ -14,7 +14,7 @@ use std::{ collections::VecDeque, - ops::ControlFlow, + ops::{ControlFlow, Not}, sync::{Arc, RwLock}, }; @@ -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 { @@ -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`. @@ -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 => { @@ -386,6 +390,9 @@ impl UpdateToVectorDiff { Update::EndReattachItems => { // Exiting the _reattaching_ mode. reattaching = false; + + // Exiting the _detaching_ mode. + detaching = false; } } }