stoolap 0.4.0

High-performance embedded SQL database with MVCC, time-travel queries, and full ACID compliance
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
// Copyright 2025 Stoolap Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Self-tuning buffer pool for efficient memory reuse
//!
//!
//! This module provides a buffer pool that automatically tunes its buffer sizes
//! based on observed usage patterns. This reduces memory allocation overhead
//! and improves performance for I/O operations.

use crossbeam::queue::ArrayQueue;
use parking_lot::Mutex;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;

/// Default number of buffers in the pool
const DEFAULT_POOL_SIZE: usize = 64;

/// Default calibration samples to collect before tuning
const DEFAULT_MAX_SAMPLES: usize = 100;

/// Calibration threshold - tune after this many samples
const CALIBRATION_THRESHOLD: usize = 50;

/// Self-tuning buffer pool for efficient memory reuse
///
/// The pool automatically adjusts buffer sizes based on observed usage patterns
/// via the PoolCalibrator. Buffers are reused to reduce allocation overhead.
///
/// # Example
/// ```
/// use stoolap::common::BufferPool;
///
/// let pool = BufferPool::new(4096, 1024 * 1024, "test");
///
/// // Get a buffer from the pool
/// let mut buf = pool.get();
/// buf.extend_from_slice(b"hello world");
///
/// // Return the buffer to the pool for reuse
/// pool.put(buf);
/// ```
pub struct BufferPool {
    /// Queue of available buffers
    pool: ArrayQueue<Vec<u8>>,
    /// Calibrator for auto-tuning
    calibrator: Arc<Mutex<PoolCalibrator>>,
    /// Default size for new buffers
    default_size: AtomicUsize,
    /// Maximum allowed buffer size
    max_size: usize,
    /// Name of the pool (for debugging)
    pool_name: String,
    /// Total buffers created (for statistics)
    buffers_created: AtomicUsize,
    /// Total get operations
    get_count: AtomicUsize,
    /// Total put operations
    put_count: AtomicUsize,
}

impl BufferPool {
    /// Create a new buffer pool
    ///
    /// # Arguments
    /// * `default_size` - Initial size for allocated buffers
    /// * `max_size` - Maximum buffer size (buffers larger than this are discarded)
    /// * `name` - Name of the pool (for debugging/logging)
    pub fn new(default_size: usize, max_size: usize, name: &str) -> Self {
        Self::with_pool_size(default_size, max_size, name, DEFAULT_POOL_SIZE)
    }

    /// Create a new buffer pool with specified pool size
    pub fn with_pool_size(
        default_size: usize,
        max_size: usize,
        name: &str,
        pool_size: usize,
    ) -> Self {
        Self {
            pool: ArrayQueue::new(pool_size),
            calibrator: Arc::new(Mutex::new(PoolCalibrator::new(DEFAULT_MAX_SAMPLES))),
            default_size: AtomicUsize::new(default_size),
            max_size,
            pool_name: name.to_string(),
            buffers_created: AtomicUsize::new(0),
            get_count: AtomicUsize::new(0),
            put_count: AtomicUsize::new(0),
        }
    }

    /// Get a buffer from the pool
    ///
    /// Returns a buffer from the pool if available, otherwise creates a new one.
    /// The buffer is cleared before returning.
    pub fn get(&self) -> Vec<u8> {
        self.get_count.fetch_add(1, Ordering::Relaxed);

        match self.pool.pop() {
            Some(mut buf) => {
                buf.clear();
                buf
            }
            None => {
                self.buffers_created.fetch_add(1, Ordering::Relaxed);
                Vec::with_capacity(self.default_size.load(Ordering::Relaxed))
            }
        }
    }

    /// Get a buffer with at least the specified capacity
    pub fn get_with_capacity(&self, capacity: usize) -> Vec<u8> {
        self.get_count.fetch_add(1, Ordering::Relaxed);

        match self.pool.pop() {
            Some(mut buf) => {
                buf.clear();
                // After clear(), len=0, so reserve(n) ensures capacity >= n
                buf.reserve(capacity);
                buf
            }
            None => {
                self.buffers_created.fetch_add(1, Ordering::Relaxed);
                let size = capacity.max(self.default_size.load(Ordering::Relaxed));
                Vec::with_capacity(size)
            }
        }
    }

