Skip to content
This repository was archived by the owner on Jul 23, 2019. It is now read-only.

Optimize add_selection_above and add_selection_below#79

Merged
as-cii merged 5 commits intomasterfrom
speedup-add-selection-above-below
May 8, 2018
Merged

Optimize add_selection_above and add_selection_below#79
as-cii merged 5 commits intomasterfrom
speedup-add-selection-above-below

Conversation

@as-cii
Copy link
Copy Markdown
Contributor

@as-cii as-cii commented May 7, 2018

Previously, dispatching the "Add Selection Above/Below" command was unusable even when the number of existing selections was relatively low (e.g. < 1000 selections). This pull request introduces a few commits that improve the situation and allow to use this command even with thousands of selections (in my tests, we don't drop any frame even with > 2000 selections):

  • Use only one malloc and move selections only once when merging them (67b8821)
  • Use SeaHash for position caches in Buffer (af545d2)
  • Introduce Buffer::insert_selections to efficiently insert a (not necessarily sorted) list of selections (b75e52c)

Other than some local tests, I have also introduced a benchmark that proves the above changes have a positive effect on the Buffer::add_selection_above and Buffer::add_selection_below methods:

benchmark

Note that, for speed purposes, I have kept the number of selections in the benchmark relatively low. Even in that scenario the proposed changes make a significant difference, but the improvements increase even more when the number of selections gets higher (i.e. thousands as opposed to hundreds of selections). This is largely due to memmove calls being still quite efficient when Vec is small.

/cc: @nathansobo

Antonio Scandurra added 5 commits May 4, 2018 15:01
Instead of using `Vec::remove` when merging adjacent selections, we now
construct a new Vec with the merged selections. In the worst case, this
corresponds to a single movement of memory from a vector to another and
a single malloc. Using `Vec::remove` repeatedly, on the other hand, was
causing the vector to be constantly shifted around, which starts
becoming slow when the number of selections increases dramatically. In
my tests, this change does not introduce slowdowns when dealing with a
small number of selections.
This change was driven by the hashing functions showing up in theprofiler. Since we don't need a cryptographic hash function in that codepath, this commit switches to SeaHash which seems to be much faster thanSipHash (Rust default hasher).
This method is very similar to Buffer::mutate_selections in that it
allows to manipulate selection sets by supplying an id. The only
difference with the latter is that it gives users of this API a readonly
slice of the existing selections and allows to return a (not necessarily
ordered) vector of new selections to insert.
@as-cii as-cii merged commit b9883a1 into master May 8, 2018
@as-cii as-cii deleted the speedup-add-selection-above-below branch May 8, 2018 14:22
@nathansobo
Copy link
Copy Markdown

Nice work on this ⚡️

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants