zipora 3.1.3

High-performance Rust implementation providing advanced data structures and compression algorithms with memory safety guarantees. Features LRU page cache, sophisticated caching layer, fiber-based concurrency, real-time compression, secure memory pools, SIMD optimizations, and complete C FFI for migration from C++.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! Context-aware memory management for entropy algorithms
//!
//! This module provides efficient buffer pooling and reuse patterns
//! optimized for entropy coding operations.

use crate::error::{Result, ZiporaError};
use std::cell::RefCell;
use std::collections::VecDeque;
use std::sync::{Arc, Mutex, OnceLock};

/// Default capacity for context buffer pool (16MB)
const DEFAULT_CAPACITY: usize = 16 << 20;

/// Maximum number of buffers to cache per context
const DEFAULT_MAX_BUFFERS: usize = 32;

/// Minimum buffer size for reuse eligibility
const MIN_REUSE_SIZE: usize = 1024;

/// Maximum buffer size for reuse eligibility (1MB)
const MAX_REUSE_SIZE: usize = 1 << 20;

/// Configuration for entropy context behavior
#[derive(Debug, Clone)]
pub struct EntropyContextConfig {
    /// Maximum total memory capacity for buffer pool
    pub capacity: usize,
    /// Maximum number of buffers to cache
    pub max_buffers: usize,
    /// Enable thread-local optimization
    pub thread_local: bool,
    /// Enable zero-copy operations where possible
    pub zero_copy: bool,
}

impl Default for EntropyContextConfig {
    fn default() -> Self {
        Self {
            capacity: DEFAULT_CAPACITY,
            max_buffers: DEFAULT_MAX_BUFFERS,
            thread_local: true,
            zero_copy: true,
        }
    }
}

/// A buffer managed by entropy context for automatic reuse
pub struct ContextBuffer {
    data: Vec<u8>,
    context: Option<Arc<Mutex<BufferPool>>>,
}

impl ContextBuffer {
    /// Create a new context buffer with given capacity
    pub fn new(capacity: usize) -> Self {
        Self {
            data: Vec::with_capacity(capacity),
            context: None,
        }
    }
    
    /// Create a context buffer managed by a pool
    fn with_context(mut data: Vec<u8>, context: Arc<Mutex<BufferPool>>) -> Self {
        data.clear();
        Self {
            data,
            context: Some(context),
        }
    }
    
    /// Get mutable access to the underlying buffer
    pub fn as_mut_vec(&mut self) -> &mut Vec<u8> {
        &mut self.data
    }
    
    /// Get immutable access to the underlying buffer
    pub fn as_vec(&self) -> &Vec<u8> {
        &self.data
    }
    
    /// Get the data as a slice
    #[inline]
    pub fn as_slice(&self) -> &[u8] {
        &self.data
    }
    
    /// Get the current length
    #[inline]
    pub fn len(&self) -> usize {
        self.data.len()
    }
    
    /// Check if the buffer is empty
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }
    
    /// Get the current capacity
    #[inline]
    pub fn capacity(&self) -> usize {
        self.data.capacity()
    }
    
    /// Resize the buffer
    pub fn resize(&mut self, new_len: usize, value: u8) {
        self.data.resize(new_len, value);
    }
    
    /// Ensure the buffer has at least the specified capacity
    pub fn ensure_capacity(&mut self, capacity: usize) {
        if self.data.capacity() < capacity {
            self.data.reserve(capacity - self.data.capacity());
        }
    }
    
    /// Clear the buffer contents
    pub fn clear(&mut self) {
        self.data.clear();
    }
    
    /// Take ownership of the underlying Vec
    pub fn into_vec(mut self) -> Vec<u8> {
        self.context = None; // Prevent return to pool
        std::mem::take(&mut self.data)
    }
}

impl Drop for ContextBuffer {
    fn drop(&mut self) {
        if let Some(context) = self.context.take() {
            // Return buffer to pool if eligible for reuse
            let capacity = self.data.capacity();
            if capacity >= MIN_REUSE_SIZE && capacity <= MAX_REUSE_SIZE {
                if let Ok(mut pool) = context.lock() {
                    pool.return_buffer(std::mem::take(&mut self.data));
                }
            }
        }
    }
}

/// Internal buffer pool for managing reusable buffers
struct BufferPool {
    buffers: VecDeque<Vec<u8>>,
    total_capacity: usize,
    max_capacity: usize,
    max_buffers: usize,
}

impl BufferPool {
    fn new(config: &EntropyContextConfig) -> Self {
        Self {
            buffers: VecDeque::new(),
            total_capacity: 0,
            max_capacity: config.capacity,
            max_buffers: config.max_buffers,
        }
    }
    