    /// Return a buffer to the pool
    ///
    /// The buffer is cleared and returned to the pool for reuse.
    /// Buffers larger than max_size are discarded to prevent memory bloat.
    pub fn put(&self, mut buf: Vec<u8>) {
        self.put_count.fetch_add(1, Ordering::Relaxed);

        // Record the size for calibration
        self.record_size(buf.capacity());

        // Don't keep oversized buffers
        if buf.capacity() > self.max_size {
            return;
        }

        // Clear the buffer before returning
        buf.clear();

        // Try to return to pool (ignore if full)
        let _ = self.pool.push(buf);
    }

    /// Record a buffer size for calibration
    ///
    /// This is called automatically during put(), but can also be called
    /// manually to record sizes without returning a buffer.
    pub fn record_size(&self, size: usize) {
        let mut calibrator = self.calibrator.lock();
        calibrator.record_sample(size);

        // Check if we should recalibrate
        if calibrator.should_calibrate() {
            let optimal = calibrator.calculate_optimal_size();
            if optimal > 0 {
                self.default_size.store(optimal, Ordering::Relaxed);
            }
            calibrator.mark_calibrated();
        }
    }

    /// Get the current optimal size based on calibration
    pub fn get_optimal_size(&self) -> usize {
        let calibrator = self.calibrator.lock();
        let optimal = calibrator.calculate_optimal_size();
        if optimal > 0 {
            optimal
        } else {
            self.default_size.load(Ordering::Relaxed)
        }
    }

    /// Get the current default buffer size
    pub fn default_size(&self) -> usize {
        self.default_size.load(Ordering::Relaxed)
    }

    /// Get the maximum allowed buffer size
    pub fn max_size(&self) -> usize {
        self.max_size
    }

    /// Get the pool name
    pub fn name(&self) -> &str {
        &self.pool_name
    }

    /// Get the number of buffers currently in the pool
    pub fn available(&self) -> usize {
        self.pool.len()
    }

    /// Get statistics about the pool
    pub fn stats(&self) -> PoolStats {
        PoolStats {
            name: self.pool_name.clone(),
            default_size: self.default_size.load(Ordering::Relaxed),
            max_size: self.max_size,
            available: self.pool.len(),
            buffers_created: self.buffers_created.load(Ordering::Relaxed),
            get_count: self.get_count.load(Ordering::Relaxed),
            put_count: self.put_count.load(Ordering::Relaxed),
            optimal_size: self.get_optimal_size(),
        }
    }
}

impl Default for BufferPool {
    fn default() -> Self {
        Self::new(4096, 1024 * 1024, "default")
    }
}

impl std::fmt::Debug for BufferPool {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BufferPool")
            .field("name", &self.pool_name)
            .field("default_size", &self.default_size.load(Ordering::Relaxed))
            .field("max_size", &self.max_size)
            .field("available", &self.pool.len())
            .finish()
    }
}

/// Statistics about a buffer pool
#[derive(Debug, Clone)]
pub struct PoolStats {
    /// Pool name
    pub name: String,
    /// Current default buffer size
    pub default_size: usize,
    /// Maximum buffer size
    pub max_size: usize,
    /// Number of buffers available in pool
    pub available: usize,
    /// Total buffers created
    pub buffers_created: usize,
    /// Total get operations
    pub get_count: usize,
    /// Total put operations
    pub put_count: usize,
    /// Current optimal size from calibrator
    pub optimal_size: usize,
}

impl std::fmt::Display for PoolStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "BufferPool '{}': size={}/{} available={} created={} get={} put={} optimal={}",
            self.name,
            self.default_size,
            self.max_size,
            self.available,
            self.buffers_created,
            self.get_count,
            self.put_count,
            self.optimal_size
        )
    }
}

/// Calibrator for auto-tuning buffer sizes
///
/// Collects samples of buffer sizes used and calculates an optimal size
/// based on the 75th percentile of observed sizes.
struct PoolCalibrator {
    /// Size samples collected
    samples: Vec<usize>,
    /// Maximum samples to keep
    max_samples: usize,
    /// Cached average size
    avg_size: f64,
    /// Whether calibration has been performed
    calibrated: bool,
    /// Number of samples since last calibration
    samples_since_calibration: usize,
}

impl PoolCalibrator {
    /// Create a new calibrator
    fn new(max_samples: usize) -> Self {
        Self {
            samples: Vec::with_capacity(max_samples),
            max_samples,
            avg_size: 0.0,
            calibrated: false,
            samples_since_calibration: 0,
        }
    }

