blz-core 1.5.5

Core library for fast local llms.txt search
Documentation
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#![allow(unsafe_code)] // Core module requires unsafe for performance-critical arena allocations
// Memory pool for efficient buffer reuse and allocation management
use std::collections::VecDeque;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use tokio::sync::Mutex;
use tracing::{debug, warn};

/// Memory pool that manages reusable buffers of different sizes
/// 
/// This pool maintains separate queues for different buffer size classes
/// to minimize allocation overhead and memory fragmentation.
pub struct MemoryPool {
    /// Small buffers (< 1KB)
    small_buffers: Arc<Mutex<VecDeque<Vec<u8>>>>,
    
    /// Medium buffers (1KB - 64KB)
    medium_buffers: Arc<Mutex<VecDeque<Vec<u8>>>>,
    
    /// Large buffers (> 64KB)
    large_buffers: Arc<Mutex<VecDeque<Vec<u8>>>>,
    
    /// String buffers for text operations
    string_buffers: Arc<Mutex<VecDeque<String>>>,
    
    /// Maximum number of buffers per pool
    max_buffers_per_pool: usize,
    
    /// Current memory usage in bytes
    current_usage: AtomicUsize,
    
    /// Maximum memory usage in bytes
    max_usage: usize,
    
    /// Statistics
    stats: Arc<MemoryPoolStats>,
}

/// Thread-safe statistics for memory pool
#[derive(Default)]
pub struct MemoryPoolStats {
    pub allocations: AtomicUsize,
    pub deallocations: AtomicUsize,
    pub cache_hits: AtomicUsize,
    pub cache_misses: AtomicUsize,
    pub peak_usage: AtomicUsize,
}

impl MemoryPool {
    /// Create a new memory pool with specified limits
    pub fn new(max_buffers_per_pool: usize, max_memory_mb: usize) -> Self {
        Self {
            small_buffers: Arc::new(Mutex::new(VecDeque::new())),
            medium_buffers: Arc::new(Mutex::new(VecDeque::new())),
            large_buffers: Arc::new(Mutex::new(VecDeque::new())),
            string_buffers: Arc::new(Mutex::new(VecDeque::new())),
            max_buffers_per_pool,
            current_usage: AtomicUsize::new(0),
            max_usage: max_memory_mb * 1024 * 1024,
            stats: Arc::new(MemoryPoolStats::default()),
        }
    }

    /// Get a byte buffer of at least the specified size
    pub async fn get_buffer(&self, min_size: usize) -> PooledBuffer<'_> {
        self.stats.allocations.fetch_add(1, Ordering::Relaxed);

        let pool = match self.classify_size(min_size) {
            BufferSize::Small => &self.small_buffers,
            BufferSize::Medium => &self.medium_buffers,
            BufferSize::Large => &self.large_buffers,
        };

        let mut buffer = {
            let mut pool_guard = pool.lock().await;
            pool_guard.pop_front()
        };

        if let Some(ref mut buf) = buffer {
            self.stats.cache_hits.fetch_add(1, Ordering::Relaxed);
            
            // Ensure buffer has sufficient capacity
            if buf.capacity() < min_size {
                buf.reserve(min_size - buf.capacity());
            }
            buf.clear();
        } else {
            self.stats.cache_misses.fetch_add(1, Ordering::Relaxed);
            buffer = Some(Vec::with_capacity(min_size.max(self.get_default_capacity(min_size))));
        }

        let buffer = buffer.expect("Buffer should be available");
        let capacity = buffer.capacity();

        // Update memory usage
        let new_usage = self.current_usage.fetch_add(capacity, Ordering::Relaxed) + capacity;
        self.update_peak_usage(new_usage);

