Skip to main content

vtcode_core/tools/registry/
optimization_facade.rs

1//! Optimization-related accessors for ToolRegistry.
2
3use std::sync::Arc;
4
5use crate::core::memory_pool::MemoryPool;
6
7use super::ToolRegistry;
8
9impl ToolRegistry {
10    /// Check if optimizations are enabled.
11    pub fn has_optimizations_enabled(&self) -> bool {
12        self.optimization_config
13            .tool_registry
14            .use_optimized_registry
15            || self.optimization_config.memory_pool.enabled
16    }
17
18    /// Borrow the memory pool for optimized allocations.
19    pub fn memory_pool_ref(&self) -> &MemoryPool {
20        self.memory_pool.as_ref()
21    }
22
23    /// Get the shared memory pool handle for callers that need to clone it.
24    pub fn memory_pool(&self) -> &Arc<MemoryPool> {
25        &self.memory_pool
26    }
27
28    /// Clear the hot tool cache (useful for testing or memory management).
29    pub fn clear_hot_cache(&self) {
30        if self
31            .optimization_config
32            .tool_registry
33            .use_optimized_registry
34        {
35            self.hot_tool_cache.write().clear();
36        }
37    }
38
39    /// Get hot cache statistics.
40    pub fn hot_cache_stats(&self) -> (usize, usize) {
41        let cache = self.hot_tool_cache.read();
42        (cache.len(), cache.cap().get())
43    }
44
45    /// Configure performance optimizations for this registry.
46    pub fn configure_optimizations(&mut self, config: vtcode_config::OptimizationConfig) {
47        self.optimization_config = config;
48
49        // Resize hot cache if needed
50        if self
51            .optimization_config
52            .tool_registry
53            .use_optimized_registry
54        {
55            let new_size = self.optimization_config.tool_registry.hot_cache_size;
56            if let Some(new_cache_size) = std::num::NonZeroUsize::new(new_size) {
57                *self.hot_tool_cache.write() = lru::LruCache::new(new_cache_size);
58            }
59        }
60    }
61
62    /// Get the current optimization configuration.
63    pub fn optimization_config(&self) -> &vtcode_config::OptimizationConfig {
64        &self.optimization_config
65    }
66}