feat(im-util): SortBy
optimises Remove
+ Insert
to Set
in another case
#49
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In the
SortBy
stream adapter, when the user is applying aobservable_vector.set(…)
that should result in aRemove
+Insert
, it sometimes can be optimised into a singleSet
. This happens when theold_index
and thenew_index
(resp. the sorted index of the value to be updated, and the sorted index of the new value) are equal. But actually, whenold_index
is lower thannew_index
, there is another missed optimisation in one particular context. Ifold_index == new_index - 1
, then we are actually updating the same location. Indeed, the code is already making it clear: whenold_index < new_index
, we need to subtract 1 tonew_index
because removing the value atold_index
is shifting all values at its right, thus 1 must be subtracted tonew_index
. The missed opportunity for an optimisation here is whenold_index == new_index - 1
, where it's clear that the same position is updated. This patch handles this situation. The tests are updated accordingly.Thanks @jplatte for having found this in #46 (comment).