tenflowers-dataset 0.1.1

Data pipeline and dataset utilities for TenfloweRS
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
//! Memory pooling utilities for dataset operations
//!
//! This module provides efficient memory allocation and reuse mechanisms
//! to reduce allocation overhead during dataset iteration and batch processing.

#![allow(unsafe_code)]

use std::alloc::{alloc, dealloc, Layout};
use std::collections::VecDeque;
use std::mem;
use std::ptr::NonNull;
use std::sync::{Arc, Mutex};

/// Memory pool statistics for monitoring
#[derive(Debug, Clone, Default)]
pub struct PoolStats {
    pub allocations: u64,
    pub deallocations: u64,
    pub cache_hits: u64,
    pub cache_misses: u64,
    pub current_size: usize,
    pub peak_size: usize,
}

impl PoolStats {
    /// Calculate the cache hit ratio
    pub fn hit_ratio(&self) -> f64 {
        if self.cache_hits + self.cache_misses == 0 {
            0.0
        } else {
            self.cache_hits as f64 / (self.cache_hits + self.cache_misses) as f64
        }
    }

    /// Get memory utilization efficiency
    pub fn efficiency(&self) -> f64 {
        if self.allocations == 0 {
            0.0
        } else {
            self.cache_hits as f64 / self.allocations as f64
        }
    }
}

/// A memory block that can be reused
#[derive(Debug)]
struct MemoryBlock {
    ptr: NonNull<u8>,
    size: usize,
    layout: Layout,
}

impl MemoryBlock {
    fn new(size: usize) -> Result<Self, String> {
        let layout = Layout::from_size_align(size, mem::align_of::<u8>())
            .map_err(|e| format!("Layout error: {e:?}"))?;

        let ptr = NonNull::new(unsafe { alloc(layout) })
            .ok_or_else(|| "Memory allocation failed".to_string())?;

        Ok(Self { ptr, size, layout })
    }

    /// Get a slice view of the memory block
    pub fn as_slice_mut(&mut self) -> &mut [u8] {
        unsafe { std::slice::from_raw_parts_mut(self.ptr.as_ptr(), self.size) }
    }

    /// Get the raw pointer
    pub fn as_ptr(&self) -> *mut u8 {
        self.ptr.as_ptr()
    }
}

impl Drop for MemoryBlock {
    fn drop(&mut self) {
        unsafe {
            dealloc(self.ptr.as_ptr(), self.layout);
        }
    }
}

// SAFETY: MemoryBlock owns its memory exclusively and the pointer is valid
unsafe impl Send for MemoryBlock {}
unsafe impl Sync for MemoryBlock {}

/// Memory pool for efficient allocation and reuse
pub struct MemoryPool {
    pools: Vec<Mutex<VecDeque<MemoryBlock>>>,
    max_blocks_per_size: usize,
    min_block_size: usize,
    max_block_size: usize,
    stats: Arc<Mutex<PoolStats>>,
}

impl MemoryPool {
    /// Create a new memory pool
    pub fn new() -> Self {
        Self::with_config(64, 1024, 1024 * 1024 * 16) // 1KB to 16MB
    }

    /// Create a memory pool with custom configuration
    pub fn with_config(
        max_blocks_per_size: usize,
        min_block_size: usize,
        max_block_size: usize,
    ) -> Self {
        // Create pools for different size classes (powers of 2)
        let mut size = min_block_size;
        let mut pools = Vec::new();

        while size <= max_block_size {
            pools.push(Mutex::new(VecDeque::new()));
            size *= 2;
        }

        Self {
            pools,
            max_blocks_per_size,
            min_block_size,
            max_block_size,
            stats: Arc::new(Mutex::new(PoolStats::default())),
        }
    }

    /// Find the appropriate size class for a requested size
    fn find_size_class(&self, size: usize) -> Option<usize> {
        if size < self.min_block_size || size > self.max_block_size {
            return None;
        }

        let mut class_size = self.min_block_size;
        let mut class_index = 0;

        while class_size < size && class_index < self.pools.len() {
            class_size *= 2;
            class_index += 1;
        }

        if class_index < self.pools.len() {
            Some(class_index)
        } else {
            None
        }
    }

