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

Support for Range::union_many or similar #249

Closed
zanieb opened this issue Aug 18, 2024 · 3 comments · Fixed by #278
Closed

Support for Range::union_many or similar #249

zanieb opened this issue Aug 18, 2024 · 3 comments · Fixed by #278

Comments

@zanieb
Copy link
Member

zanieb commented Aug 18, 2024

I find myself constructing a range in a loop with union, which we worry is not performant. It'd be nice to have a union_many that takes an iterable of ranges and unions them in a single efficient operation.

Some example usage

https://github.com/astral-sh/uv/blob/0335efd0a901ee2351e663d0488b1ce87fd4cc80/crates/pep508-rs/src/marker/algebra.rs#L638-L645

https://github.com/astral-sh/uv/blob/e1a8beb64b9f6eb573b3af616ffff680c282a316/crates/uv-resolver/src/pubgrub/report.rs#L1098-L1102

I started to take a look at this myself, but realized I'm out of my depth :)

@Eh2406
Copy link
Member

Eh2406 commented Aug 21, 2024

Yes, union is linear in the length of the ranges being merged so doing it in a loop is quadratic.
The fundamental union algorithm is:

  1. Create a combined list of all of the segments from all inputs.
  2. Sort this list based on segments start.
  3. Merge consecutive overlapping segments.
  4. Declare what's left the union range.

Some optimizations can be done with the sort because we know the inputs are themselves sorted. Similarly we can duplicate the list as we constructe it for the classic 2 argument case.

A union_many could use a binary heap to find the next segment to process.

@mpizenberg
Copy link
Member

mpizenberg commented Aug 22, 2024

Well summarized!

It’s been a long time since I looked at this. I suppose a rather simple adaptation of the current code would just need to adjust the smaller_interval selection among a list of segments iterators (one per range). Instead of just checking the "left" and "right", it would check every iterator head.

let smaller_interval = match (left_iter.peek(), right_iter.peek()) {

That would be better than repeatedly computing union, but still not optimal. An optimal one I guess would also remember how the iterator heads are sorted among themselves. Is that what you meant with the binary heap @Eh2406 ?

@Eh2406
Copy link
Member

Eh2406 commented Aug 22, 2024

Yes. smaller_interval is "among all non-exhausted iterators pick the first segment from the one who's got the smallest start point". If we had a IterWithOrdBySmallestStart then we could calculate smaller_interval by popping a BinaryHeap<IterWithOrdBySmallestStart>. If the IterWithOrdBySmallestStart is not exhausted then re-add what's left to the BinaryHeap. If the binary keep is empty then you're done.

konstin added a commit to astral-sh/pubgrub that referenced this issue Nov 4, 2024
## Motivation

https://github.com/astral-sh/uv/pull/8797/files#diff-17170cd58cc6474d6c6ee0413af4b3b28610aca267bbe7863411627219c26549R232 needs to modify an existing `Ranges`. We don't want to allow direct access to ranges since this allows breaking the version-ranges invariants. This led me to two questions: How do we best construct ranges from a number of versions (pubgrub-rs#249), does the user need to hold up the invariants or do we sort and merge the given ranges? And: What are our invariants?

Given my previous profiling, construction ranges isn't nearly noticeable in benchmarks (but i'm gladly proven otherwise), the input ranges are too small and we do orders of magnitude more ranges constructions in the pubgrub algorithm itself, so the primary problem of https://github.com/astral-sh/uv/pull/8797/files#diff-17170cd58cc6474d6c6ee0413af4b3b28610aca267bbe7863411627219c26549R232 and pubgrub-rs#249 is ergonomics and zero-to-low overhead construction is a secondary problem, one that i'm tackling only because it would be inconsistent to have a slow API in pubgrub. Currently, we don't have a method for constructing ranges from multiple segments and this makes for an unergonomic API.

## Invariants

For the invariants, we need 1), but i'm not clear if we also need 2) and 3) as strong as they are. They are currently invariants in the version ranges, but we could weaken them. What i like about them is that they imply that any instance of ranges is "fully reduced", it can't be simplified further (without knowning the actual versions available).

1. The segments are sorted, from lowest to highest (through `Ord`).
2. Each segment contains at least one version (start < end).
3. There is at least one version between two segments.

## API

I added two functions: A `from_iter` where the user has to pass valid segments. This wants to be a `try_collect`, but that's still unstable. It is targeted at https://github.com/astral-sh/uv/pull/8797/files#diff-17170cd58cc6474d6c6ee0413af4b3b28610aca267bbe7863411627219c26549R232, which currently does this in a less secure fashion. There, you'd replace `iter_mut().for_each(...)` with an `.into_iter().map(...).collect()` (With the `IntoIter` i've also added this shouldn't even be an allocation; i'm optimizing this too for consistency's sake but i'd be surprised if it mattered to users). The other is `from_unsorted` which is ergonomics and could be used for https://github.com/astral-sh/uv/blob/0335efd0a901ee2351e663d0488b1ce87fd4cc80/crates/pep508-rs/src/marker/algebra.rs#L638-L645 and https://github.com/astral-sh/uv/blob/e1a8beb64b9f6eb573b3af616ffff680c282a316/crates/uv-resolver/src/pubgrub/report.rs#L1098-L1102 from pubgrub-rs#249. The user passes arbitrary segments and we're merging them into valid ranges.

Please discuss :)
konstin added a commit that referenced this issue Nov 8, 2024
Add a method to construct ranges from an iterator of arbitrary segments. This allows to `.collect()` an iterator of tuples of bounds. This is more ergonomic than folding the previous ranges with the next segment each time, and also faster.

Split out from #273
Closes astral-sh#33
Fixes #249
konstin added a commit that referenced this issue Nov 8, 2024
Add a method to construct ranges from an iterator of arbitrary segments. This allows to `.collect()` an iterator of tuples of bounds. This is more ergonomic than folding the previous ranges with the next segment each time, and also faster.

Split out from #273
Closes astral-sh#33
Fixes #249
konstin added a commit that referenced this issue Nov 8, 2024
Add a method to construct ranges from an iterator of arbitrary segments. This allows to `.collect()` an iterator of tuples of bounds. This is more ergonomic than folding the previous ranges with the next segment each time, and also faster.

Split out from #273
Closes astral-sh#33
Fixes #249
konstin added a commit that referenced this issue Nov 8, 2024
Add a method to construct ranges from an iterator of arbitrary segments. This allows to `.collect()` an iterator of tuples of bounds. This is more ergonomic than folding the previous ranges with the next segment each time, and also faster.

Split out from #273
Closes astral-sh#33
Fixes #249
konstin added a commit that referenced this issue Nov 14, 2024
Add a method to construct ranges from an iterator of arbitrary segments. This allows to `.collect()` an iterator of tuples of bounds. This is more ergonomic than folding the previous ranges with the next segment each time, and also faster.

Split out from #273
Closes astral-sh#33
Fixes #249
github-merge-queue bot pushed a commit that referenced this issue Nov 14, 2024
* Add `FromIter` for `Ranges`

Add a method to construct ranges from an iterator of arbitrary segments. This allows to `.collect()` an iterator of tuples of bounds. This is more ergonomic than folding the previous ranges with the next segment each time, and also faster.

Split out from #273
Closes astral-sh#33
Fixes #249

* Fix ascii art alignment

* Fix algorithm with new proptest

* Sorting comment

* Review
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 a pull request may close this issue.

3 participants