shardex 0.1.0

A high-performance memory-mapped vector search engine with ACID transactions and incremental updates
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
//! Memory pool for text operations to reduce allocation overhead
//!
//! This module provides RAII-managed memory pools for String and Vec<u8> buffers,
//! reducing allocation overhead in text processing operations.

use parking_lot::Mutex;
use std::sync::Arc;
use std::time::{Duration, SystemTime};

/// Configuration for memory pool behavior
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct MemoryPoolConfig {
    /// Maximum number of buffers to keep in each pool
    pub max_pool_size: usize,
    /// Maximum buffer capacity to keep in pool (larger buffers are discarded)
    pub max_buffer_capacity: usize,
    /// Time before unused buffers are eligible for cleanup
    pub buffer_ttl: Duration,
    /// Growth factor for buffer capacity when expanding
    pub growth_factor: f64,
}

impl Default for MemoryPoolConfig {
    fn default() -> Self {
        Self {
            max_pool_size: 1000,
            max_buffer_capacity: 1024 * 1024,     // 1MB
            buffer_ttl: Duration::from_secs(300), // 5 minutes
            growth_factor: 1.5,
        }
    }
}

/// Buffer entry with metadata for pool management
#[derive(Debug)]
#[allow(dead_code)]
struct BufferEntry<T> {
    buffer: T,
    last_used: SystemTime,
    usage_count: u64,
}

impl<T> BufferEntry<T> {
    fn new(buffer: T) -> Self {
        Self {
            buffer,
            last_used: SystemTime::now(),
            usage_count: 1,
        }
    }

    fn touch(&mut self) {
        self.last_used = SystemTime::now();
        self.usage_count += 1;
    }

    fn is_expired(&self, ttl: Duration) -> bool {
        self.last_used.elapsed().unwrap_or(Duration::ZERO) > ttl
    }
}

/// Statistics for memory pool performance
#[derive(Debug, Clone, Default)]
#[allow(dead_code)]
pub struct MemoryPoolStats {
    /// Total buffer requests
    pub total_requests: u64,
    /// Requests served from pool (cache hits)
    pub pool_hits: u64,
    /// Requests that required new allocation (cache misses)
    pub pool_misses: u64,
    /// Total buffers currently in string pool
    pub string_pool_size: usize,
    /// Total buffers currently in byte pool
    pub byte_pool_size: usize,
    /// Total capacity in string pool (bytes)
    pub string_pool_capacity: usize,
    /// Total capacity in byte pool (bytes)
    pub byte_pool_capacity: usize,
    /// Number of cleanup operations performed
    pub cleanup_operations: u64,
    /// Number of buffers discarded due to size limits
    pub oversized_discards: u64,
    /// Number of buffers discarded due to TTL
    pub expired_discards: u64,
}

impl MemoryPoolStats {
    /// Calculate hit ratio
    pub fn hit_ratio(&self) -> f64 {
        if self.total_requests == 0 {
            0.0
        } else {
            self.pool_hits as f64 / self.total_requests as f64
        }
    }

    /// Calculate average buffer size in string pool
    pub fn avg_string_buffer_size(&self) -> f64 {
        if self.string_pool_size == 0 {
            0.0
        } else {
            self.string_pool_capacity as f64 / self.string_pool_size as f64
        }
    }

    /// Calculate average buffer size in byte pool
    pub fn avg_byte_buffer_size(&self) -> f64 {
        if self.byte_pool_size == 0 {
            0.0
        } else {
            self.byte_pool_capacity as f64 / self.byte_pool_size as f64
        }
    }

    /// Calculate memory efficiency (hit ratio adjusted for buffer sizes)
    pub fn memory_efficiency(&self) -> f64 {
        let hit_ratio = self.hit_ratio();
        let total_capacity = self.string_pool_capacity + self.byte_pool_capacity;
        let total_buffers = self.string_pool_size + self.byte_pool_size;

        if total_buffers == 0 {
            hit_ratio
        } else {
            let avg_buffer_size = total_capacity as f64 / total_buffers as f64;
            let size_factor = (avg_buffer_size / 1024.0).min(1.0); // Normalize to KB
            hit_ratio * size_factor
        }
    }
}

/// Memory pool for text operations to reduce allocation overhead
#[allow(dead_code)]
pub struct TextMemoryPool {
    /// Pool of reusable string buffers
    string_pool: Arc<Mutex<Vec<BufferEntry<String>>>>,
    /// Pool of reusable byte vectors
    byte_pool: Arc<Mutex<Vec<BufferEntry<Vec<u8>>>>>,
    /// Pool configuration
    config: MemoryPoolConfig,
    /// Pool statistics
    stats: Arc<Mutex<MemoryPoolStats>>,
}

impl TextMemoryPool {
    /// Create new text memory pool with configuration
    pub fn new(config: MemoryPoolConfig) -> Self {
        Self {
            string_pool: Arc::new(Mutex::new(Vec::new())),
            byte_pool: Arc::new(Mutex::new(Vec::new())),
            config,
            stats: Arc::new(Mutex::new(MemoryPoolStats::default())),
        }
    }

    /// Create new text memory pool with default configuration
    pub fn new_default() -> Self {
        Self::new(MemoryPoolConfig::default())
    }

    /// Get string buffer from pool or create new one
    pub fn get_string_buffer(&self, min_capacity: usize) -> PooledString {
        self.record_request();

        let mut pool = self.string_pool.lock();

        // Look for suitable buffer in pool
        for i in (0..pool.len()).rev() {
            if pool[i].buffer.capacity() >= min_capacity {
                let mut entry = pool.swap_remove(i);
                entry.touch();
                entry.buffer.clear();

                // Expand capacity if needed with growth factor
                if entry.buffer.capacity() < min_capacity {
                    let new_capacity = (min_capacity as f64 * self.config.growth_factor) as usize;
                    entry.buffer.reserve(new_capacity - entry.buffer.capacity());
                }

                self.record_hit();
                return PooledString::new(
                    entry.buffer,
                    Arc::clone(&self.string_pool),
                    &self.config,
                    Arc::clone(&self.stats),
                );
            }
        }

        // No suitable buffer found, create new one
        let capacity = (min_capacity as f64 * self.config.growth_factor) as usize;
        let buffer = String::with_capacity(capacity);
        self.record_miss();

        PooledString::new(
            buffer,
            Arc::clone(&self.string_pool),
            &self.config,
            Arc::clone(&self.stats),
        )
    }

    /// Get byte buffer from pool or create new one
    pub fn get_byte_buffer(&self, min_capacity: usize) -> PooledBytes {
        self.record_request();

        let mut pool = self.byte_pool.lock();

        // Look for suitable buffer in pool
        for i in (0..pool.len()).rev() {
            if pool[i].buffer.capacity() >= min_capacity {
                let mut entry = pool.swap_remove(i);
                entry.touch();
                entry.buffer.clear();

                // Expand capacity if needed with growth factor
                if entry.buffer.capacity() < min_capacity {
                    let new_capacity = (min_capacity as f64 * self.config.growth_factor) as usize;
                    entry.buffer.reserve(new_capacity - entry.buffer.capacity());
                }

                self.record_hit();
                return PooledBytes::new(
                    entry.buffer,
                    Arc::clone(&self.byte_pool),
                    &self.config,
                    Arc::clone(&self.stats),
                );
            }
        }

        // No suitable buffer found, create new one
        let capacity = (min_capacity as f64 * self.config.growth_factor) as usize;
        let buffer = Vec::with_capacity(capacity);
        self.record_miss();

        PooledBytes::new(
            buffer,
            Arc::clone(&self.byte_pool),
            &self.config,
            Arc::clone(&self.stats),
        )
    }

    /// Pre-warm pool with buffers of specified capacities
    pub fn prewarm(&self, string_count: usize, string_capacity: usize, byte_count: usize, byte_capacity: usize) {
        // Pre-warm string pool
        {
            let mut pool = self.string_pool.lock();
            for _ in 0..string_count {
                if pool.len() >= self.config.max_pool_size {
                    break;
                }
                let buffer = String::with_capacity(string_capacity);
                pool.push(BufferEntry::new(buffer));
            }
        }

        // Pre-warm byte pool
        {
            let mut pool = self.byte_pool.lock();
            for _ in 0..byte_count {
                if pool.len() >= self.config.max_pool_size {
                    break;
                }
                let buffer = Vec::with_capacity(byte_capacity);
                pool.push(BufferEntry::new(buffer));
            }
        }

        log::debug!(
            "Pre-warmed pool with {} string buffers ({} bytes each) and {} byte buffers ({} bytes each)",
            string_count,
            string_capacity,
            byte_count,
            byte_capacity
        );
    }

    /// Perform cleanup of expired and oversized buffers
    pub fn cleanup(&self) {
        let mut cleanup_count = 0;

        // Clean up string pool
        {
            let mut pool = self.string_pool.lock();
            let original_len = pool.len();

            pool.retain(|entry| {
                let should_keep = !entry.is_expired(self.config.buffer_ttl)
                    && entry.buffer.capacity() <= self.config.max_buffer_capacity;

                if !should_keep {
                    cleanup_count += 1;
                    if entry.is_expired(self.config.buffer_ttl) {
                        self.record_expired_discard();
                    } else {
                        self.record_oversized_discard();
                    }
                }

                should_keep
            });

            log::trace!(
                "Cleaned up {} of {} string buffers",
                original_len - pool.len(),
                original_len
            );
        }

        // Clean up byte pool
        {
            let mut pool = self.byte_pool.lock();
            let original_len = pool.len();

            pool.retain(|entry| {
                let should_keep = !entry.is_expired(self.config.buffer_ttl)
                    && entry.buffer.capacity() <= self.config.max_buffer_capacity;

                if !should_keep {
                    cleanup_count += 1;
                    if entry.is_expired(self.config.buffer_ttl) {
                        self.record_expired_discard();
                    } else {
                        self.record_oversized_discard();
                    }
                }

                should_keep
            });

            log::trace!(
                "Cleaned up {} of {} byte buffers",
                original_len - pool.len(),
                original_len
            );
        }

        if cleanup_count > 0 {
            self.record_cleanup();
            log::debug!("Memory pool cleanup removed {} buffers", cleanup_count);
        }
    }

    /// Get current pool statistics
    pub fn get_stats(&self) -> MemoryPoolStats {
        let mut stats = self.stats.lock().clone();

        // Update current pool sizes
        {
            let string_pool = self.string_pool.lock();
            stats.string_pool_size = string_pool.len();
            stats.string_pool_capacity = string_pool.iter().map(|e| e.buffer.capacity()).sum();
        }

        {
            let byte_pool = self.byte_pool.lock();
            stats.byte_pool_size = byte_pool.len();
            stats.byte_pool_capacity = byte_pool.iter().map(|e| e.buffer.capacity()).sum();
        }

        stats
    }

    /// Reset pool statistics
    pub fn reset_stats(&self) {
        let mut stats = self.stats.lock();
        *stats = MemoryPoolStats::default();
    }

    /// Clear all pools and reset statistics
    pub fn clear(&self) {
        {
            let mut pool = self.string_pool.lock();
            pool.clear();
        }

        {
            let mut pool = self.byte_pool.lock();
            pool.clear();
        }

        self.reset_stats();
    }

    /// Get pool configuration
    pub fn config(&self) -> &MemoryPoolConfig {
        &self.config
    }

    // Statistics recording methods
    fn record_request(&self) {
        let mut stats = self.stats.lock();
        stats.total_requests += 1;
    }

    fn record_hit(&self) {
        let mut stats = self.stats.lock();
        stats.pool_hits += 1;
    }

    fn record_miss(&self) {
        let mut stats = self.stats.lock();
        stats.pool_misses += 1;
    }

    fn record_cleanup(&self) {
        let mut stats = self.stats.lock();
        stats.cleanup_operations += 1;
    }

    fn record_oversized_discard(&self) {
        let mut stats = self.stats.lock();
        stats.oversized_discards += 1;
    }

    fn record_expired_discard(&self) {
        let mut stats = self.stats.lock();
        stats.expired_discards += 1;
    }
}

/// RAII wrapper for pooled strings
#[allow(dead_code)]
pub struct PooledString {
    buffer: Option<String>,
    pool: Arc<Mutex<Vec<BufferEntry<String>>>>,
    config: MemoryPoolConfig,
    stats: Arc<Mutex<MemoryPoolStats>>,
}

impl PooledString {
    fn new(
        buffer: String,
        pool: Arc<Mutex<Vec<BufferEntry<String>>>>,
        config: &MemoryPoolConfig,
        stats: Arc<Mutex<MemoryPoolStats>>,
    ) -> Self {
        Self {
            buffer: Some(buffer),
            pool,
            config: config.clone(),
            stats,
        }
    }

    /// Get mutable access to the buffer
    pub fn buffer_mut(&mut self) -> &mut String {
        self.buffer
            .as_mut()
            .expect("Buffer should always be present when not dropped")
    }

    /// Get immutable access to the buffer
    pub fn buffer(&self) -> &String {
        self.buffer
            .as_ref()
            .expect("Buffer should always be present when not dropped")
    }

