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.tool_registry.use_optimized_registry || self.optimization_config.memory_pool.enabled
13    }
14
15    /// Borrow the memory pool for optimized allocations.
16    pub fn memory_pool_ref(&self) -> &MemoryPool {
17        self.memory_pool.as_ref()
18    }
19
20    /// Get the shared memory pool handle for callers that need to clone it.
21    pub fn memory_pool(&self) -> &Arc<MemoryPool> {
22        &self.memory_pool
23    }
24
25    /// Clear the hot tool cache (useful for testing or memory management).
26    pub fn clear_hot_cache(&self) {
27        if self.optimization_config.tool_registry.use_optimized_registry {
28            self.hot_tool_cache.write().clear();
29        }
30    }
31
32    /// Get hot cache statistics.
33    pub fn hot_cache_stats(&self) -> (usize, usize) {
34        let cache = self.hot_tool_cache.read();
35        (cache.len(), cache.cap().get())
36    }
37
38    /// Configure performance optimizations for this registry.
39    pub fn configure_optimizations(&mut self, config: vtcode_config::OptimizationConfig) {
40        self.optimization_config = config;
41
42        // Resize hot cache if needed
43        if self.optimization_config.tool_registry.use_optimized_registry {
44            let new_size = self.optimization_config.tool_registry.hot_cache_size;
45            if let Some(new_cache_size) = std::num::NonZeroUsize::new(new_size) {
46                *self.hot_tool_cache.write() = lru::LruCache::new(new_cache_size);
47            }
48        }
49    }
50
51    /// Get the current optimization configuration.
52    pub fn optimization_config(&self) -> &vtcode_config::OptimizationConfig {
53        &self.optimization_config
54    }
55}