Repository: meilisearch/meilisearch
File: crates/milli/src/search/new/bucket_sort.rs (Rust)

Code before:
    let mut valid_docids = vec![];
    let mut valid_scores = vec![];
    let mut cur_offset = 0usize;

    macro_rules! maybe_add_to_results {
        ($candidates:expr) => {
            maybe_add_to_results(
                ctx,
                from,
                length,
                logger,
                &mut valid_docids,
                &mut valid_scores,
                &mut all_candidates,
                &mut ranking_rule_universes,
                &mut ranking_rules,
                cur_ranking_rule_index,
                &mut cur_offset,
                distinct_fid,
                &ranking_rule_scores,
                $candidates,
            )?;
        };
    }

    let max_total_hits = max_total_hits.unwrap_or(usize::MAX);
    while valid_docids.len() < length
        || (exhaustive_number_hits
            && ranking_score_threshold.is_some()
            && valid_docids.len() < max_total_hits)
    {
        if time_budget.exceeded() {
            loop {
                let bucket = std::mem::take(&mut ranking_rule_universes[cur_ranking_rule_index]);
                ranking_rule_scores.push(ScoreDetails::Skipped);

                // remove candidates from the universe without adding them to result if their score is below the threshold
                let is_below_threshold =
                    ranking_score_threshold.is_some_and(|ranking_score_threshold| {
                        let current_score = ScoreDetails::global_score(ranking_rule_scores.iter());
                        current_score < ranking_score_threshold
                    });

                if is_below_threshold {
                    all_candidates -= &bucket;
                    all_candidates -= &ranking_rule_universes[cur_ranking_rule_index];
                } else {
                    maybe_add_to_results!(bucket);
                }

                ranking_rule_scores.pop();

Review comment (suggestion):
to avoid accidental full scans of the DB should `max_total_hits` somehow not be set, I advise we only take into account `exhaustive_number_hits` when `max_total_hits.is_some()`.

A nice way to express that could be the following:

```rust
let max_len_to_evaluate = match (max_total_hits, exhaustive_number_hits && ranking_score_threshold.is_some()) {
    (Some(max_total_hits), true) => max_total_hits,
    _ => length
};

while valid_docids.len() < max_len_to_evaluate {
// ...
}

Code after:
    let mut valid_docids = vec![];
    let mut valid_scores = vec![];
    let mut cur_offset = 0usize;

    macro_rules! maybe_add_to_results {
        ($candidates:expr) => {
            maybe_add_to_results(
                ctx,
                from,
                length,
                logger,
                &mut valid_docids,
                &mut valid_scores,
                &mut all_candidates,
                &mut ranking_rule_universes,
                &mut ranking_rules,
                cur_ranking_rule_index,
                &mut cur_offset,
                distinct_fid,
                &ranking_rule_scores,
                $candidates,
            )?;
        };
    }

    let max_len_to_evaluate =
        match (max_total_hits, exhaustive_number_hits && ranking_score_threshold.is_some()) {
            (Some(max_total_hits), true) => max_total_hits,
            _ => length,
        };

    while valid_docids.len() < max_len_to_evaluate {
        if time_budget.exceeded() {
            loop {
                let bucket = std::mem::take(&mut ranking_rule_universes[cur_ranking_rule_index]);
                ranking_rule_scores.push(ScoreDetails::Skipped);

                // remove candidates from the universe without adding them to result if their score is below the threshold
                let is_below_threshold =
                    ranking_score_threshold.is_some_and(|ranking_score_threshold| {
                        let current_score = ScoreDetails::global_score(ranking_rule_scores.iter());
                        current_score < ranking_score_threshold
                    });

                if is_below_threshold {
                    all_candidates -= &bucket;
                    all_candidates -= &ranking_rule_universes[cur_ranking_rule_index];
                } else {
                    maybe_add_to_results!(bucket);
                }