Performance Tuning
Optimize MoFA applications for maximum performance.
Build Optimization
Release Profile
# Cargo.toml
[profile.release]
opt-level = 3
lto = true
codegen-units = 1
strip = true
Feature Flags
Only enable features you need:
[dependencies]
# Minimal: smaller binary, faster compile
mofa-sdk = { version = "0.1", default-features = false, features = ["openai"] }
# Avoid unused features
# mofa-sdk = { version = "0.1", features = ["full"] } # Don't do this
Concurrency
Agent Concurrency
#![allow(unused)]
fn main() {
// Limit concurrent executions
let capabilities = AgentCapabilities::builder()
.max_concurrency(100)
.build();
}
Database Connections
#![allow(unused)]
fn main() {
// Tune connection pool
let pool = sqlx::postgres::PgPoolOptions::new()
.max_connections(20)
.min_connections(5)
.connect(&database_url)
.await?;
}
Tokio Runtime
// Configure runtime
#[tokio::main(flavor = "multi_thread", worker_threads = 8)]
async fn main() {
// ...
}
Memory Management
Session Caching
#![allow(unused)]
fn main() {
// Limit session cache size
let config = PersistenceConfig {
session_cache_size: 1000,
session_ttl: Duration::from_secs(3600),
};
}
Context Window
#![allow(unused)]
fn main() {
// Use sliding window for long conversations
let agent = LLMAgentBuilder::from_env()?
.with_sliding_window(20) // Keep last 20 messages
.build_async()
.await;
}
LLM Optimization
Batching
#![allow(unused)]
fn main() {
// Batch multiple requests
let results = run_agents(agent, inputs).await?;
}
Caching
#![allow(unused)]
fn main() {
// Enable response caching
let client = LLMClient::builder()
.with_cache(CacheConfig {
enabled: true,
ttl: Duration::from_secs(300),
max_entries: 1000,
})
.build();
}
Streaming
#![allow(unused)]
fn main() {
// Use streaming for better UX
let stream = client.stream()
.system("You are helpful.")
.user("Tell a story")
.start()
.await?;
while let Some(chunk) = stream.next().await {
print!("{}", chunk?);
}
}
Profiling
CPU Profiling
# Using perf
cargo build --release
perf record -g ./target/release/my-agent
perf report
Memory Profiling
# Using valgrind
valgrind --tool=massif ./target/release/my-agent
Flamegraphs
cargo install flamegraph
cargo flamegraph --root
Benchmarks
# Run built-in benchmarks
cargo bench
# Benchmark specific operations
cargo bench -- agent_execution
See Also
- Production Deployment — Deployment guide
- Configuration — Runtime configuration