From b34f19a46f3be515d324d5ec21c0f0e19e941794 Mon Sep 17 00:00:00 2001 From: tidely <43219534+tidely@users.noreply.github.com> Date: Sun, 11 May 2025 19:14:17 +0300 Subject: [PATCH] agent: Reduce allocations (#30220) Just a tiny patch to reduce allocations during context loading Calling `.cloned()` on an iterator clones each element one by one, while `into_iter().collect()` pre-allocates the resulting `Vec` Release Notes: - N/A --- crates/agent/src/context.rs | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/crates/agent/src/context.rs b/crates/agent/src/context.rs index b89a6f10c8..f458896b18 100644 --- a/crates/agent/src/context.rs +++ b/crates/agent/src/context.rs @@ -830,23 +830,20 @@ pub fn load_context( prompt_store: &Option>, cx: &mut App, ) -> Task { - let mut load_tasks = Vec::new(); - - for context in contexts.iter().cloned() { - match context { - AgentContextHandle::File(context) => load_tasks.push(context.load(cx)), - AgentContextHandle::Directory(context) => { - load_tasks.push(context.load(project.clone(), cx)) - } - AgentContextHandle::Symbol(context) => load_tasks.push(context.load(cx)), - AgentContextHandle::Selection(context) => load_tasks.push(context.load(cx)), - AgentContextHandle::FetchedUrl(context) => load_tasks.push(context.load()), - AgentContextHandle::Thread(context) => load_tasks.push(context.load(cx)), - AgentContextHandle::TextThread(context) => load_tasks.push(context.load(cx)), - AgentContextHandle::Rules(context) => load_tasks.push(context.load(prompt_store, cx)), - AgentContextHandle::Image(context) => load_tasks.push(context.load(cx)), - } - } + let load_tasks: Vec<_> = contexts + .into_iter() + .map(|context| match context { + AgentContextHandle::File(context) => context.load(cx), + AgentContextHandle::Directory(context) => context.load(project.clone(), cx), + AgentContextHandle::Symbol(context) => context.load(cx), + AgentContextHandle::Selection(context) => context.load(cx), + AgentContextHandle::FetchedUrl(context) => context.load(), + AgentContextHandle::Thread(context) => context.load(cx), + AgentContextHandle::TextThread(context) => context.load(cx), + AgentContextHandle::Rules(context) => context.load(prompt_store, cx), + AgentContextHandle::Image(context) => context.load(cx), + }) + .collect(); cx.background_spawn(async move { let load_results = future::join_all(load_tasks).await;