jeffparsons / rangemap

Map data structure whose keys are stored as ranges

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RangeInclusiveMap does not reliably coalesce contiguous ranges

nmathewson opened this issue · comments

Here is a simple example of a test program that doesn't work for me, using rangemap 0.1.8

fn main() {
    let mut map: RangeInclusiveMap<u16, u16, _> = RangeInclusiveMap::new();

    map.insert(99..=200, 7);
    map.insert(190..=200, 6);
    map.insert(190..=200, 7);

    dbg!(map);
}

I expect the output to be:

map = {
      99..=200: 7,
}

But instead I get:

[src/main.rs:11] map = {
    99..=189: 7,
    190..=200: 7,
}

It appears that there is a similar issue with RangeMap. For example, you can try:

  map.insert(58..105, 65);
  map.insert(65..105, 36);
  map.insert(65..105, 65);

Thanks for reporting this, @nmathewson. I might not get to this promptly (owing to current hectic $LIFE circumstances) but I will take a look when I get a chance.

I'd very happily merge a fix if one were to materialise in the meantime. 😊

There are at least two different bugs here. One was pretty shallow, but the other is making me hate how complex the code is. I feel like it should be possible to make it a lot simpler, so I'm going to take a swing at that in the hope of leaving fewer places for these kinds of bugs to hide .

@nmathewson By very lucky chance I found a chance to look at this tonight. 😃 I've published version 0.1.9 with the bug fixed.

As I commented on d1999f4, I'm not super-happy with this fix because it highlights to me that the code is too complex/fiddly. I think there's a simpler version of it that would do away with all the edge cases (using "poor engineer's faux cursors"), but that's more of a rewrite than a quick fix.

Thanks for the fix, @jeffparsons !

I've run the fuzzers for a while and confirmed I can no longer reproduce this bug. Now I can use rangemap directly in my code without workarounds! 👍

One question: is it possible that this has caused a performance regression? The tool I have been writing seems to be much slower with the new version.

I might have a fix for the performance regression, but I need to read a little first...

I've made a PR for the performance regression fix as #27 . Thanks!