Repository: meilisearch/meilisearch
File: crates/milli/src/update/new/indexer/post_processing/facet_bulk.rs (Rust)

Code before:

    clear_levels(db, wtxn, field_id)?;

    let mut base_level = 0;
    // That's a do-while loop
    while {
        let mut level_size = 0;
        for reader in compute_level(index, wtxn, db, field_id, base_level)? {
            let mut cursor = reader.into_cursor()?;
            while let Some((left_bound, facet_group_value)) = cursor.move_on_next()? {
                level_size += 1;
                let level = base_level.checked_add(1).unwrap();
                let key = FacetGroupKey { field_id, level, left_bound };
                debug_assert!(
                    db.get(wtxn, &key).transpose().is_none(),
                    "entry must not be there and must have already been deleted: {key:?}"
                );
                db.remap_data_type::<Bytes>().put(wtxn, &key, facet_group_value)?;
            }
        }

        base_level += 1;

        // If the next level will have the minimum required groups, continue.
        (level_size / FACET_GROUP_SIZE as usize) >= FACET_MIN_LEVEL_SIZE as usize
    } {}

    Ok(())
}

/// Compute the groups of facets from the provided base level
/// and write the content into different grenad files.
fn compute_level(
    index: &Index,
    wtxn: &heed::RwTxn,
    db: Database<FacetGroupKeyCodec<BytesRefCodec>, LazyDecode<FacetGroupValueCodec>>,
    field_id: FieldId,
    base_level: u8,
) -> Result<Vec<grenad::Reader<BufReader<File>>>, crate::Error> {
    let thread_count = rayon::current_num_threads();
    let rtxns = iter::repeat_with(|| index.env.nested_read_txn(wtxn))
        .take(thread_count)
        .collect::<heed::Result<Vec<_>>>()?;

    let range = {
        // Based on the first possible value for the base level up to
        // the first possible value for the next level *excluded*.
        let left = FacetGroupKey::<&[u8]> { field_id, level: base_level, left_bound: &[] };
        let right = FacetGroupKey::<&[u8]> {
            field_id,
            level: base_level.checked_add(1).unwrap(),

Review comment (bug):
I think it's the same 🤔 
I moved up the `level = base_level.checked_add(1).unwrap();` and assigned `base_level = level` after the loops.
Let me know if I am wrong:
```suggestion
    while {
        let mut level_size = 0;
        let level = base_level.checked_add(1).unwrap();
        for reader in compute_level(index, wtxn, db, field_id, base_level)? {
            let mut cursor = reader.into_cursor()?;
            while let Some((left_bound, facet_group_value)) = cursor.move_on_next()? {
                level_size += 1;
                let key = FacetGroupKey { field_id, level, left_bound };
                debug_assert!(
                    db.get(wtxn, &key).transpose().is_none(),
                    "entry must not be there and must have already been deleted: {key:?}"
                );
                db.remap_data_type::<Bytes>().put(wtxn, &key, facet_group_value)?;
            }
        }

        base_level = level;

        // If the next level will have the minimum required groups, continue.
        (level_size / FACET_GROUP_SIZE as usize) >= FACET_MIN_LEVEL_SIZE as usize
    } {}
```

Code after:

    clear_levels(db, wtxn, field_id)?;

    let mut base_level: u8 = 0;
    // That's a do-while loop
    while {
        let mut level_size = 0;
        let level = base_level.checked_add(1).unwrap();
        for reader in compute_level(index, wtxn, db, field_id, base_level)? {
            let mut cursor = reader.into_cursor()?;
            while let Some((left_bound, facet_group_value)) = cursor.move_on_next()? {
                level_size += 1;
                let key = FacetGroupKey { field_id, level, left_bound };
                debug_assert!(
                    db.get(wtxn, &key).transpose().is_none(),
                    "entry must not be there and must have already been deleted: {key:?}"
                );
                db.remap_data_type::<Bytes>().put(wtxn, &key, facet_group_value)?;
            }
        }

        base_level = level;

        // If the next level will have the minimum required groups, continue.
        (level_size / FACET_GROUP_SIZE as usize) >= FACET_MIN_LEVEL_SIZE as usize
    } {}

    Ok(())
}

/// Compute the groups of facets from the provided base level
/// and write the content into different grenad files.
fn compute_level(
    index: &Index,
    wtxn: &heed::RwTxn,
    db: Database<FacetGroupKeyCodec<BytesRefCodec>, LazyDecode<FacetGroupValueCodec>>,
    field_id: FieldId,
    base_level: u8,
) -> Result<Vec<grenad::Reader<BufReader<File>>>, crate::Error> {
    let thread_count = rayon::current_num_threads();
    let rtxns = iter::repeat_with(|| index.env.nested_read_txn(wtxn))
        .take(thread_count)
        .collect::<heed::Result<Vec<_>>>()?;

    let range = {
        // Based on the first possible value for the base level up to
        // the first possible value for the next level *excluded*.
        let left = FacetGroupKey::<&[u8]> { field_id, level: base_level, left_bound: &[] };
        let right = FacetGroupKey::<&[u8]> {
            field_id,
            level: base_level.checked_add(1).unwrap(),