    /// Allocate a memory block from the pool
    pub fn allocate(self: &Arc<Self>, size: usize) -> Result<PooledMemory, String> {
        let mut stats = self
            .stats
            .lock()
            .map_err(|e| format!("Failed to acquire stats lock: {e}"))?;
        stats.allocations += 1;

        if let Some(class_index) = self.find_size_class(size) {
            let mut pool = self.pools[class_index]
                .lock()
                .map_err(|e| format!("Failed to acquire pool lock: {e}"))?;

            if let Some(block) = pool.pop_front() {
                stats.cache_hits += 1;
                stats.current_size -= block.size;
                drop(stats);
                drop(pool);

                return Ok(PooledMemory {
                    block: Some(block),
                    pool: Arc::downgrade(self),
                    class_index: Some(class_index),
                });
            } else {
                stats.cache_misses += 1;
                drop(pool);
            }
        } else {
            stats.cache_misses += 1;
        }

        // Allocate new block
        let actual_size = if let Some(class_index) = self.find_size_class(size) {
            self.min_block_size << class_index
        } else {
            size
        };

        let block = MemoryBlock::new(actual_size)?;
        stats.current_size += block.size;
        stats.peak_size = stats.peak_size.max(stats.current_size);
        drop(stats);

        Ok(PooledMemory {
            block: Some(block),
            pool: Arc::downgrade(self),
            class_index: self.find_size_class(size),
        })
    }

    /// Return a memory block to the pool
    fn deallocate(&self, block: MemoryBlock, class_index: Option<usize>) {
        let mut stats = self.stats.lock().expect("lock should not be poisoned");
        stats.deallocations += 1;

        if let Some(class_index) = class_index {
            if class_index < self.pools.len() {
                let mut pool = self.pools[class_index]
                    .lock()
                    .expect("lock should not be poisoned");

                if pool.len() < self.max_blocks_per_size {
                    stats.current_size += block.size;
                    pool.push_back(block);
                    return;
                }
            }
        }

        // Block will be dropped automatically if not returned to pool
        stats.current_size = stats.current_size.saturating_sub(block.size);
    }

    /// Get current pool statistics
    pub fn stats(&self) -> PoolStats {
        self.stats
            .lock()
            .expect("lock should not be poisoned")
            .clone()
    }

    /// Clear all cached blocks
    pub fn clear(&self) {
        for pool in &self.pools {
            pool.lock().expect("lock should not be poisoned").clear();
        }

        let mut stats = self.stats.lock().expect("lock should not be poisoned");
        stats.current_size = 0;
    }
}

impl Clone for MemoryPool {
    fn clone(&self) -> Self {
        // Note: This creates a new pool with the same configuration
        // The actual cached blocks are not cloned
        Self::with_config(
            self.max_blocks_per_size,
            self.min_block_size,
            self.max_block_size,
        )
    }
}

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

/// A memory allocation from the pool that automatically returns to the pool on drop
pub struct PooledMemory {
    block: Option<MemoryBlock>,
    pool: std::sync::Weak<MemoryPool>,
    class_index: Option<usize>,
}

impl PooledMemory {
    /// Get the size of the allocated memory
    pub fn size(&self) -> usize {
        self.block.as_ref().map(|b| b.size).unwrap_or(0)
    }

    /// Get a mutable slice view of the memory
    pub fn as_slice_mut(&mut self) -> &mut [u8] {
        self.block
            .as_mut()
            .expect("block should exist for valid PooledMemory")
            .as_slice_mut()
    }

    /// Get the raw pointer
    pub fn as_ptr(&self) -> *mut u8 {
        self.block
            .as_ref()
            .expect("block should exist for valid PooledMemory")
            .as_ptr()
    }

    /// Convert to a `Vec<u8>` (consumes the pooled memory)
    pub fn into_vec(mut self) -> Vec<u8> {
        let block = self
            .block
            .take()
            .expect("block should exist for valid PooledMemory");
        let size = block.size;
        let ptr = block.as_ptr();

        // Prevent the block from being deallocated
        mem::forget(block);

        // Create a Vec from the raw pointer
        unsafe { Vec::from_raw_parts(ptr, size, size) }
    }
}

impl Drop for PooledMemory {
    fn drop(&mut self) {
        if let Some(block) = self.block.take() {
            if let Some(pool) = self.pool.upgrade() {
                pool.deallocate(block, self.class_index);
            }
            // If pool is dropped, block will be automatically deallocated
        }
    }
}

/// Thread-safe global memory pool
pub struct GlobalMemoryPool {
    pool: Arc<MemoryPool>,
}

