-
Notifications
You must be signed in to change notification settings - Fork 15
Optimise the RangeManager a bit since its now a lot more heavily used #1033
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
Open
rad-cord
wants to merge
5
commits into
master
Choose a base branch
from
rp/optimisation/optimise-range-manager-operations
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+581
−86
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
557bd48
Optimise the RangeManager a bit since its now a lot more heavily used
rad-cord bdcbd80
Relax timing thresholds
rad-cord eb1a08d
Relax timing thresholds
rad-cord feefc4f
Apply suggestions from code review
rad-cord d410ebb
Ruff format
rad-cord File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import bisect | ||
| import sys | ||
| from typing import List, Optional, Tuple | ||
|
|
||
|
|
||
| class IntegerRangeSet: | ||
| def __init__(self) -> None: | ||
| self._ranges: List[Tuple[int, int]] = [] | ||
|
|
||
| def add(self, start: int, end: int) -> None: | ||
| if start > end: | ||
| return | ||
|
|
||
| # 1. Find Merge Window | ||
| i = bisect.bisect_left(self._ranges, (start, end)) | ||
| if i > 0 and self._ranges[i - 1][1] >= start - 1: | ||
| i -= 1 | ||
| start = self._ranges[i][0] | ||
|
|
||
| j = bisect.bisect_right(self._ranges, (end + 1, sys.maxsize)) | ||
| if j > i: | ||
| end = max(end, self._ranges[j - 1][1]) | ||
|
|
||
| # 2. Bulk Replace | ||
| self._ranges[i:j] = [(start, end)] | ||
|
|
||
| def intersection(self, start: int, end: int) -> Optional[List[Tuple[int, int]]]: | ||
| """ | ||
| Returns a list of overlapping tuples or None. | ||
| Complexity: O(log N + K) where K is the number of overlapping fragments. | ||
| """ | ||
| if start > end or not self._ranges: | ||
| return None | ||
|
|
||
| # 1. Define Search Window | ||
| # We only need to check ranges that could possibly overlap. | ||
| # Start Index (lo): The range immediately before where 'start' would fit. | ||
| # End Index (hi): The first range that starts strictly AFTER 'end'. | ||
| lo = bisect.bisect_right(self._ranges, (start, sys.maxsize)) - 1 | ||
| hi = bisect.bisect_right(self._ranges, (end, sys.maxsize)) | ||
|
|
||
| # Ensure lo isn't negative (if start is smaller than the very first range) | ||
| lo = max(0, lo) | ||
|
|
||
| overlaps: List[Tuple[int, int]] = [] | ||
|
|
||
| # 2. Iterate only the relevant slice | ||
| for i in range(lo, hi): | ||
| r_start, r_end = self._ranges[i] | ||
|
|
||
| # Calculate mathematical intersection: [max(starts), min(ends)] | ||
| o_start = max(start, r_start) | ||
| o_end = min(end, r_end) | ||
|
|
||
| # If valid range, it's an overlap | ||
| if o_start <= o_end: | ||
| overlaps.append((o_start, o_end)) | ||
|
|
||
| return overlaps | ||
|
|
||
| def remove(self, start: int, end: int) -> None: | ||
| """Removes a range, splitting or deleting existing ranges.""" | ||
| if start > end: | ||
| return | ||
|
|
||
| # 1. Find Search Window | ||
| # i: The first range that *could* overlap (starts before the removal ends) | ||
| # j: The first range that definitely starts *after* the removal ends | ||
| i = bisect.bisect_right(self._ranges, (start, sys.maxsize)) - 1 | ||
| i = max(0, i) | ||
| j = bisect.bisect_right(self._ranges, (end, sys.maxsize)) | ||
|
|
||
| new_ranges = [] | ||
|
|
||
| # 2. Iterate only the affected slice | ||
| for k in range(i, j): | ||
| r_start, r_end = self._ranges[k] | ||
|
|
||
| # Optimization: If this range is entirely to the left, keep it as is. | ||
| # (This happens because our 'i' search is slightly loose) | ||
| if r_end < start: | ||
| new_ranges.append((r_start, r_end)) | ||
| continue | ||
|
|
||
| # Check for Left Survivor (Range starts before removal starts) | ||
| if r_start < start: | ||
| new_ranges.append((r_start, start - 1)) | ||
|
|
||
| # Check for Right Survivor (Range ends after removal ends) | ||
| if r_end > end: | ||
| new_ranges.append((end + 1, r_end)) | ||
|
|
||
| # 3. Bulk Replace the affected slice with the survivors | ||
| self._ranges[i:j] = new_ranges | ||
|
|
||
| def clear(self) -> None: | ||
| self._ranges = [] | ||
|
|
||
| def __repr__(self) -> str: | ||
| return f"RangeSet({self._ranges})" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if an error should be thrown here instead.
Maybe the code that calls this should be the one that checks it I guess? But curious about your thoughts on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. There are some scenarios where you don't want to throw an error - where it's particularly expensive or the app "must not crash", but neither apply here