Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(im-util): SortBy optimises Remove + Insert to Set in another case #49

Merged
merged 1 commit into from
Mar 14, 2024

Conversation

Hywan
Copy link
Collaborator

@Hywan Hywan commented Mar 11, 2024

In the SortBy stream adapter, when the user is applying a observable_vector.set(…) that should result in a Remove + Insert, it sometimes can be optimised into a single Set. This happens when the old_index and the new_index (resp. the sorted index of the value to be updated, and the sorted index of the new value) are equal. But actually, when old_index is lower than new_index, there is another missed optimisation in one particular context. If old_index == new_index - 1, then we are actually updating the same location. Indeed, the code is already making it clear: when old_index < new_index, we need to subtract 1 to new_index because removing the value at old_index is shifting all values at its right, thus 1 must be subtracted to new_index. The missed opportunity for an optimisation here is when old_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).

…ther case.

In the `SortBy` stream adapter, when the user is applying a
`observable_vector.set(…)` that should result in a `Remove` + `Insert`,
it sometimes can be optimised into a single `Set`. This happens when
the `old_index` and the `new_index` (resp. the sorted index of the
value to be updated, and the sorted index of the new value) are equal.
But actually, when `old_index` is lower than `new_index`, there is
_another_ missed optimisation in one particular context. If `old_index
== new_index - 1`, then we are actually updating the same location.
Indeed, the code is already making it clear: when `old_index <
new_index`, we need to subtract 1 to `new_index` because removing the
value at `old_index` is shifting all values at its right, thus 1 must
be subtracted to `new_index`. The missed opportunity for an optimisation
here is when `old_index == new_index - 1`, where it's clear that the
same position is updated. This patch handles this situation. The tests
are updated accordingly.
@jplatte jplatte merged commit 9559347 into jplatte:main Mar 14, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants