bodil / im-rs

Assorted immutable collection datatypes for Rust
http://immutable.rs/
Mozilla Public License 2.0
1.49k stars 111 forks source link

Separate the lifetimes of each input of OrdMap::diff and OrdSet::diff #168

Open SimonSapin opened 3 years ago

SimonSapin commented 3 years ago

This is a breaking change but only for code that uses turbofish syntax on the diff method, the DiffIter type, or the DiffItem type.


Motivation:

We use diff in an algorithm that merges two maps similar to union_with_key, but tries to maximize sharing of internal nodes between similar maps. See "Stage 2" in https://github.com/bodil/im-rs/issues/166. This works by collecting the results of diff into two Vecs of (key, value) pairs that would need to be inserted in either input map in order to turn it into the desired merge/union. After that is done, we pick the smaller set of updates in order to decide which of the input maps to mutate.

At the moment these Vec contain cloned keys and cloned values. With this PR, they could instead be borrowed with each Vec only borrowing from one of the two input maps. After we’ve decided which Vec to use, only its contents needs to be cloned for inserting into the mutated map.

The full algorithm is at https://foss.heptapod.net/octobus/mercurial-devel/-/commit/53e9ccfaacd492852419cb9dca1abae1765a7474#1e7b6882ee05cf3bc9882ed81b2a0a8dac61ded6_179_182