    fn get_buffer(&mut self, min_capacity: usize) -> Vec<u8> {
        // Find a suitable buffer from the pool
        let mut best_idx = None;
        let mut best_capacity = usize::MAX;
        
        for (idx, buffer) in self.buffers.iter().enumerate() {
            let capacity = buffer.capacity();
            if capacity >= min_capacity && capacity < best_capacity {
                best_capacity = capacity;
                best_idx = Some(idx);
                // Early exit for exact match
                if capacity == min_capacity {
                    break;
                }
            }
        }
        
        if let Some(idx) = best_idx {
            let buffer = self.buffers.remove(idx).expect("buffer index valid from position()");
            self.total_capacity -= buffer.capacity();
            buffer
        } else {
            // Create new buffer
            Vec::with_capacity(min_capacity)
        }
    }
    
    fn return_buffer(&mut self, buffer: Vec<u8>) {
        let capacity = buffer.capacity();
        
        // Only cache if within limits
        if self.buffers.len() < self.max_buffers 
            && self.total_capacity + capacity <= self.max_capacity {
            self.total_capacity += capacity;
            self.buffers.push_back(buffer);
        }
        // Otherwise, let buffer drop naturally
    }
}

/// Thread-local entropy context for optimal performance
struct ThreadLocalContext {
    pool: RefCell<BufferPool>,
    config: EntropyContextConfig,
}

thread_local! {
    static TLS_CONTEXT: OnceLock<ThreadLocalContext> = const { OnceLock::new() };
}

/// Global entropy context for cross-thread sharing
static GLOBAL_CONTEXT: OnceLock<Arc<Mutex<BufferPool>>> = OnceLock::new();
static GLOBAL_CONFIG: OnceLock<EntropyContextConfig> = OnceLock::new();

/// Main entropy context for managing memory efficiently during compression/decompression
pub struct EntropyContext {
    pool: Arc<Mutex<BufferPool>>,
    config: EntropyContextConfig,
}

impl EntropyContext {
    /// Create a new entropy context with default configuration
    pub fn new() -> Self {
        Self::with_config(EntropyContextConfig::default())
    }
    
    /// Create a new entropy context with custom configuration
    pub fn with_config(config: EntropyContextConfig) -> Self {
        let pool = Arc::new(Mutex::new(BufferPool::new(&config)));
        Self { pool, config }
    }
    
    /// Get the global entropy context (thread-safe)
    pub fn global() -> Self {
        let config = GLOBAL_CONFIG.get_or_init(EntropyContextConfig::default);
        let pool = GLOBAL_CONTEXT.get_or_init(|| {
            Arc::new(Mutex::new(BufferPool::new(config)))
        });
        
        Self {
            pool: pool.clone(),
            config: config.clone(),
        }
    }
    
    /// Get thread-local entropy context for optimal single-threaded performance
    pub fn thread_local() -> Self {
        TLS_CONTEXT.with(|ctx| {
            let tls_ctx = ctx.get_or_init(|| {
                let config = GLOBAL_CONFIG.get_or_init(EntropyContextConfig::default).clone();
                ThreadLocalContext {
                    pool: RefCell::new(BufferPool::new(&config)),
                    config: config.clone(),
                }
            });
            
            // For thread-local, we create a temporary context that borrows from TLS
            Self::with_config(tls_ctx.config.clone())
        })
    }
    
    /// Allocate a buffer with at least the specified capacity
    pub fn alloc(&self, capacity: usize) -> Result<ContextBuffer> {
        let buffer = if self.config.thread_local && capacity <= MAX_REUSE_SIZE {
            // Try thread-local allocation first for better performance
            TLS_CONTEXT.with(|ctx| {
                if let Some(tls_ctx) = ctx.get() {
                    if let Ok(mut pool) = tls_ctx.pool.try_borrow_mut() {
                        return pool.get_buffer(capacity);
                    }
                }
                Vec::with_capacity(capacity)
            })
        } else {
            // Use global pool
            match self.pool.lock() {
                Ok(mut pool) => pool.get_buffer(capacity),
                Err(_) => return Err(ZiporaError::resource_exhausted("Context pool lock failed")),
            }
        };
        
        Ok(ContextBuffer::with_context(buffer, self.pool.clone()))
    }
    
    /// Allocate a zero-initialized buffer
    pub fn alloc_zeroed(&self, size: usize) -> Result<ContextBuffer> {
        let mut buffer = self.alloc(size)?;
        buffer.resize(size, 0);
        Ok(buffer)
    }
    
    /// Get configuration
    pub fn config(&self) -> &EntropyContextConfig {
        &self.config
    }
    
    /// Get pool statistics
    pub fn stats(&self) -> Result<ContextStats> {
        match self.pool.lock() {
            Ok(pool) => Ok(ContextStats {
                cached_buffers: pool.buffers.len(),
                total_capacity: pool.total_capacity,
                max_capacity: pool.max_capacity,
                max_buffers: pool.max_buffers,
            }),
            Err(_) => Err(ZiporaError::resource_exhausted("Context pool lock failed")),
        }
    }
    
    /// Get a buffer from the context for temporary use
    pub fn get_buffer(&mut self, size: usize) -> Result<Vec<u8>> {
        let buffer = self.alloc(size)?;
        let mut vec = buffer.into_vec();
        vec.resize(size, 0);
        Ok(vec)
    }
    
    /// Get a temporary buffer from the context
    pub fn get_temp_buffer(&mut self, size: usize) -> Result<Vec<u8>> {
        self.get_buffer(size)
    }
}

impl Default for EntropyContext {
    fn default() -> Self {
        Self::new()
    }
}

/// Statistics about context buffer usage
#[derive(Debug, Clone)]
pub struct ContextStats {
    /// Number of buffers currently cached
    pub cached_buffers: usize,
    /// Total capacity of cached buffers
    pub total_capacity: usize,
    /// Maximum allowed capacity
    pub max_capacity: usize,
    /// Maximum number of buffers
    pub max_buffers: usize,
}

/// Convenience wrapper for entropy operation results with context
pub struct EntropyResult {
    /// The compressed/decompressed data
    pub data: Vec<u8>,
    /// The context buffer (can be reused)
    pub buffer: ContextBuffer,
}

impl EntropyResult {
    /// Create a new entropy result
    pub fn new(data: Vec<u8>, buffer: ContextBuffer) -> Self {
        Self { data, buffer }
    }
    
    /// Take ownership of the data, returning the buffer to the context
    pub fn into_data(self) -> Vec<u8> {
        self.data
    }
    
    /// Get a reference to the data
    #[inline]
    pub fn data(&self) -> &[u8] {
        &self.data
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_context_buffer_basic() {
        let mut buffer = ContextBuffer::new(1024);
        assert_eq!(buffer.capacity(), 1024);
        assert_eq!(buffer.len(), 0);
        assert!(buffer.is_empty());
        
        buffer.resize(100, 42);
        assert_eq!(buffer.len(), 100);
        assert!(!buffer.is_empty());
        assert_eq!(buffer.as_slice()[0], 42);
    }
    
    #[test]
    fn test_entropy_context_allocation() {
        let context = EntropyContext::new();
        
        let buffer1 = context.alloc(1024).unwrap();
        assert!(buffer1.capacity() >= 1024);
        
        let buffer2 = context.alloc_zeroed(512).unwrap();
        assert_eq!(buffer2.len(), 512);
        assert!(buffer2.as_slice().iter().all(|&b| b == 0));
    }
    
    #[test]
    fn test_buffer_reuse() {
        let context = EntropyContext::new();
        
        // Allocate and drop a buffer
        {
            let _buffer = context.alloc(2048).unwrap();
        }
        
        // Stats should show cached buffer
        let stats = context.stats().unwrap();
        println!("Stats after drop: cached_buffers={}, total_capacity={}", 
                stats.cached_buffers, stats.total_capacity);
        
        // Allocate again - should reuse
        let buffer2 = context.alloc(1024).unwrap();
        assert!(buffer2.capacity() >= 1024);
    }
    
    #[test]
    fn test_thread_local_context() {
        let _ctx1 = EntropyContext::thread_local();
        let _ctx2 = EntropyContext::thread_local();
        
        // Should work without panicking
        let buffer = _ctx1.alloc(1024).unwrap();
        assert!(buffer.capacity() >= 1024);
    }
    
    #[test]
    fn test_global_context() {
        let ctx1 = EntropyContext::global();
        let ctx2 = EntropyContext::global();
        
        // Both should work
        let _buffer1 = ctx1.alloc(1024).unwrap();
        let _buffer2 = ctx2.alloc(1024).unwrap();
    }
    
    #[test]
    fn test_entropy_result() {
        let context = EntropyContext::new();
        let buffer = context.alloc(1024).unwrap();
        let data = vec![1, 2, 3, 4, 5];
        
        let result = EntropyResult::new(data.clone(), buffer);
        assert_eq!(result.data(), &data);
        
        let extracted_data = result.into_data();
        assert_eq!(extracted_data, data);
    }
    
    #[test]
    fn test_context_config() {
        let config = EntropyContextConfig {
            capacity: 1024,
            max_buffers: 4,
            thread_local: false,
            zero_copy: true,
        };
        
        let context = EntropyContext::with_config(config.clone());
        assert_eq!(context.config().capacity, 1024);
        assert_eq!(context.config().max_buffers, 4);
        assert!(!context.config().thread_local);
        assert!(context.config().zero_copy);
    }
}