        PooledBuffer {
            buffer,
            pool: self,
            size_class: self.classify_size(min_size),
            capacity,
        }
    }

    /// Get a string buffer for text operations
    pub async fn get_string_buffer(&self, min_capacity: usize) -> PooledString<'_> {
        self.stats.allocations.fetch_add(1, Ordering::Relaxed);

        let mut buffer = {
            let mut pool_guard = self.string_buffers.lock().await;
            pool_guard.pop_front()
        };

        if let Some(ref mut buf) = buffer {
            self.stats.cache_hits.fetch_add(1, Ordering::Relaxed);
            
            if buf.capacity() < min_capacity {
                buf.reserve(min_capacity - buf.capacity());
            }
            buf.clear();
        } else {
            self.stats.cache_misses.fetch_add(1, Ordering::Relaxed);
            buffer = Some(String::with_capacity(min_capacity.max(256)));
        }

        let buffer = buffer.expect("String buffer should be available");
        let capacity = buffer.capacity();

        // String capacity is in bytes, but we store chars, so estimate
        let estimated_byte_capacity = capacity;
        let new_usage = self.current_usage.fetch_add(estimated_byte_capacity, Ordering::Relaxed) 
                       + estimated_byte_capacity;
        self.update_peak_usage(new_usage);

        PooledString {
            buffer,
            pool: self,
            capacity: estimated_byte_capacity,
        }
    }

    /// Classify buffer size into appropriate pool
    fn classify_size(&self, size: usize) -> BufferSize {
        if size <= 1024 {
            BufferSize::Small
        } else if size <= 65536 {
            BufferSize::Medium
        } else {
            BufferSize::Large
        }
    }

    /// Get default capacity for size class to reduce reallocations
    fn get_default_capacity(&self, min_size: usize) -> usize {
        match self.classify_size(min_size) {
            BufferSize::Small => 1024,      // 1KB
            BufferSize::Medium => 8192,     // 8KB
            BufferSize::Large => 65536,     // 64KB
        }
    }

    /// Return a buffer to the appropriate pool
    async fn return_buffer(&self, mut buffer: Vec<u8>, size_class: BufferSize) {
        self.stats.deallocations.fetch_add(1, Ordering::Relaxed);

        let pool = match size_class {
            BufferSize::Small => &self.small_buffers,
            BufferSize::Medium => &self.medium_buffers,
            BufferSize::Large => &self.large_buffers,
        };

        let mut pool_guard = pool.lock().await;
        
        // Only return to pool if we have space and buffer isn't too large
        if pool_guard.len() < self.max_buffers_per_pool && buffer.capacity() <= self.get_max_buffer_size() {
            buffer.clear(); // Clear contents but keep capacity
            pool_guard.push_back(buffer);
        }
        // Otherwise, let buffer be dropped (freed)
    }

    /// Return a string buffer to the pool
    async fn return_string_buffer(&self, mut buffer: String) {
        self.stats.deallocations.fetch_add(1, Ordering::Relaxed);

        let mut pool_guard = self.string_buffers.lock().await;
        
        // Only return to pool if we have space and buffer isn't too large
        if pool_guard.len() < self.max_buffers_per_pool && buffer.capacity() <= 1_000_000 {
            buffer.clear();
            pool_guard.push_back(buffer);
        }
    }

    /// Maximum size for a buffer to be returned to pool (to prevent memory waste)
    fn get_max_buffer_size(&self) -> usize {
        1_000_000 // 1MB max
    }

    /// Update peak memory usage
    fn update_peak_usage(&self, current: usize) {
        let mut peak = self.stats.peak_usage.load(Ordering::Relaxed);
        while current > peak {
            match self.stats.peak_usage.compare_exchange_weak(
                peak,
                current,
                Ordering::Relaxed,
                Ordering::Relaxed
            ) {
                Ok(_) => break,
                Err(x) => peak = x,
            }
        }
    }

    /// Get current memory pool statistics
    pub fn get_stats(&self) -> MemoryPoolStatsSummary {
        MemoryPoolStatsSummary {
            allocations: self.stats.allocations.load(Ordering::Relaxed),
            deallocations: self.stats.deallocations.load(Ordering::Relaxed),
            cache_hits: self.stats.cache_hits.load(Ordering::Relaxed),
            cache_misses: self.stats.cache_misses.load(Ordering::Relaxed),
            current_usage_bytes: self.current_usage.load(Ordering::Relaxed),
            peak_usage_bytes: self.stats.peak_usage.load(Ordering::Relaxed),
            hit_rate: {
                let hits = self.stats.cache_hits.load(Ordering::Relaxed);
                let misses = self.stats.cache_misses.load(Ordering::Relaxed);
                if hits + misses > 0 {
                    hits as f64 / (hits + misses) as f64
                } else {
                    0.0
                }
            },
        }
    }

    /// Clear all pools (useful for testing)
    pub async fn clear(&self) {
        let mut small = self.small_buffers.lock().await;
        let mut medium = self.medium_buffers.lock().await;
        let mut large = self.large_buffers.lock().await;
        let mut strings = self.string_buffers.lock().await;

        small.clear();
        medium.clear();
        large.clear();
        strings.clear();

        self.current_usage.store(0, Ordering::Relaxed);
    }

    /// Trim excess buffers to reduce memory usage
    pub async fn trim(&self) {
        let target_count = self.max_buffers_per_pool / 2;

        // Trim each pool to half capacity
        let pools = [&self.small_buffers, &self.medium_buffers, &self.large_buffers];
        
        for pool in &pools {
            let mut pool_guard = pool.lock().await;
            while pool_guard.len() > target_count {
                pool_guard.pop_front();
            }
        }

        // Trim string buffers
        let mut string_pool = self.string_buffers.lock().await;
        while string_pool.len() > target_count {
            string_pool.pop_front();
        }

        debug!("Memory pool trimmed to reduce memory usage");
    }
}