    /// Record a size sample
    fn record_sample(&mut self, size: usize) {
        if self.samples.len() >= self.max_samples {
            // Remove oldest sample (rotate buffer)
            self.samples.remove(0);
        }
        self.samples.push(size);
        self.samples_since_calibration += 1;

        // Update running average
        let sum: usize = self.samples.iter().sum();
        self.avg_size = sum as f64 / self.samples.len() as f64;
    }

    /// Check if we should recalibrate
    fn should_calibrate(&self) -> bool {
        self.samples_since_calibration >= CALIBRATION_THRESHOLD
    }

    /// Mark that calibration has been performed
    fn mark_calibrated(&mut self) {
        self.calibrated = true;
        self.samples_since_calibration = 0;
    }

    /// Calculate the optimal buffer size
    ///
    /// Returns the 75th percentile of observed sizes, rounded up to the
    /// nearest power of 2 for efficient allocation.
    fn calculate_optimal_size(&self) -> usize {
        if self.samples.is_empty() {
            return 0;
        }

        // Sort samples to find percentiles
        let mut sorted: Vec<usize> = self.samples.clone();
        sorted.sort_unstable();

        // Use 75th percentile
        let p75_index = (sorted.len() * 75) / 100;
        let p75 = sorted[p75_index.min(sorted.len() - 1)];

        // Round up to next power of 2 for efficient allocation
        round_up_to_power_of_2(p75)
    }

    /// Get the average observed size
    #[allow(dead_code)]
    fn average_size(&self) -> f64 {
        self.avg_size
    }
}

/// Round up to the next power of 2
fn round_up_to_power_of_2(n: usize) -> usize {
    if n == 0 {
        return 1;
    }
    let mut v = n - 1;
    v |= v >> 1;
    v |= v >> 2;
    v |= v >> 4;
    v |= v >> 8;
    v |= v >> 16;
    #[cfg(target_pointer_width = "64")]
    {
        v |= v >> 32;
    }
    v + 1
}

/// Global buffer pools for common use cases
pub mod global {
    use super::BufferPool;
    use std::sync::OnceLock;

    static SMALL_POOL: OnceLock<BufferPool> = OnceLock::new();
    static MEDIUM_POOL: OnceLock<BufferPool> = OnceLock::new();
    static LARGE_POOL: OnceLock<BufferPool> = OnceLock::new();

