Repository: vercel/next.js
File: turbopack/crates/turbo-tasks/src/priority_runner.rs (Rust)

Code before:
        if let Some(heap_item) = queue.pop() {
            if queue.len() * 2 + 16 < queue.capacity() {
                queue.shrink_to_fit();
            }
            drop(queue);
            let tx = heap_item.tx;
            let new_future =
                self.executor
                    .execute(execute_context, heap_item.task, heap_item.priority);

            if !unused_active_count {
                self.active_workers.fetch_add(1, Ordering::Relaxed);
            }
            WorkerFuture::spawn(new_future, tx, execute_context.clone(), self.clone());
            true
        } else {
            false
        }
    }
}

#[derive(Debug)]
enum WorkerState {
    UnfinishedFuture,
    PendingFuture,
    Done,
    Closed,
}

pin_project! {
    struct WorkerFuture<C, T, P, E>
    where
        // pin_project doesn't support bounds with +
        C: Send,
        C: Sync,
        C: 'static,
        T: Send,
        T: 'static,
        P: Ord,
        P: Send,
        P: 'static,
        E: Executor<C, T, P>,
        E: 'static,

    {
        #[pin]
        future: E::Future,
        tx: Option<Sender<()>>,
        execute_context: Arc<C>,
        runner: Arc<PriorityRunner<C, T, P, E>>,
        state: WorkerState,

Review comment (suggestion):
Checks out: https://en.wikipedia.org/wiki/Dynamic_array#Geometric_expansion_and_amortized_cost

> Expanding the array by any constant proportion _a_ ensures that inserting _n_ elements takes [_O_(_n_)](https://en.wikipedia.org/wiki/Big_O_notation "Big O notation") time overall, meaning that each insertion takes [amortized](https://en.wikipedia.org/wiki/Amortized_analysis "Amortized analysis") constant time. Many dynamic arrays also deallocate some of the underlying storage if its size drops below a certain threshold, such as 30% of the capacity. This threshold must be strictly smaller than 1/_a_ in order to provide [hysteresis](https://en.wikipedia.org/wiki/Hysteresis "Hysteresis") (provide a stable band to avoid repeatedly growing and shrinking) and support mixed sequences of insertions and removals with amortized constant cost.

Code after:
            drop(queue);
            let tx = heap_item.tx;
            let new_future =
                self.executor
                    .execute(execute_context, heap_item.task, heap_item.priority);

            if !unused_active_count {
                self.active_workers.fetch_add(1, Ordering::Relaxed);
            }
            WorkerFuture::spawn(new_future, tx, execute_context.clone(), self.clone());
            true
        } else {
            false
        }
    }
}

fn shrink_amortized<P, T>(queue: &mut BinaryHeap<HeapItem<P, T>>) {
    // Amortized shrinking of the queue, but with a lower threshold to avoid
    // frequent reallocations when the queue is small.
    if queue.capacity() > queue.len() * 3 && queue.capacity() > 128 {
        let new_capacity = queue.len().next_power_of_two().max(128);
        queue.shrink_to(new_capacity);
    }
}

#[derive(Debug)]
enum WorkerState {
    UnfinishedFuture,
    PendingFuture,
    Done,
    Closed,
}

pin_project! {
    struct WorkerFuture<C, T, P, E>
    where
        // pin_project doesn't support bounds with +
        C: Send,
        C: Sync,
        C: 'static,
        T: Send,
        T: 'static,
        P: Ord,
        P: Send,
        P: 'static,
        E: Executor<C, T, P>,
        E: 'static,

    {
        #[pin]