impl GlobalMemoryPool {
    /// Get the global memory pool instance
    pub fn instance() -> &'static GlobalMemoryPool {
        static INSTANCE: std::sync::OnceLock<GlobalMemoryPool> = std::sync::OnceLock::new();
        INSTANCE.get_or_init(|| GlobalMemoryPool {
            pool: Arc::new(MemoryPool::new()),
        })
    }

    /// Allocate memory from the global pool
    pub fn allocate(size: usize) -> Result<PooledMemory, String> {
        Self::instance().pool.allocate(size)
    }

    /// Get global pool statistics
    pub fn stats() -> PoolStats {
        Self::instance().pool.stats()
    }

    /// Clear the global pool
    pub fn clear() {
        Self::instance().pool.clear()
    }
}

/// Extension trait for easy memory pool allocation
pub trait MemoryPoolExt<T> {
    /// Allocate a vector using the memory pool
    fn with_pool_capacity(capacity: usize) -> Result<Vec<T>, String>;
}

impl<T> MemoryPoolExt<T> for Vec<T> {
    fn with_pool_capacity(capacity: usize) -> Result<Vec<T>, String> {
        let size = capacity * mem::size_of::<T>();
        let pooled = GlobalMemoryPool::allocate(size)?;

        // Convert to Vec
        let vec = pooled.into_vec();

        // Cast to the proper type (this is safe since we allocated the right amount)
        let ptr = vec.as_ptr() as *mut T;
        let len = 0;

        mem::forget(vec); // Prevent deallocation

        Ok(unsafe { Vec::from_raw_parts(ptr, len, capacity) })
    }
}

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

    #[test]
    fn test_memory_pool_basic() {
        let pool = Arc::new(MemoryPool::new());

        // Allocate some memory
        let mut mem1 = pool.allocate(1024).expect("test: operation should succeed");
        assert_eq!(mem1.size(), 1024);

        // Write some data
        let slice = mem1.as_slice_mut();
        slice[0] = 42;
        slice[1023] = 99;

        drop(mem1);

        // Allocate again
        let mut mem2 = pool.allocate(1024).expect("test: operation should succeed");
        let slice2 = mem2.as_slice_mut();

        // Verify we can write to the new allocation
        slice2[0] = 100;
        assert_eq!(slice2[0], 100);

        let stats = pool.stats();
        assert_eq!(stats.allocations, 2);
        // With the Arc optimization, we should now see cache hits
        assert_eq!(stats.cache_misses, 1);
        assert_eq!(stats.cache_hits, 1);
    }

    #[test]
    fn test_memory_pool_different_sizes() {
        let pool = Arc::new(MemoryPool::new());

        let mem1 = pool.allocate(512).expect("test: operation should succeed");
        let mem2 = pool.allocate(1024).expect("test: operation should succeed");
        let mem3 = pool.allocate(2048).expect("test: operation should succeed");

        assert!(mem1.size() >= 512);
        assert!(mem2.size() >= 1024);
        assert!(mem3.size() >= 2048);

        drop(mem1);
        drop(mem2);
        drop(mem3);

        let stats = pool.stats();
        assert_eq!(stats.allocations, 3);
    }

    #[test]
    fn test_global_memory_pool() {
        let mem1 = GlobalMemoryPool::allocate(1024).expect("test: operation should succeed");
        assert_eq!(mem1.size(), 1024);

        let mem2 = GlobalMemoryPool::allocate(2048).expect("test: operation should succeed");
        assert!(mem2.size() >= 2048);

        // Basic functionality test - just verify allocations work
        drop(mem1);
        drop(mem2);

        let stats = GlobalMemoryPool::stats();
        assert!(stats.allocations >= 2);
    }

    #[test]
    fn test_vec_with_pool_capacity() {
        GlobalMemoryPool::clear();

        let mut vec: Vec<i32> =
            Vec::with_pool_capacity(100).expect("test: operation should succeed");
        vec.push(42);
        vec.push(99);

        assert_eq!(vec.len(), 2);
        assert_eq!(vec.capacity(), 100);
        assert_eq!(vec[0], 42);
        assert_eq!(vec[1], 99);
    }

    #[test]
    fn test_pool_stats() {
        let pool = Arc::new(MemoryPool::new());

        let stats = pool.stats();
        assert_eq!(stats.allocations, 0);
        assert_eq!(stats.deallocations, 0);
        assert_eq!(stats.cache_hits, 0);
        assert_eq!(stats.cache_misses, 0);
        assert_eq!(stats.hit_ratio(), 0.0);
        assert_eq!(stats.efficiency(), 0.0);

        let _mem = pool.allocate(1024).expect("test: operation should succeed");
        let stats = pool.stats();
        assert_eq!(stats.allocations, 1);
        assert_eq!(stats.cache_misses, 1);
    }
}