    /// Get buffer length
    pub fn len(&self) -> usize {
        self.buffer().len()
    }

    /// Check if buffer is empty
    pub fn is_empty(&self) -> bool {
        self.buffer().is_empty()
    }

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

    /// Take ownership of the buffer (prevents return to pool)
    pub fn into_inner(mut self) -> String {
        self.buffer
            .take()
            .expect("Buffer should always be present when not dropped")
    }
}

impl Drop for PooledString {
    fn drop(&mut self) {
        if let Some(buffer) = self.buffer.take() {
            let capacity = buffer.capacity();

            // Only return to pool if it's not too large
            if capacity <= self.config.max_buffer_capacity {
                let mut pool = self.pool.lock();

                // Only add if pool isn't full
                if pool.len() < self.config.max_pool_size {
                    pool.push(BufferEntry::new(buffer));
                    log::trace!("Returned string buffer to pool (capacity: {})", capacity);
                } else {
                    log::trace!("Discarded string buffer - pool full");
                }
            } else {
                // Record oversized discard
                let mut stats = self.stats.lock();
                stats.oversized_discards += 1;
                log::trace!("Discarded oversized string buffer (capacity: {})", capacity);
            }
        }
    }
}

/// RAII wrapper for pooled byte vectors
#[allow(dead_code)]
pub struct PooledBytes {
    buffer: Option<Vec<u8>>,
    pool: Arc<Mutex<Vec<BufferEntry<Vec<u8>>>>>,
    config: MemoryPoolConfig,
    stats: Arc<Mutex<MemoryPoolStats>>,
}

impl PooledBytes {
    fn new(
        buffer: Vec<u8>,
        pool: Arc<Mutex<Vec<BufferEntry<Vec<u8>>>>>,
        config: &MemoryPoolConfig,
        stats: Arc<Mutex<MemoryPoolStats>>,
    ) -> Self {
        Self {
            buffer: Some(buffer),
            pool,
            config: config.clone(),
            stats,
        }
    }

    /// Get mutable access to the buffer
    pub fn buffer_mut(&mut self) -> &mut Vec<u8> {
        self.buffer
            .as_mut()
            .expect("Buffer should always be present when not dropped")
    }

    /// Get immutable access to the buffer
    pub fn buffer(&self) -> &Vec<u8> {
        self.buffer
            .as_ref()
            .expect("Buffer should always be present when not dropped")
    }

    /// Get buffer length
    pub fn len(&self) -> usize {
        self.buffer().len()
    }

    /// Check if buffer is empty
    pub fn is_empty(&self) -> bool {
        self.buffer().is_empty()
    }

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

    /// Take ownership of the buffer (prevents return to pool)
    pub fn into_inner(mut self) -> Vec<u8> {
        self.buffer
            .take()
            .expect("Buffer should always be present when not dropped")
    }
}