    /// Small buffer pool (4KB default, 64KB max)
    pub fn small() -> &'static BufferPool {
        SMALL_POOL.get_or_init(|| BufferPool::new(4096, 64 * 1024, "small"))
    }

    /// Medium buffer pool (64KB default, 1MB max)
    pub fn medium() -> &'static BufferPool {
        MEDIUM_POOL.get_or_init(|| BufferPool::new(64 * 1024, 1024 * 1024, "medium"))
    }

    /// Large buffer pool (1MB default, 16MB max)
    pub fn large() -> &'static BufferPool {
        LARGE_POOL.get_or_init(|| BufferPool::new(1024 * 1024, 16 * 1024 * 1024, "large"))
    }
}

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

    #[test]
    fn test_buffer_pool_new() {
        let pool = BufferPool::new(1024, 4096, "test");
        assert_eq!(pool.default_size(), 1024);
        assert_eq!(pool.max_size(), 4096);
        assert_eq!(pool.name(), "test");
    }

    #[test]
    fn test_buffer_pool_get() {
        let pool = BufferPool::new(1024, 4096, "test");

        let buf = pool.get();
        assert!(buf.capacity() >= 1024);
        assert!(buf.is_empty());
    }

    #[test]
    fn test_buffer_pool_get_with_capacity() {
        let pool = BufferPool::new(1024, 4096, "test");

        let buf = pool.get_with_capacity(2048);
        assert!(buf.capacity() >= 2048);
        assert!(buf.is_empty());
    }

    #[test]
    fn test_buffer_pool_put() {
        let pool = BufferPool::new(1024, 4096, "test");

        // Get a buffer, use it, put it back
        let mut buf = pool.get();
        buf.extend_from_slice(b"hello world");
        pool.put(buf);

        assert_eq!(pool.available(), 1);

        // Get it back - should be empty
        let buf = pool.get();
        assert!(buf.is_empty());
    }

    #[test]
    fn test_buffer_pool_reuse() {
        let pool = BufferPool::new(1024, 4096, "test");

        // Put some buffers
        for _ in 0..5 {
            let buf = pool.get();
            pool.put(buf);
        }

        // Check stats
        let stats = pool.stats();
        assert!(stats.available <= 5);
        assert!(stats.buffers_created <= 5);
    }

    #[test]
    fn test_buffer_pool_oversized_discard() {
        let pool = BufferPool::new(1024, 4096, "test");

        // Create an oversized buffer
        let buf = vec![0u8; 8192];
        pool.put(buf);

        // Oversized buffer should be discarded
        assert_eq!(pool.available(), 0);
    }

    #[test]
    fn test_buffer_pool_stats() {
        let pool = BufferPool::new(1024, 4096, "test");

        for _ in 0..10 {
            let buf = pool.get();
            pool.put(buf);
        }

        let stats = pool.stats();
        assert_eq!(stats.get_count, 10);
        assert_eq!(stats.put_count, 10);
    }

    #[test]
    fn test_buffer_pool_concurrent() {
        let pool = Arc::new(BufferPool::new(1024, 4096, "test"));
        let num_threads = 4;
        let ops_per_thread = 100;

        let handles: Vec<_> = (0..num_threads)
            .map(|_| {
                let pool = Arc::clone(&pool);
                thread::spawn(move || {
                    for _ in 0..ops_per_thread {
                        let mut buf = pool.get();
                        buf.extend_from_slice(b"test data");
                        pool.put(buf);
                    }
                })
            })
            .collect();

        for handle in handles {
            handle.join().unwrap();
        }

        let stats = pool.stats();
        assert_eq!(stats.get_count, num_threads * ops_per_thread);
        assert_eq!(stats.put_count, num_threads * ops_per_thread);
    }

    #[test]
    fn test_calibrator_basic() {
        let mut calibrator = PoolCalibrator::new(100);

        // Record some samples
        for i in 1..=10 {
            calibrator.record_sample(i * 100);
        }

        assert!(calibrator.average_size() > 0.0);
    }

    #[test]
    fn test_calibrator_optimal_size() {
        let mut calibrator = PoolCalibrator::new(100);

        // Record samples clustered around 1000
        for _ in 0..50 {
            calibrator.record_sample(900);
            calibrator.record_sample(1000);
            calibrator.record_sample(1100);
        }

        let optimal = calibrator.calculate_optimal_size();
        // Should be around 1024 (next power of 2 after ~1000)
        assert!((1024..=2048).contains(&optimal));
    }

    #[test]
    fn test_calibrator_auto_tune() {
        let pool = BufferPool::new(1024, 1024 * 1024, "test");

        // Record many samples of larger sizes
        for _ in 0..100 {
            pool.record_size(4000);
        }

        // Pool should have auto-tuned
        let optimal = pool.get_optimal_size();
        assert!(optimal >= 4096); // Should be at least 4096 (next power of 2)
    }

    #[test]
    fn test_round_up_to_power_of_2() {
        assert_eq!(round_up_to_power_of_2(0), 1);
        assert_eq!(round_up_to_power_of_2(1), 1);
        assert_eq!(round_up_to_power_of_2(2), 2);
        assert_eq!(round_up_to_power_of_2(3), 4);
        assert_eq!(round_up_to_power_of_2(4), 4);
        assert_eq!(round_up_to_power_of_2(5), 8);
        assert_eq!(round_up_to_power_of_2(1000), 1024);
        assert_eq!(round_up_to_power_of_2(1024), 1024);
        assert_eq!(round_up_to_power_of_2(1025), 2048);
    }

    #[test]
    fn test_default_pool() {
        let pool = BufferPool::default();
        assert_eq!(pool.default_size(), 4096);
        assert_eq!(pool.max_size(), 1024 * 1024);
    }

    #[test]
    fn test_global_pools() {
        // Just ensure global pools are accessible
        let _small = global::small();
        let _medium = global::medium();
        let _large = global::large();
    }

    #[test]
    fn test_pool_debug() {
        let pool = BufferPool::new(1024, 4096, "debug_test");
        let debug = format!("{:?}", pool);
        assert!(debug.contains("debug_test"));
        assert!(debug.contains("1024"));
    }

    #[test]
    fn test_stats_display() {
        let pool = BufferPool::new(1024, 4096, "display_test");
        let _ = pool.get();
        let stats = pool.stats();
        let display = stats.to_string();
        assert!(display.contains("display_test"));
    }
}