vtcode_config/
optimization.rs

1//! Configuration for performance optimization features
2
3use serde::{Deserialize, Serialize};
4
5/// Configuration for all optimization features
6#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
7#[derive(Debug, Clone, Serialize, Deserialize, Default)]
8pub struct OptimizationConfig {
9    /// Memory pool configuration
10    #[serde(default)]
11    pub memory_pool: MemoryPoolConfig,
12
13    /// Tool registry optimization settings
14    #[serde(default)]
15    pub tool_registry: ToolRegistryConfig,
16
17    /// Async pipeline configuration
18    #[serde(default)]
19    pub async_pipeline: AsyncPipelineConfig,
20
21    /// LLM client optimization settings
22    #[serde(default)]
23    pub llm_client: LLMClientConfig,
24
25    /// Agent execution optimization
26    #[serde(default)]
27    pub agent_execution: AgentExecutionConfig,
28
29    /// Performance profiling settings
30    #[serde(default)]
31    pub profiling: ProfilingConfig,
32}
33
34/// Memory pool configuration
35#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct MemoryPoolConfig {
38    /// Enable memory pool (can be disabled for debugging)
39    pub enabled: bool,
40
41    /// Maximum number of strings to pool
42    pub max_string_pool_size: usize,
43
44    /// Maximum number of Values to pool
45    pub max_value_pool_size: usize,
46
47    /// Maximum number of Vec<String> to pool
48    pub max_vec_pool_size: usize,
49}
50
51/// Tool registry optimization configuration
52#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct ToolRegistryConfig {
55    /// Enable optimized registry
56    pub use_optimized_registry: bool,
57
58    /// Maximum concurrent tool executions
59    pub max_concurrent_tools: usize,
60
61    /// Hot cache size for frequently used tools
62    pub hot_cache_size: usize,
63
64    /// Tool execution timeout in seconds
65    pub default_timeout_secs: u64,
66}
67
68/// Async pipeline configuration
69#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
70#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct AsyncPipelineConfig {
72    /// Enable request batching
73    pub enable_batching: bool,
74
75    /// Enable result caching
76    pub enable_caching: bool,
77
78    /// Maximum batch size for tool requests
79    pub max_batch_size: usize,
80
81    /// Batch timeout in milliseconds
82    pub batch_timeout_ms: u64,
83
84    /// Result cache size
85    pub cache_size: usize,
86}
87
88/// LLM client optimization configuration
89#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
90#[derive(Debug, Clone, Serialize, Deserialize)]
91pub struct LLMClientConfig {
92    /// Enable connection pooling
93    pub enable_connection_pooling: bool,
94
95    /// Enable response caching
96    pub enable_response_caching: bool,
97
98    /// Enable request batching
99    pub enable_request_batching: bool,
100
101    /// Connection pool size
102    pub connection_pool_size: usize,
103
104    /// Response cache size
105    pub response_cache_size: usize,
106
107    /// Response cache TTL in seconds
108    pub cache_ttl_secs: u64,
109
110    /// Rate limit: requests per second
111    pub rate_limit_rps: f64,
112
113    /// Rate limit burst capacity
114    pub rate_limit_burst: usize,
115}
116
117/// Agent execution optimization configuration
118#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
119#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct AgentExecutionConfig {
121    /// Enable optimized agent execution loop
122    pub use_optimized_loop: bool,
123
124    /// Enable performance prediction
125    pub enable_performance_prediction: bool,
126
127    /// State transition history size
128    pub state_history_size: usize,
129
130    /// Resource monitoring interval in milliseconds
131    pub resource_monitor_interval_ms: u64,
132
133    /// Maximum memory usage in MB
134    pub max_memory_mb: u64,
135
136    /// Maximum execution time in seconds
137    pub max_execution_time_secs: u64,
138
139    /// Idle detection timeout in milliseconds (0 to disable)
140    /// When the agent is idle for this duration, it will enter a low-power state
141    pub idle_timeout_ms: u64,
142
143    /// Back-off duration in milliseconds when no work is pending
144    /// This reduces CPU usage during idle periods
145    pub idle_backoff_ms: u64,
146
147    /// Maximum consecutive idle cycles before entering deep sleep
148    pub max_idle_cycles: usize,
149}
150
151/// Performance profiling configuration
152#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
153#[derive(Debug, Clone, Serialize, Deserialize)]
154pub struct ProfilingConfig {
155    /// Enable performance profiling
156    pub enabled: bool,
157
158    /// Resource monitoring interval in milliseconds
159    pub monitor_interval_ms: u64,
160
161    /// Maximum benchmark history size
162    pub max_history_size: usize,
163
164    /// Auto-export results to file
165    pub auto_export_results: bool,
166
167    /// Export file path
168    pub export_file_path: String,
169
170    /// Enable regression testing
171    pub enable_regression_testing: bool,
172
173    /// Maximum allowed performance regression percentage
174    pub max_regression_percent: f64,
175}
176
177impl Default for MemoryPoolConfig {
178    fn default() -> Self {
179        Self {
180            enabled: true,
181            max_string_pool_size: 64,
182            max_value_pool_size: 32,
183            max_vec_pool_size: 16,
184        }
185    }
186}
187
188impl Default for ToolRegistryConfig {
189    fn default() -> Self {
190        Self {
191            use_optimized_registry: true, // Enable by default for better performance
192            max_concurrent_tools: 4,
193            hot_cache_size: 16,
194            default_timeout_secs: 180,
195        }
196    }
197}
198
199impl Default for AsyncPipelineConfig {
200    fn default() -> Self {
201        Self {
202            enable_batching: false, // Conservative default
203            enable_caching: true,
204            max_batch_size: 5,
205            batch_timeout_ms: 100,
206            cache_size: 100,
207        }
208    }
209}
210
211impl Default for LLMClientConfig {
212    fn default() -> Self {
213        Self {
214            enable_connection_pooling: false, // Conservative default
215            enable_response_caching: true,
216            enable_request_batching: false, // Conservative default
217            connection_pool_size: 4,
218            response_cache_size: 50,
219            cache_ttl_secs: 300,
220            rate_limit_rps: 10.0,
221            rate_limit_burst: 20,
222        }
223    }
224}
225
226impl Default for AgentExecutionConfig {
227    fn default() -> Self {
228        Self {
229            use_optimized_loop: true, // Enable by default for better performance
230            enable_performance_prediction: false, // Conservative default
231            state_history_size: 1000,
232            resource_monitor_interval_ms: 100,
233            max_memory_mb: 1024,
234            max_execution_time_secs: 300,
235            idle_timeout_ms: 5000, // 5 seconds idle timeout
236            idle_backoff_ms: 100,  // 100ms backoff during idle
237            max_idle_cycles: 10,   // 10 consecutive idle cycles before deep sleep
238        }
239    }
240}
241
242impl Default for ProfilingConfig {
243    fn default() -> Self {
244        Self {
245            enabled: false, // Disabled by default to avoid overhead
246            monitor_interval_ms: 100,
247            max_history_size: 1000,
248            auto_export_results: false,
249            export_file_path: "benchmark_results.json".to_string(),
250            enable_regression_testing: false,
251            max_regression_percent: 10.0,
252        }
253    }
254}
255
256impl OptimizationConfig {
257    /// Get optimized configuration for development
258    pub fn development() -> Self {
259        Self {
260            memory_pool: MemoryPoolConfig {
261                enabled: true,
262                ..Default::default()
263            },
264            tool_registry: ToolRegistryConfig {
265                use_optimized_registry: true,
266                max_concurrent_tools: 2,
267                ..Default::default()
268            },
269            async_pipeline: AsyncPipelineConfig {
270                enable_batching: true,
271                enable_caching: true,
272                max_batch_size: 3,
273                ..Default::default()
274            },
275            llm_client: LLMClientConfig {
276                enable_connection_pooling: true,
277                enable_response_caching: true,
278                connection_pool_size: 2,
279                rate_limit_rps: 5.0,
280                ..Default::default()
281            },
282            agent_execution: AgentExecutionConfig {
283                use_optimized_loop: true,
284                enable_performance_prediction: false, // Disabled for dev
285                max_memory_mb: 512,
286                idle_timeout_ms: 2000, // Shorter idle timeout for development
287                idle_backoff_ms: 50,   // Shorter backoff for development
288                max_idle_cycles: 5,    // Fewer idle cycles for development
289                ..Default::default()
290            },
291            profiling: ProfilingConfig {
292                enabled: true, // Enabled for development
293                auto_export_results: true,
294                ..Default::default()
295            },
296        }
297    }
298
299    /// Get optimized configuration for production
300    pub fn production() -> Self {
301        Self {
302            memory_pool: MemoryPoolConfig {
303                enabled: true,
304                max_string_pool_size: 128,
305                max_value_pool_size: 64,
306                max_vec_pool_size: 32,
307            },
308            tool_registry: ToolRegistryConfig {
309                use_optimized_registry: true,
310                max_concurrent_tools: 8,
311                hot_cache_size: 32,
312                default_timeout_secs: 300,
313            },
314            async_pipeline: AsyncPipelineConfig {
315                enable_batching: true,
316                enable_caching: true,
317                max_batch_size: 10,
318                batch_timeout_ms: 50,
319                cache_size: 200,
320            },
321            llm_client: LLMClientConfig {
322                enable_connection_pooling: true,
323                enable_response_caching: true,
324                enable_request_batching: true,
325                connection_pool_size: 8,
326                response_cache_size: 100,
327                cache_ttl_secs: 600,
328                rate_limit_rps: 20.0,
329                rate_limit_burst: 50,
330            },
331            agent_execution: AgentExecutionConfig {
332                use_optimized_loop: true,
333                enable_performance_prediction: true,
334                state_history_size: 2000,
335                resource_monitor_interval_ms: 50,
336                max_memory_mb: 2048,
337                max_execution_time_secs: 600,
338                idle_timeout_ms: 10000, // Longer idle timeout for production
339                idle_backoff_ms: 200,   // Longer backoff for production
340                max_idle_cycles: 20,    // More idle cycles for production
341            },
342            profiling: ProfilingConfig {
343                enabled: false, // Disabled in production unless needed
344                monitor_interval_ms: 1000,
345                max_history_size: 500,
346                auto_export_results: false,
347                export_file_path: "/var/log/vtcode/benchmark_results.json".to_string(),
348                enable_regression_testing: true,
349                max_regression_percent: 5.0,
350            },
351        }
352    }
353}