perf: eliminate unnecessary heap allocations across agent loop, memory, and channels

- Replace clone()+clear() with std::mem::take() in chunker (items 1, 6)
- Add Vec::with_capacity() hints in chunker split functions (item 2)
- Replace collect::<Vec<_>>().join() with direct iteration in IRC and
  email channels (item 3)
- Share heading strings via Rc<str> instead of cloning per chunk (item 5)
- Use borrowed references in provider tool spec types to avoid cloning
  name/description/parameters per tool per request (item 7)

Closes #712

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Alex Gorevski 2026-02-18 19:02:10 -08:00
parent dce7280812
commit a4b27d2afe
6 changed files with 77 additions and 63 deletions

View file

@ -54,12 +54,12 @@ impl ResponseMessage {
}
#[derive(Debug, Serialize)]
struct NativeChatRequest {
struct NativeChatRequest<'a> {
model: String,
messages: Vec<NativeMessage>,
temperature: f64,
#[serde(skip_serializing_if = "Option::is_none")]
tools: Option<Vec<NativeToolSpec>>,
tools: Option<Vec<NativeToolSpec<'a>>>,
#[serde(skip_serializing_if = "Option::is_none")]
tool_choice: Option<String>,
}
@ -75,18 +75,18 @@ struct NativeMessage {
tool_calls: Option<Vec<NativeToolCall>>,
}
#[derive(Debug, Serialize, Deserialize)]
struct NativeToolSpec {
#[derive(Debug, Serialize)]
struct NativeToolSpec<'a> {
#[serde(rename = "type")]
kind: String,
function: NativeToolFunctionSpec,
kind: &'static str,
function: NativeToolFunctionSpec<'a>,
}
#[derive(Debug, Serialize, Deserialize)]
struct NativeToolFunctionSpec {
name: String,
description: String,
parameters: serde_json::Value,
#[derive(Debug, Serialize)]
struct NativeToolFunctionSpec<'a> {
name: &'a str,
description: &'a str,
parameters: &'a serde_json::Value,
}
#[derive(Debug, Serialize, Deserialize)]
@ -150,16 +150,16 @@ impl OpenAiProvider {
}
}
fn convert_tools(tools: Option<&[ToolSpec]>) -> Option<Vec<NativeToolSpec>> {
fn convert_tools<'a>(tools: Option<&'a [ToolSpec]>) -> Option<Vec<NativeToolSpec<'a>>> {
tools.map(|items| {
items
.iter()
.map(|tool| NativeToolSpec {
kind: "function".to_string(),
kind: "function",
function: NativeToolFunctionSpec {
name: tool.name.clone(),
description: tool.description.clone(),
parameters: tool.parameters.clone(),
name: &tool.name,
description: &tool.description,
parameters: &tool.parameters,
},
})
.collect()