impl Default for MemoryPool {
    fn default() -> Self {
        Self::new(100, 100) // 100 buffers per pool, 100MB max
    }
}

/// Size classification for buffers
#[derive(Debug, Clone, Copy)]
enum BufferSize {
    Small,  // < 1KB
    Medium, // 1KB - 64KB
    Large,  // > 64KB
}

/// RAII wrapper for pooled byte buffer
pub struct PooledBuffer<'a> {
    buffer: Vec<u8>,
    pool: &'a MemoryPool,
    size_class: BufferSize,
    capacity: usize,
}

impl<'a> PooledBuffer<'a> {
    /// Get mutable access to the buffer
    pub fn as_mut(&mut self) -> &mut Vec<u8> {
        &mut self.buffer
    }

    /// Get immutable access to the buffer
    pub fn as_slice(&self) -> &[u8] {
        &self.buffer
    }

    /// Get the buffer's capacity
    pub fn capacity(&self) -> usize {
        self.buffer.capacity()
    }

    /// Consume this wrapper and return the inner buffer
    /// 
    /// Note: This prevents the buffer from being returned to the pool
    pub fn into_inner(mut self) -> Vec<u8> {
        // Update memory usage since buffer won't be returned
        self.pool.current_usage.fetch_sub(self.capacity, Ordering::Relaxed);
        
        // Take buffer to prevent drop from returning it to pool
        std::mem::take(&mut self.buffer)
    }
}

impl Drop for PooledBuffer<'_> {
    fn drop(&mut self) {
        // Return buffer to pool
        let buffer = std::mem::take(&mut self.buffer);
        let pool = self.pool;
        let size_class = self.size_class;
        let capacity = self.capacity;

        // Update memory usage
        pool.current_usage.fetch_sub(capacity, Ordering::Relaxed);

        // Spawn task to return buffer (can't await in Drop)
        tokio::spawn(async move {
            pool.return_buffer(buffer, size_class).await;
        });
    }
}

/// RAII wrapper for pooled string buffer
pub struct PooledString<'a> {
    buffer: String,
    pool: &'a MemoryPool,
    capacity: usize,
}

impl<'a> PooledString<'a> {
    /// Get mutable access to the string
    pub fn as_mut(&mut self) -> &mut String {
        &mut self.buffer
    }

    /// Get string slice
    pub fn as_str(&self) -> &str {
        &self.buffer
    }

    /// Get the string's capacity
    pub fn capacity(&self) -> usize {
        self.buffer.capacity()
    }

