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
//! Memory optimization module for Maproom.
//!
//! This module provides memory-efficient data structures and techniques to minimize
//! memory usage while maintaining performance:
//!
//! - **String Interning**: Deduplicates repeated strings (paths, symbols, languages)
//! - **Vector Quantization**: Compresses embeddings from f32 to i8 (4x reduction)
//! - **Buffer Pooling**: Reuses buffers for file reading and parsing
//! - **Memory Metrics**: Tracks allocations and memory usage
//!
//! # Performance Target
//!
//! Memory usage <500MB for 100k chunks with:
//! - String interning for paths and symbols
//! - Quantized embeddings (f32 → i8)
//! - Pooled buffers for I/O operations
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────┐
//! │ Memory Optimization │
//! ├─────────────────────────────────────────────┤
//! │ │
//! │ String Interning Vector Quantization │
//! │ ┌──────────────┐ ┌──────────────┐ │
//! │ │ Interner │ │ Quantizer │ │
//! │ │ HashMap │ │ f32 → i8 │ │
//! │ │ Arc<str> │ │ 4x reduction │ │
//! │ └──────────────┘ └──────────────┘ │
//! │ │
//! │ Buffer Pooling Memory Metrics │
//! │ ┌──────────────┐ ┌──────────────┐ │
//! │ │ Pool │ │ Allocations │ │
//! │ │ Reusable │ │ Usage │ │
//! │ │ Vec<u8> │ │ Peak │ │
//! │ └──────────────┘ └──────────────┘ │
//! │ │
//! └─────────────────────────────────────────────┘
//! ```
//!
//! # Example
//!
//! ```no_run
//! use maproom::memory::{
//! StringInterner, quantize_embedding, BufferPool, get_memory_metrics
//! };
//! use std::sync::Arc;
//!
//! # fn main() {
//! // String interning
//! let interner = StringInterner::new();
//! let path1 = interner.intern("src/main.rs");
//! let path2 = interner.intern("src/main.rs"); // Same Arc
//! assert!(Arc::ptr_eq(&path1, &path2));
//!
//! // Vector quantization
//! let embedding = vec![0.5, -0.3, 0.8];
//! let quantized = quantize_embedding(&embedding);
//! assert_eq!(quantized.len(), embedding.len());
//!
//! // Buffer pooling
//! let pool = BufferPool::new(1024 * 64, 10);
//! let mut buffer = pool.acquire();
//! // Use buffer for reading...
//! drop(buffer); // Returns to pool
//!
//! // Memory metrics
//! let metrics = get_memory_metrics();
//! println!("Current usage: {} bytes", metrics.current_bytes());
//! # }
//! ```
pub use ;
pub use ;
pub use ;
pub use ;