Skip to content

Conversation

@rad-cord
Copy link
Collaborator

@rad-cord rad-cord commented Nov 26, 2025

Introduction and Explanation

Replace the RangeManager implementation, with one backed by IntegerRangeSet which is a bit smarter about how it keeps tracks of ranges. Results in >100x speedup in situations where we deal with a lot of ranges

Tests

  • expand the coverage of functionality tests
  • added performance regression tests for the new RangeManager

Future work

The RangeManager is now a wrapper around an external class, and in python even the overhead of a function call is noticeable

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @rad-cord, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on a critical performance enhancement for range management within the system. It introduces a new, highly efficient IntegerRangeSet data structure and re-architects the RangeManager to leverage this new component. This results in substantial speed improvements for operations involving numerous ranges, addresses potential bottlenecks, and includes memory optimizations for Range objects. The changes are thoroughly validated with expanded unit and performance tests.

Highlights

  • Introduction of IntegerRangeSet: A new, highly optimized data structure for managing integer ranges is added, leveraging Python's bisect module for efficient operations like adding, removing, and finding intersections of ranges.
  • RangeManager Re-architecture: The existing RangeManager class is refactored to utilize the new IntegerRangeSet as its backend, leading to significant performance gains, especially for operations involving many ranges.
  • Performance Boost: The changes are expected to deliver a "greater than 100x speedup" in scenarios with heavy range usage, as stated in the PR description, by replacing less efficient list-based operations with bisect-optimized methods.
  • Memory Optimization for Range objects: The Range class now uses __slots__ to reduce memory footprint, which is beneficial for applications dealing with numerous range instances by preventing the creation of instance dictionaries.
  • Enhanced Test Coverage: Comprehensive unit tests have been added and updated for RangeManager, alongside a new dedicated suite of performance regression tests to ensure the optimizations are maintained and to prevent future performance degradations.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions
Copy link

github-actions bot commented Nov 26, 2025

Unit test report (Python 3.9.24, Pydantic 1.10.22)

307 tests   307 ✅  11s ⏱️
  1 suites    0 💤
  1 files      0 ❌

Results for commit d410ebb.

♻️ This comment has been updated with latest results.

@github-actions
Copy link

github-actions bot commented Nov 26, 2025

Unit test report (Python 3.9.24, Pydantic 2.12.3)

307 tests   307 ✅  11s ⏱️
  1 suites    0 💤
  1 files      0 ❌

Results for commit d410ebb.

♻️ This comment has been updated with latest results.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a significant performance optimization for RangeManager by replacing its list-based implementation with a new IntegerRangeSet class that uses bisect for more efficient range management. The changes are well-structured and include comprehensive unit and performance tests. My review focuses on further performance improvements, API design, and test clarity. Overall, this is a great improvement.

rad-cord and others added 2 commits November 26, 2025 19:12
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@github-actions
Copy link

github-actions bot commented Nov 26, 2025

SDK integration test report

285 tests  ±0   275 ✅  - 2   13m 0s ⏱️ - 5m 12s
  1 suites ±0     4 💤 ±0 
  1 files   ±0     6 ❌ +2 

For more details on these failures, see this check.

Results for commit d410ebb. ± Comparison against base commit dbabc87.

♻️ This comment has been updated with latest results.

Comment on lines +1 to +19
"""Performance regression tests for RangeManager."""

import time

import pytest

from encord.common.range_manager import RangeManager
from encord.objects.frames import Range

NUM_RANGES = 100_000

# Performance thresholds (in seconds)
# These are based on current performance and should alert if there's regression
ADD_RANGES_THRESHOLD = 1
ADD_RANGE_SEQUENTIAL_THRESHOLD = 1
INIT_FRAMES_THRESHOLD = 1
GET_RANGES_THRESHOLD = 1
INTERSECTION_THRESHOLD = 1
REMOVE_RANGE_THRESHOLD = 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crazy! Performance tests in the SDK? Love it.

Comment on lines +11 to +12
if start > end:
return
Copy link
Contributor

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.

Copy link
Collaborator Author

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

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 this pull request may close these issues.

3 participants