    /// Consume wrapper and return inner string
    pub fn into_inner(mut self) -> String {
        // Update memory usage since string won't be returned
        self.pool.current_usage.fetch_sub(self.capacity, Ordering::Relaxed);
        
        std::mem::take(&mut self.buffer)
    }
}

impl Drop for PooledString<'_> {
    fn drop(&mut self) {
        // Return string to pool
        let buffer = std::mem::take(&mut self.buffer);
        let pool = self.pool;
        let capacity = self.capacity;

        // Update memory usage
        pool.current_usage.fetch_sub(capacity, Ordering::Relaxed);

        // Spawn task to return buffer
        tokio::spawn(async move {
            pool.return_string_buffer(buffer).await;
        });
    }
}

/// Summary of memory pool statistics
#[derive(Debug, Clone)]
pub struct MemoryPoolStatsSummary {
    pub allocations: usize,
    pub deallocations: usize,
    pub cache_hits: usize,
    pub cache_misses: usize,
    pub current_usage_bytes: usize,
    pub peak_usage_bytes: usize,
    pub hit_rate: f64,
}

/// Arena allocator for temporary allocations within a scope
/// 
/// This allocator is useful for operations that need many small allocations
/// that all have the same lifetime and can be freed together.
pub struct Arena {
    /// Current buffer being allocated from
    current_buffer: Vec<u8>,
    
    /// Position in current buffer
    position: usize,
    
    /// All buffers allocated by this arena
    buffers: Vec<Vec<u8>>,
    
    /// Default buffer size
    buffer_size: usize,
}

impl Arena {
    /// Create a new arena with specified buffer size
    pub fn new(buffer_size: usize) -> Self {
        Self {
            current_buffer: Vec::with_capacity(buffer_size),
            position: 0,
            buffers: Vec::new(),
            buffer_size,
        }
    }

    /// Allocate space for a value of type T
    pub fn alloc<T>(&mut self, value: T) -> &mut T {
        let layout = std::alloc::Layout::new::<T>();
        let ptr = self.alloc_raw(layout);
        
        // SAFETY: ptr is valid because:
        // 1. alloc_raw returned a properly aligned pointer with enough space for T
        // 2. The memory is within our managed arena buffer
        // 3. We write the value before creating the reference
        // 4. The returned reference has the same lifetime as the arena (&mut self)
        unsafe {
            let typed_ptr = ptr as *mut T;
            std::ptr::write(typed_ptr, value);
            &mut *typed_ptr
        }
    }

    /// Allocate raw bytes with specified alignment
    pub fn alloc_raw(&mut self, layout: std::alloc::Layout) -> *mut u8 {
        let size = layout.size();
        let align = layout.align();

        // Align current position
        let aligned_pos = (self.position + align - 1) & !(align - 1);
        
        // Check if we need a new buffer
        if aligned_pos + size > self.current_buffer.capacity() {
            // Move current buffer to storage and create new one
            let old_buffer = std::mem::replace(
                &mut self.current_buffer, 
                Vec::with_capacity(size.max(self.buffer_size))
            );
            
            if !old_buffer.is_empty() {
                self.buffers.push(old_buffer);
            }
            
            self.position = 0;
            let aligned_pos = 0;
        }

        // Allocate from current buffer
        // SAFETY: aligned_pos is calculated to be within buffer bounds and properly aligned
        let ptr = unsafe {
            self.current_buffer.as_mut_ptr().add(aligned_pos)
        };
        
        self.position = aligned_pos + size;
        
        // Update buffer length if needed
        if self.position > self.current_buffer.len() {
            // SAFETY: self.position is calculated to be within buffer capacity
            // and all memory up to self.position has been allocated/initialized
            unsafe {
                self.current_buffer.set_len(self.position);
            }
        }

        ptr
    }

    /// Get total bytes allocated
    pub fn total_allocated(&self) -> usize {
        self.buffers.iter().map(|b| b.capacity()).sum::<usize>() 
            + self.current_buffer.capacity()
    }

