Skip to content

⚡ Bolt: O(n) hash map lookup for reranking scores#384

Open
bashandbone wants to merge 1 commit into
mainfrom
bolt-reranking-rank-map-60115920967308526
Open

⚡ Bolt: O(n) hash map lookup for reranking scores#384
bashandbone wants to merge 1 commit into
mainfrom
bolt-reranking-rank-map-60115920967308526

Conversation

@bashandbone

@bashandbone bashandbone commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

💡 What: Pre-compute an $O(n)$ hash map (rank_map) of indices to ranks for mapped scores outside the loop, rather than using an $O(n)$ search for every result inside the generator.
🎯 Why: The previous code used next((j + 1 for j, (idx, _) in enumerate(mapped_scores) if idx == i), -1) inside a generator comprehension iterating over results. This resulted in an $O(n^2)$ algorithmic complexity where $n$ is the number of results, causing unnecessary overhead for large result sets.
📊 Impact: Reduces the time complexity of the batch_rank assignment from $O(n^2)$ to $O(n)$ using $O(1)$ dictionary lookups, significantly improving performance when reranking a large number of chunks.
🔬 Measurement: Benchmark default_reranking_output_transformer with large collections of results/chunks to verify the reduction in processing time.


PR created automatically by Jules for task 60115920967308526 started by @bashandbone

Summary by Sourcery

Enhancements:

  • Precompute a rank map from sorted mapped scores and reuse it for batch_rank assignment to reduce algorithmic complexity from O(n^2) to O(n).

…ted generator

Pre-computes a dictionary mapping `idx` to `j + 1` from `mapped_scores` to avoid searching the entire list for every score.

Co-authored-by: bashandbone <89049923+bashandbone@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@sourcery-ai

sourcery-ai Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Precomputes a rank lookup map from result indices to their rank in the sorted scores to reduce batch_rank computation from O(n^2) to O(n) in default_reranking_output_transformer.

File-Level Changes

Change Details Files
Optimize reranking batch_rank assignment by using a precomputed index-to-rank dictionary instead of repeatedly scanning mapped_scores.
  • Keep mapped_scores as a sorted list of (index, score) pairs based on score descending.
  • Introduce rank_map dictionary mapping each result index to its 1-based rank using enumerate over mapped_scores.
  • Replace the inline next(...) generator search over mapped_scores with a constant-time rank_map.get(i, -1) lookup when constructing RerankingResult instances.
  • Add a comment documenting the performance motivation to avoid O(n^2) complexity from nested iteration.
src/codeweaver/providers/reranking/providers/base.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

🤖 Hi @bashandbone, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions

github-actions Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

🤖 I'm sorry @bashandbone, but I was unable to process your request. Please see the logs for more details.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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.

1 participant