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
This commit is contained in:
tidely 2025-05-11 19:14:17 +03:00 committed by GitHub
parent 09ace088ac
commit b34f19a46f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -830,23 +830,20 @@ pub fn load_context(
prompt_store: &Option<Entity<PromptStore>>, prompt_store: &Option<Entity<PromptStore>>,
cx: &mut App, cx: &mut App,
) -> Task<ContextLoadResult> { ) -> Task<ContextLoadResult> {
let mut load_tasks = Vec::new(); let load_tasks: Vec<_> = contexts
.into_iter()
for context in contexts.iter().cloned() { .map(|context| match context {
match context { AgentContextHandle::File(context) => context.load(cx),
AgentContextHandle::File(context) => load_tasks.push(context.load(cx)), AgentContextHandle::Directory(context) => context.load(project.clone(), cx),
AgentContextHandle::Directory(context) => { AgentContextHandle::Symbol(context) => context.load(cx),
load_tasks.push(context.load(project.clone(), cx)) AgentContextHandle::Selection(context) => context.load(cx),
} AgentContextHandle::FetchedUrl(context) => context.load(),
AgentContextHandle::Symbol(context) => load_tasks.push(context.load(cx)), AgentContextHandle::Thread(context) => context.load(cx),
AgentContextHandle::Selection(context) => load_tasks.push(context.load(cx)), AgentContextHandle::TextThread(context) => context.load(cx),
AgentContextHandle::FetchedUrl(context) => load_tasks.push(context.load()), AgentContextHandle::Rules(context) => context.load(prompt_store, cx),
AgentContextHandle::Thread(context) => load_tasks.push(context.load(cx)), AgentContextHandle::Image(context) => context.load(cx),
AgentContextHandle::TextThread(context) => load_tasks.push(context.load(cx)), })
AgentContextHandle::Rules(context) => load_tasks.push(context.load(prompt_store, cx)), .collect();
AgentContextHandle::Image(context) => load_tasks.push(context.load(cx)),
}
}
cx.background_spawn(async move { cx.background_spawn(async move {
let load_results = future::join_all(load_tasks).await; let load_results = future::join_all(load_tasks).await;