@@ -253,7 +253,7 @@ impl UpdateToVectorDiff {
253
253
//
254
254
// From the `VectorDiff` “point of view”, this optimisation aims at avoiding
255
255
// removing items to push them again later.
256
- let mut mute_push_items = false ;
256
+ let mut reattaching = false ;
257
257
258
258
for update in updates {
259
259
match update {
@@ -329,18 +329,22 @@ impl UpdateToVectorDiff {
329
329
}
330
330
331
331
Update :: PushItems { at : position, items } => {
332
+ let number_of_chunks = self . chunks . len ( ) ;
332
333
let ( offset, ( chunk_index, chunk_length) ) = self . map_to_offset ( position) ;
333
334
334
- // Update the length of the chunk in `self.chunks`.
335
+ let is_pushing_back =
336
+ chunk_index + 1 == number_of_chunks && position. index ( ) >= * chunk_length;
337
+
338
+ // Add the number of items to the chunk in `self.chunks`.
335
339
* chunk_length += items. len ( ) ;
336
340
337
- // See `mute_push_items ` to learn more.
338
- if mute_push_items {
341
+ // See `reattaching ` to learn more.
342
+ if reattaching {
339
343
continue ;
340
344
}
341
345
342
346
// Optimisation: we can emit a `VectorDiff::Append` in this particular case.
343
- if chunk_index + 1 == self . chunks . len ( ) {
347
+ if is_pushing_back {
344
348
diffs. push ( VectorDiff :: Append { values : items. into ( ) } ) ;
345
349
}
346
350
// No optimisation: let's emit `VectorDiff::Insert`.
@@ -375,13 +379,13 @@ impl UpdateToVectorDiff {
375
379
}
376
380
377
381
Update :: StartReattachItems => {
378
- // Entering the `reattaching` mode.
379
- mute_push_items = true ;
382
+ // Entering the _reattaching_ mode.
383
+ reattaching = true ;
380
384
}
381
385
382
386
Update :: EndReattachItems => {
383
- // Exiting the `reattaching` mode.
384
- mute_push_items = false ;
387
+ // Exiting the _reattaching_ mode.
388
+ reattaching = false ;
385
389
}
386
390
}
387
391
}
@@ -411,14 +415,11 @@ impl UpdateToVectorDiff {
411
415
412
416
// Chunk has not been found.
413
417
ControlFlow :: Continue ( ..) => {
414
- // SAFETY: Assuming `LinkedChunk` and `ObservableUpdates` are
415
- // not buggy, and assuming
416
- // `Self::chunks` is correctly initialized, it
417
- // is not possible to push items on a chunk that does not exist.
418
- // If this predicate fails,
419
- // it means `LinkedChunk` or
420
- // `ObservableUpdates` contain a bug.
421
- panic ! ( "Pushing items: The chunk is not found" ) ;
418
+ // SAFETY: Assuming `LinkedChunk` and `ObservableUpdates` are not buggy, and
419
+ // assuming `Self::chunks` is correctly initialized, it is not possible to work
420
+ // on a chunk that does not exist. If this predicate fails, it means
421
+ // `LinkedChunk` or `ObservableUpdates` contain a bug.
422
+ panic ! ( "The chunk is not found" ) ;
422
423
}
423
424
}
424
425
} ;
0 commit comments