use crate::memory::{SecureMemoryPool, SecurePoolConfig};
use std::sync::Arc;
fn default_memory_pool() -> Arc<SecureMemoryPool> {
SecureMemoryPool::new(SecurePoolConfig::small_secure())
.or_else(|_| SecureMemoryPool::new(SecurePoolConfig::default()))
.unwrap_or_else(|_| {
let mut emergency_config = SecurePoolConfig::default();
emergency_config.chunk_size = 64; emergency_config.max_chunks = 2; emergency_config.use_guard_pages = false; SecureMemoryPool::new(emergency_config)
.expect("CRITICAL: Emergency pool creation failed - this should never happen")
})
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TrieStrategy {
Patricia {
max_path_length: usize,
compression_threshold: usize,
adaptive_compression: bool,
},
CriticalBit {
cache_critical_bytes: bool,
optimize_for_strings: bool,
bit_level_optimization: bool,
},
DoubleArray {
initial_capacity: usize,
growth_factor: f64,
free_list_management: bool,
auto_shrink: bool,
},
Louds {
nesting_levels: usize,
fragment_compression: bool,
adaptive_backends: bool,
cache_aligned: bool,
},
CompressedSparse {
sparse_threshold: f64,
compression_level: u8,
adaptive_sparse: bool,
},
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TrieStorageStrategy {
Standard {
initial_capacity: usize,
growth_factor: f64,
},
Succinct {
bit_vector_type: BitVectorType,
rank_select_type: RankSelectType,
interleaved_layout: bool,
},
CacheOptimized {
cache_line_size: usize,
numa_aware: bool,
prefetch_enabled: bool,
},
PoolAllocated {
#[cfg_attr(feature = "serde", serde(skip, default = "default_memory_pool"))]
pool: Arc<SecureMemoryPool>,
size_class: usize,
chunk_size: usize,
},
Hybrid {
primary: Box<TrieStorageStrategy>,
secondary: Box<TrieStorageStrategy>,
switch_threshold: usize,
},
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum TrieCompressionStrategy {
None,
PathCompression {
min_path_length: usize,
max_path_length: usize,
adaptive_threshold: bool,
},
FragmentCompression {
fragment_size: usize,
frequency_threshold: f64,
dictionary_size: usize,
},
Hierarchical {
levels: usize,
compression_ratio: f64,
adaptive_levels: bool,
},
Adaptive {
strategies: Vec<TrieCompressionStrategy>,
decision_threshold: usize,
},
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum RankSelectType {
Interleaved256,
MixedIL256,
MixedXL256,
MixedXLBitPacked,
Simple,
Adaptive,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum BitVectorType {
Standard,
RankSelectOptimized,
CacheAligned,
Compressed,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ZiporaTrieConfig {
pub trie_strategy: TrieStrategy,
pub storage_strategy: TrieStorageStrategy,
pub compression_strategy: TrieCompressionStrategy,
pub rank_select_type: RankSelectType,
pub enable_simd: bool,
pub enable_concurrency: bool,
pub cache_optimization: bool,
}
impl Default for ZiporaTrieConfig {
fn default() -> Self {
Self {
trie_strategy: TrieStrategy::DoubleArray {
initial_capacity: 256,
growth_factor: 1.5,
free_list_management: false,
auto_shrink: false,
},
storage_strategy: TrieStorageStrategy::Standard {
initial_capacity: 64,
growth_factor: 2.0,
},
compression_strategy: TrieCompressionStrategy::None,
rank_select_type: RankSelectType::Adaptive,
enable_simd: true,
enable_concurrency: false,
cache_optimization: true,
}
}
}
impl ZiporaTrieConfig {
pub fn max_levels(&self) -> usize {
match &self.trie_strategy {
TrieStrategy::Louds { nesting_levels, .. } => *nesting_levels,
TrieStrategy::Patricia { .. } => 4, TrieStrategy::CriticalBit { .. } => 6, TrieStrategy::DoubleArray { .. } => 5, TrieStrategy::CompressedSparse { .. } => 6, }
}
pub fn cache_optimized() -> Self {
Self {
trie_strategy: TrieStrategy::DoubleArray {
initial_capacity: 512,
growth_factor: 1.5,
free_list_management: false,
auto_shrink: false,
},
storage_strategy: TrieStorageStrategy::CacheOptimized {
cache_line_size: 64,
numa_aware: true,
prefetch_enabled: true,
},
compression_strategy: TrieCompressionStrategy::None,
rank_select_type: RankSelectType::MixedIL256,
enable_simd: true,
enable_concurrency: false,
cache_optimization: true,
}
}
pub fn space_optimized() -> Self {
Self {
trie_strategy: TrieStrategy::Louds {
nesting_levels: 4,
fragment_compression: true,
adaptive_backends: true,
cache_aligned: false,
},
storage_strategy: TrieStorageStrategy::Succinct {
bit_vector_type: BitVectorType::Compressed,
rank_select_type: RankSelectType::MixedXLBitPacked,
interleaved_layout: true,
},
compression_strategy: TrieCompressionStrategy::Hierarchical {
levels: 3,
compression_ratio: 0.7,
adaptive_levels: true,
},
rank_select_type: RankSelectType::MixedXLBitPacked,
enable_simd: true,
enable_concurrency: false,
cache_optimization: false,
}
}
pub fn concurrent_high_performance(pool: Arc<SecureMemoryPool>) -> Self {
Self {
trie_strategy: TrieStrategy::DoubleArray {
initial_capacity: 1, growth_factor: 1.5,
free_list_management: true,
auto_shrink: false,
},
storage_strategy: TrieStorageStrategy::PoolAllocated {
pool,
size_class: 1024,
chunk_size: 4096,
},
compression_strategy: TrieCompressionStrategy::None,
rank_select_type: RankSelectType::Adaptive,
enable_simd: true,
enable_concurrency: true,
cache_optimization: true,
}
}
pub fn sparse_optimized() -> Self {
Self {
trie_strategy: TrieStrategy::CompressedSparse {
sparse_threshold: 0.3,
compression_level: 6,
adaptive_sparse: true,
},
storage_strategy: TrieStorageStrategy::Succinct {
bit_vector_type: BitVectorType::Compressed,
rank_select_type: RankSelectType::Simple,
interleaved_layout: false,
},
compression_strategy: TrieCompressionStrategy::FragmentCompression {
fragment_size: 8,
frequency_threshold: 0.1,
dictionary_size: 4096,
},
rank_select_type: RankSelectType::Simple,
enable_simd: true,
enable_concurrency: false,
cache_optimization: false,
}
}
pub fn string_specialized() -> Self {
Self {
trie_strategy: TrieStrategy::CriticalBit {
cache_critical_bytes: true,
optimize_for_strings: true,
bit_level_optimization: true,
},
storage_strategy: TrieStorageStrategy::CacheOptimized {
cache_line_size: 64,
numa_aware: false,
prefetch_enabled: true,
},
compression_strategy: TrieCompressionStrategy::PathCompression {
min_path_length: 1,
max_path_length: 64,
adaptive_threshold: true,
},
rank_select_type: RankSelectType::Interleaved256,
enable_simd: true,
enable_concurrency: false,
cache_optimization: true,
}
}
}