    /// Reset arena, freeing all allocations
    pub fn reset(&mut self) {
        self.buffers.clear();
        self.current_buffer.clear();
        self.position = 0;
    }
}

impl Default for Arena {
    fn default() -> Self {
        Self::new(8192) // 8KB default buffer size
    }
}

impl Drop for Arena {
    fn drop(&mut self) {
        // All allocations are automatically freed when arena is dropped
        self.reset();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio_test;

    #[tokio::test]
    async fn test_memory_pool_basic() {
        let pool = MemoryPool::new(10, 10);
        
        {
            let mut buffer = pool.get_buffer(100).await;
            buffer.as_mut().extend_from_slice(b"test data");
            assert_eq!(buffer.as_slice(), b"test data");
        } // Buffer returned to pool here
        
        let stats = pool.get_stats();
        assert_eq!(stats.allocations, 1);
        assert_eq!(stats.deallocations, 1);
    }

    #[tokio::test]
    async fn test_memory_pool_reuse() {
        let pool = MemoryPool::new(10, 10);
        
        // Allocate and return a buffer
        {
            let _buffer = pool.get_buffer(100).await;
        }
        
        // Allocate again - should reuse
        {
            let _buffer = pool.get_buffer(100).await;
        }
        
        let stats = pool.get_stats();
        assert!(stats.cache_hits > 0);
    }

    #[tokio::test]
    async fn test_string_pool_basic() {
        let pool = MemoryPool::new(10, 10);
        
        {
            let mut str_buf = pool.get_string_buffer(50).await;
            str_buf.as_mut().push_str("test string");
            assert_eq!(str_buf.as_str(), "test string");
        }
        
        let stats = pool.get_stats();
        assert_eq!(stats.allocations, 1);
    }

    #[tokio::test]
    async fn test_memory_pool_size_classes() {
        let pool = MemoryPool::new(10, 10);
        
        // Test different size classes
        let small = pool.get_buffer(500).await;      // Small
        let medium = pool.get_buffer(5000).await;    // Medium
        let large = pool.get_buffer(100000).await;   // Large
        
        assert!(small.capacity() >= 500);
        assert!(medium.capacity() >= 5000);
        assert!(large.capacity() >= 100000);
    }

    #[tokio::test]
    async fn test_memory_pool_trim() {
        let pool = MemoryPool::new(10, 10);
        
        // Allocate several buffers and return them
        for _ in 0..5 {
            let _buffer = pool.get_buffer(100).await;
        }
        
        pool.trim().await;
        
        let stats = pool.get_stats();
        // Should have trimmed some buffers
        assert!(stats.deallocations > 0);
    }

    #[test]
    fn test_arena_basic() {
        let mut arena = Arena::new(1024);
        
        let value = arena.alloc(42i32);
        assert_eq!(*value, 42);
        
        let str_val = arena.alloc("hello");
        assert_eq!(*str_val, "hello");
    }

    #[test]
    fn test_arena_multiple_buffers() {
        let mut arena = Arena::new(64); // Small buffer to force multiple
        
        // Allocate many values to trigger buffer expansion
        for i in 0..100 {
            let value = arena.alloc(i);
            assert_eq!(*value, i);
        }
        
        assert!(arena.total_allocated() > 64);
    }

    #[test]
    fn test_arena_reset() {
        let mut arena = Arena::new(1024);
        
        for i in 0..10 {
            arena.alloc(i);
        }
        
        let allocated_before = arena.total_allocated();
        assert!(allocated_before > 0);
        
        arena.reset();
        assert_eq!(arena.total_allocated(), 1024); // One buffer remains with capacity
    }

    #[tokio::test]
    async fn test_memory_pool_stats() {
        let pool = MemoryPool::new(10, 10);
        
        {
            let _buf1 = pool.get_buffer(100).await;
            let _buf2 = pool.get_buffer(100).await;
        }
        
        {
            let _buf3 = pool.get_buffer(100).await; // Should be cache hit
        }
        
        let stats = pool.get_stats();
        assert!(stats.hit_rate > 0.0);
        assert!(stats.peak_usage_bytes > 0);
    }
}