impl Drop for PooledBytes {
    fn drop(&mut self) {
        if let Some(buffer) = self.buffer.take() {
            let capacity = buffer.capacity();

            // Only return to pool if it's not too large
            if capacity <= self.config.max_buffer_capacity {
                let mut pool = self.pool.lock();

                // Only add if pool isn't full
                if pool.len() < self.config.max_pool_size {
                    pool.push(BufferEntry::new(buffer));
                    log::trace!("Returned byte buffer to pool (capacity: {})", capacity);
                } else {
                    log::trace!("Discarded byte buffer - pool full");
                }
            } else {
                // Record oversized discard
                let mut stats = self.stats.lock();
                stats.oversized_discards += 1;
                log::trace!("Discarded oversized byte buffer (capacity: {})", capacity);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;
    use std::time::Duration;

    #[test]
    fn test_memory_pool_basic_operations() {
        let pool = TextMemoryPool::new_default();

        // Test string buffer
        let mut string_buf = pool.get_string_buffer(100);
        string_buf.buffer_mut().push_str("Hello, world!");
        assert_eq!(string_buf.buffer(), "Hello, world!");
        assert!(string_buf.capacity() >= 100);
        drop(string_buf);

        // Test byte buffer
        let mut byte_buf = pool.get_byte_buffer(50);
        byte_buf.buffer_mut().extend_from_slice(b"Hello, bytes!");
        assert_eq!(byte_buf.buffer(), b"Hello, bytes!");
        assert!(byte_buf.capacity() >= 50);
        drop(byte_buf);

        let stats = pool.get_stats();
        assert_eq!(stats.total_requests, 2);
        assert_eq!(stats.pool_misses, 2); // First requests are always misses
    }

    #[test]
    fn test_buffer_reuse() {
        let pool = TextMemoryPool::new_default();

        // Create and drop a buffer
        {
            let mut buf = pool.get_string_buffer(100);
            buf.buffer_mut().push_str("Test content");
            assert_eq!(buf.len(), 12);
        }

        // Get another buffer - should reuse the previous one
        {
            let buf = pool.get_string_buffer(50); // Smaller capacity, should still reuse
            assert!(buf.is_empty()); // Should be cleared
            assert!(buf.capacity() >= 100); // Should maintain original capacity
        }

        let stats = pool.get_stats();
        assert_eq!(stats.total_requests, 2);
        assert_eq!(stats.pool_hits, 1);
        assert_eq!(stats.pool_misses, 1);
        assert_eq!(stats.hit_ratio(), 0.5);
    }

    #[test]
    fn test_prewarming() {
        let pool = TextMemoryPool::new_default();

        // Pre-warm the pool
        pool.prewarm(10, 256, 5, 512);

        let stats = pool.get_stats();
        assert_eq!(stats.string_pool_size, 10);
        assert_eq!(stats.byte_pool_size, 5);
        assert!(stats.string_pool_capacity >= 10 * 256);
        assert!(stats.byte_pool_capacity >= 5 * 512);
    }

    #[test]
    fn test_oversized_buffer_handling() {
        let config = MemoryPoolConfig {
            max_buffer_capacity: 100, // Small limit for testing
            ..Default::default()
        };
        let pool = TextMemoryPool::new(config);

        // Create a large buffer that exceeds the limit
        {
            let mut buf = pool.get_string_buffer(200); // Exceeds max_buffer_capacity
            buf.buffer_mut().push_str("Large buffer content");
        }

        // The buffer should not be returned to the pool
        let stats = pool.get_stats();
        assert_eq!(stats.string_pool_size, 0);
        assert!(stats.oversized_discards > 0);
    }

    #[test]
    fn test_buffer_expiration() {
        let config = MemoryPoolConfig {
            buffer_ttl: Duration::from_millis(10), // Very short TTL for testing
            ..Default::default()
        };
        let pool = TextMemoryPool::new(config);

        // Create a buffer
        {
            let buf = pool.get_string_buffer(100);
            drop(buf);
        }

        // Wait for expiration
        thread::sleep(Duration::from_millis(20));

        // Clean up expired buffers
        pool.cleanup();

        let stats = pool.get_stats();
        assert_eq!(stats.string_pool_size, 0);
    }

    #[test]
    fn test_memory_pool_stats() {
        let pool = TextMemoryPool::new_default();

        // Generate some activity
        for _ in 0..10 {
            let buf = pool.get_string_buffer(100);
            drop(buf);
        }

        // Generate some hits
        for _ in 0..5 {
            let buf = pool.get_string_buffer(50);
            drop(buf);
        }

        let stats = pool.get_stats();
        assert_eq!(stats.total_requests, 15);
        assert!(stats.pool_hits > 0);
        assert!(stats.pool_misses > 0);
        assert!(stats.hit_ratio() > 0.0);
        assert!(stats.hit_ratio() <= 1.0);
    }

    #[test]
    fn test_pool_size_limits() {
        let config = MemoryPoolConfig {
            max_pool_size: 2, // Very small pool for testing
            ..Default::default()
        };
        let pool = TextMemoryPool::new(config);

        // Create more buffers than the pool can hold
        for _ in 0..5 {
            let buf = pool.get_string_buffer(100);
            drop(buf);
        }

        let stats = pool.get_stats();
        assert!(stats.string_pool_size <= 2); // Should not exceed max_pool_size
    }

    #[test]
    fn test_into_inner() {
        let pool = TextMemoryPool::new_default();

        let mut buf = pool.get_string_buffer(100);
        buf.buffer_mut().push_str("Test content");

        let owned_string = buf.into_inner();
        assert_eq!(owned_string, "Test content");

        // The buffer should not be returned to the pool
        let stats = pool.get_stats();
        assert_eq!(stats.string_pool_size, 0);
    }

    #[test]
    fn test_growth_factor() {
        let config = MemoryPoolConfig {
            growth_factor: 2.0,
            ..Default::default()
        };
        let pool = TextMemoryPool::new(config);

        let buf = pool.get_string_buffer(100);
        // Capacity should be at least 100 * 2.0 = 200
        assert!(buf.capacity() >= 200);
    }
}