Repository: qdrant/qdrant
File: lib/collection/src/update_workers/optimization_worker.rs (Rust)

Code before:
        handles.append(&mut new_handles);
    }

    /// Checks conditions for all optimizers until there is no suggested segment
    /// Starts a task for each optimization
    /// Returns handles for started tasks
    pub(crate) fn launch_optimization<F>(
        optimizers: Arc<Vec<Arc<Optimizer>>>,
        optimizers_log: Arc<Mutex<TrackerLog>>,
        total_optimized_points: Arc<AtomicUsize>,
        optimizer_resource_budget: &ResourceBudget,
        segments: LockedSegmentHolder,
        callback: F,
        limit: Option<usize>,
    ) -> Vec<StoppableTaskHandle<bool>>
    where
        F: Fn() + Send + Clone + Sync + 'static,
    {
        let mut handles = vec![];
        let is_optimization_failed = Arc::new(AtomicBool::new(false));

        let scheduled;
        let mut pending_segments = 0;
        let mut pending_points = 0;
        {
            let segments = segments.read();
            let mut planner = OptimizationPlanner::new(
                segments.running_optimizations.count(),
                segments.iter_original(),
            );
            for optimizer in optimizers.iter() {
                planner.set_optimizer(Arc::clone(optimizer));
                optimizer.plan_optimizations(&mut planner);
            }
            scheduled = planner.into_scheduled();

            for (_, segment_ids) in &scheduled {
                pending_segments += segment_ids.len();
                for &segment_id in segment_ids {
                    if let Some(LockedSegment::Original(segment)) = segments.get(segment_id) {
                        pending_points += segment.read().total_point_count()
                    }
                }
            }
        }

        optimizers_log.lock().pending = PendingOptimizations {
            optimizations: scheduled.len(),
            segments: pending_segments,
            points: pending_points,
        };

Review comment (suggestion):
Nit. WDYT about returning `scheduled` from the scope?
```
let scheduled = {
    ...
}
```

Code after:
        let mut handles = optimization_handles.lock().await;
        handles.append(&mut new_handles);
    }

    /// Checks conditions for all optimizers until there is no suggested segment
    /// Starts a task for each optimization
    /// Returns handles for started tasks
    pub(crate) fn launch_optimization<F>(
        optimizers: Arc<Vec<Arc<Optimizer>>>,
        optimizers_log: Arc<Mutex<TrackerLog>>,
        total_optimized_points: Arc<AtomicUsize>,
        optimizer_resource_budget: &ResourceBudget,
        segments: LockedSegmentHolder,
        callback: F,
        limit: Option<usize>,
    ) -> Vec<StoppableTaskHandle<bool>>
    where
        F: Fn() + Send + Clone + Sync + 'static,
    {
        let mut handles = vec![];
        let is_optimization_failed = Arc::new(AtomicBool::new(false));

        let scheduled;
        let mut pending_segments = 0;
        let mut pending_points = 0;
        {
            let segments = segments.read();
            let mut planner = OptimizationPlanner::new(
                segments.running_optimizations.count(),
                segments.iter_original(),
            );
            for optimizer in optimizers.iter() {
                planner.set_optimizer(Arc::clone(optimizer));
                optimizer.plan_optimizations(&mut planner);
            }
            scheduled = planner.into_scheduled();

            for (_, segment_ids) in &scheduled {
                pending_segments += segment_ids.len();
                for &segment_id in segment_ids {
                    if let Some(LockedSegment::Original(segment)) = segments.get(segment_id) {
                        pending_points += segment.read().available_point_count();
                    }
                }
            }
        }

        optimizers_log.lock().pending = PendingOptimizations {
            optimizations: scheduled.len(),
            segments: pending_segments,
            points: pending_points,