ringkernel-core 0.4.2

Core traits and types for RingKernel GPU-native actor system
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
//! Analytics context for grouped buffer lifecycle management.
//!
//! This module provides the `AnalyticsContext` type for managing buffer
//! allocations during analytics operations like DFG mining, BFS traversal,
//! and pattern detection.
//!
//! # Purpose
//!
//! Analytics operations often need multiple temporary buffers that are
//! used together and released together. The `AnalyticsContext` provides:
//!
//! - **Grouped lifecycle**: All buffers are released when the context drops
//! - **Statistics tracking**: Peak memory usage, allocation count
//! - **Named contexts**: For debugging and profiling
//!
//! # Example
//!
//! ```
//! use ringkernel_core::analytics_context::AnalyticsContext;
//!
//! // Create context for a BFS operation
//! let mut ctx = AnalyticsContext::new("bfs_traversal");
//!
//! // Allocate buffers for frontier, visited, and distances
//! let frontier_idx = ctx.allocate(1024);
//! let visited_idx = ctx.allocate(1024);
//! let distances_idx = ctx.allocate_typed::<u32>(256);
//!
//! // Use the buffers
//! ctx.get_mut(frontier_idx)[0] = 1;
//!
//! // Check stats
//! let stats = ctx.stats();
//! println!("Peak memory: {} bytes", stats.peak_bytes);
//!
//! // All buffers released when ctx drops
//! ```

use std::any::TypeId;
use std::collections::HashMap;

/// Statistics for an analytics context.
#[derive(Debug, Clone, Default)]
pub struct ContextStats {
    /// Total number of allocations made.
    pub allocations: usize,
    /// Peak memory usage in bytes.
    pub peak_bytes: usize,
    /// Current memory usage in bytes.
    pub current_bytes: usize,
    /// Number of typed allocations (via allocate_typed).
    pub typed_allocations: usize,
    /// Allocation counts by type (for typed allocations).
    pub allocations_by_type: HashMap<TypeId, usize>,
}

impl ContextStats {
    /// Create new empty stats.
    pub fn new() -> Self {
        Self::default()
    }

    /// Get memory efficiency (1.0 = all allocated memory still in use).
    pub fn memory_efficiency(&self) -> f64 {
        if self.peak_bytes > 0 {
            self.current_bytes as f64 / self.peak_bytes as f64
        } else {
            1.0
        }
    }
}

/// Handle to an allocation within an AnalyticsContext.
///
/// This is an opaque index type for type safety.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct AllocationHandle(usize);

impl AllocationHandle {
    /// Get the raw index (for advanced use).
    pub fn index(&self) -> usize {
        self.0
    }
}

/// Context for analytics operations with grouped buffer lifecycle.
///
/// All buffers allocated through this context are released together
/// when the context is dropped. This is useful for analytics operations
/// that need multiple temporary buffers.
///
/// # Thread Safety
///
/// `AnalyticsContext` is `Send` but not `Sync`. Each context should be
/// used from a single thread. For parallel analytics, create a separate
/// context per thread.
pub struct AnalyticsContext {
    /// Context name for debugging.
    name: String,
    /// Allocated buffers.
    allocations: Vec<Box<[u8]>>,
    /// Allocation sizes (for potential future deallocation).
    sizes: Vec<usize>,
    /// Statistics.
    stats: ContextStats,
}

impl AnalyticsContext {
    /// Create a new analytics context.
    ///
    /// # Arguments
    ///
    /// * `name` - Descriptive name for debugging and profiling
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            allocations: Vec::new(),
            sizes: Vec::new(),
            stats: ContextStats::new(),
        }
    }

    /// Create a context with pre-allocated capacity.
    ///
    /// Reserves space for the expected number of allocations to avoid
    /// reallocations of the internal vectors.
    pub fn with_capacity(name: impl Into<String>, expected_allocations: usize) -> Self {
        Self {
            name: name.into(),
            allocations: Vec::with_capacity(expected_allocations),
            sizes: Vec::with_capacity(expected_allocations),
            stats: ContextStats::new(),
        }
    }

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

    /// Allocate a buffer of the specified size.
    ///
    /// Returns a handle that can be used with `get()` and `get_mut()`.
    ///
    /// # Arguments
    ///
    /// * `size` - Size in bytes
    ///
    /// # Returns
    ///
    /// Handle to the allocated buffer
    pub fn allocate(&mut self, size: usize) -> AllocationHandle {
        let buf = vec![0u8; size].into_boxed_slice();
        let handle = AllocationHandle(self.allocations.len());

        self.allocations.push(buf);
        self.sizes.push(size);

        self.stats.allocations += 1;
        self.stats.current_bytes += size;
        self.stats.peak_bytes = self.stats.peak_bytes.max(self.stats.current_bytes);

        handle
    }

    /// Allocate a typed buffer.
    ///
    /// Allocates space for `count` elements of type `T`, zero-initialized.
    ///
    /// # Arguments
    ///
    /// * `count` - Number of elements
    ///
    /// # Returns
    ///
    /// Handle to the allocated buffer
    ///
    /// # Type Parameters
    ///
    /// * `T` - Element type (must be Copy and have a meaningful zero value)
    pub fn allocate_typed<T: Copy + Default + 'static>(
        &mut self,
        count: usize,
    ) -> AllocationHandle {
        let size = count * std::mem::size_of::<T>();
        let handle = self.allocate(size);

        // Track typed allocation
        self.stats.typed_allocations += 1;
        *self
            .stats
            .allocations_by_type
            .entry(TypeId::of::<T>())
            .or_insert(0) += 1;

        handle
    }

    /// Get a reference to an allocated buffer.
    ///
    /// # Arguments
    ///
    /// * `handle` - Handle returned from `allocate()` or `allocate_typed()`
    ///
    /// # Panics
    ///
    /// Panics if the handle is invalid.
    pub fn get(&self, handle: AllocationHandle) -> &[u8] {
        &self.allocations[handle.0]
    }

    /// Get a mutable reference to an allocated buffer.
    ///
    /// # Arguments
    ///
    /// * `handle` - Handle returned from `allocate()` or `allocate_typed()`
    ///
    /// # Panics
    ///
    /// Panics if the handle is invalid.
    pub fn get_mut(&mut self, handle: AllocationHandle) -> &mut [u8] {
        &mut self.allocations[handle.0]
    }

    /// Get a typed reference to an allocated buffer.
    ///
    /// # Safety
    ///
    /// The caller must ensure the buffer was allocated with the correct type
    /// and size using `allocate_typed::<T>()`.
    ///
    /// # Panics
    ///
    /// Panics if the handle is invalid.
    pub fn get_typed<T: Copy>(&self, handle: AllocationHandle) -> &[T] {
        let bytes = &self.allocations[handle.0];
        let len = bytes.len() / std::mem::size_of::<T>();
        unsafe { std::slice::from_raw_parts(bytes.as_ptr() as *const T, len) }
    }

    /// Get a mutable typed reference to an allocated buffer.
    ///
    /// # Safety
    ///
    /// The caller must ensure the buffer was allocated with the correct type
    /// and size using `allocate_typed::<T>()`.
    ///
    /// # Panics
    ///
    /// Panics if the handle is invalid.
    pub fn get_typed_mut<T: Copy>(&mut self, handle: AllocationHandle) -> &mut [T] {
        let bytes = &mut self.allocations[handle.0];
        let len = bytes.len() / std::mem::size_of::<T>();
        unsafe { std::slice::from_raw_parts_mut(bytes.as_mut_ptr() as *mut T, len) }
    }

    /// Try to get a reference to an allocated buffer.
    ///
    /// Returns `None` if the handle is invalid.
    pub fn try_get(&self, handle: AllocationHandle) -> Option<&[u8]> {
        self.allocations.get(handle.0).map(|b| b.as_ref())
    }

    /// Try to get a mutable reference to an allocated buffer.
    ///
    /// Returns `None` if the handle is invalid.
    pub fn try_get_mut(&mut self, handle: AllocationHandle) -> Option<&mut [u8]> {
        self.allocations.get_mut(handle.0).map(|b| b.as_mut())
    }

    /// Get the size of an allocation.
    ///
    /// # Panics
    ///
    /// Panics if the handle is invalid.
    pub fn allocation_size(&self, handle: AllocationHandle) -> usize {
        self.sizes[handle.0]
    }

    /// Get the number of allocations in this context.
    pub fn allocation_count(&self) -> usize {
        self.allocations.len()
    }

    /// Get statistics for this context.
    pub fn stats(&self) -> &ContextStats {
        &self.stats
    }

    /// Release all allocations and reset the context.
    ///
    /// After calling this, all handles become invalid.
    pub fn release_all(&mut self) {
        self.allocations.clear();
        self.sizes.clear();
        self.stats.current_bytes = 0;
    }

    /// Create a sub-context for a nested operation.
    ///
    /// The sub-context shares no allocations with the parent.
    pub fn sub_context(&self, name: impl Into<String>) -> Self {
        Self::new(format!("{}::{}", self.name, name.into()))
    }
}

impl Drop for AnalyticsContext {
    fn drop(&mut self) {
        // All allocations are automatically freed when Vec drops
        // We could add logging here if needed:
        // log::trace!("Dropping AnalyticsContext '{}': {} bytes released", self.name, self.stats.current_bytes);
    }
}

/// Builder for AnalyticsContext with pre-configuration.
pub struct AnalyticsContextBuilder {
    name: String,
    expected_allocations: Option<usize>,
    preallocations: Vec<usize>,
}

impl AnalyticsContextBuilder {
    /// Create a new builder.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            expected_allocations: None,
            preallocations: Vec::new(),
        }
    }

    /// Set expected number of allocations (reserves vector capacity).
    pub fn with_expected_allocations(mut self, count: usize) -> Self {
        self.expected_allocations = Some(count);
        self
    }

    /// Pre-allocate a buffer of the given size when building.
    pub fn with_preallocation(mut self, size: usize) -> Self {
        self.preallocations.push(size);
        self
    }

    /// Build the context.
    pub fn build(self) -> AnalyticsContext {
        let mut ctx = match self.expected_allocations {
            Some(cap) => {
                AnalyticsContext::with_capacity(self.name, cap.max(self.preallocations.len()))
            }
            None => AnalyticsContext::new(self.name),
        };

        for size in self.preallocations {
            ctx.allocate(size);
        }

        ctx
    }
}

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

    #[test]
    fn test_context_creation() {
        let ctx = AnalyticsContext::new("test");
        assert_eq!(ctx.name(), "test");
        assert_eq!(ctx.allocation_count(), 0);
    }

    #[test]
    fn test_context_with_capacity() {
        let ctx = AnalyticsContext::with_capacity("test", 10);
        assert_eq!(ctx.name(), "test");
        assert_eq!(ctx.allocation_count(), 0);
    }

    #[test]
    fn test_allocate() {
        let mut ctx = AnalyticsContext::new("test");

        let h1 = ctx.allocate(100);
        let h2 = ctx.allocate(200);

        assert_eq!(ctx.allocation_count(), 2);
        assert_eq!(ctx.allocation_size(h1), 100);
        assert_eq!(ctx.allocation_size(h2), 200);
        assert_eq!(ctx.get(h1).len(), 100);
        assert_eq!(ctx.get(h2).len(), 200);
    }

    #[test]
    fn test_allocate_typed() {
        let mut ctx = AnalyticsContext::new("test");

        let h = ctx.allocate_typed::<u32>(10);

        assert_eq!(ctx.allocation_size(h), 40); // 10 * 4 bytes
        assert_eq!(ctx.get_typed::<u32>(h).len(), 10);

        let stats = ctx.stats();
        assert_eq!(stats.typed_allocations, 1);
    }

    #[test]
    fn test_get_mut() {
        let mut ctx = AnalyticsContext::new("test");

        let h = ctx.allocate(10);
        ctx.get_mut(h)[0] = 42;

        assert_eq!(ctx.get(h)[0], 42);
    }

    #[test]
    fn test_get_typed_mut() {
        let mut ctx = AnalyticsContext::new("test");

        let h = ctx.allocate_typed::<u32>(5);
        ctx.get_typed_mut::<u32>(h)[2] = 12345;

        assert_eq!(ctx.get_typed::<u32>(h)[2], 12345);
    }

    #[test]
    fn test_try_get() {
        let mut ctx = AnalyticsContext::new("test");

        let h = ctx.allocate(10);
        let invalid = AllocationHandle(999);

        assert!(ctx.try_get(h).is_some());
        assert!(ctx.try_get(invalid).is_none());
        assert!(ctx.try_get_mut(h).is_some());
        assert!(ctx.try_get_mut(invalid).is_none());
    }

    #[test]
    fn test_stats_tracking() {
        let mut ctx = AnalyticsContext::new("test");

        ctx.allocate(100);
        ctx.allocate(200);
        ctx.allocate(50);

        let stats = ctx.stats();
        assert_eq!(stats.allocations, 3);
        assert_eq!(stats.current_bytes, 350);
        assert_eq!(stats.peak_bytes, 350);
    }

    #[test]
    fn test_stats_peak_bytes() {
        let mut ctx = AnalyticsContext::new("test");

        ctx.allocate(100);
        ctx.allocate(200);
        let peak = ctx.stats().peak_bytes;

        ctx.release_all();
        assert_eq!(ctx.stats().current_bytes, 0);
        assert_eq!(ctx.stats().peak_bytes, peak); // Peak preserved after release
    }

    #[test]
    fn test_release_all() {
        let mut ctx = AnalyticsContext::new("test");

        let h1 = ctx.allocate(100);
        let h2 = ctx.allocate(200);

        assert_eq!(ctx.allocation_count(), 2);

        ctx.release_all();

        assert_eq!(ctx.allocation_count(), 0);
        assert_eq!(ctx.stats().current_bytes, 0);
        // Handles are now invalid - don't use them!
        assert!(ctx.try_get(h1).is_none());
        assert!(ctx.try_get(h2).is_none());
    }

    #[test]
    fn test_sub_context() {
        let parent = AnalyticsContext::new("parent");
        let child = parent.sub_context("child");

        assert_eq!(child.name(), "parent::child");
    }

    #[test]
    fn test_builder() {
        let ctx = AnalyticsContextBuilder::new("builder_test")
            .with_expected_allocations(10)
            .with_preallocation(100)
            .with_preallocation(200)
            .build();

        assert_eq!(ctx.name(), "builder_test");
        assert_eq!(ctx.allocation_count(), 2);
        assert_eq!(ctx.stats().current_bytes, 300);
    }

    #[test]
    fn test_context_stats_default() {
        let stats = ContextStats::default();
        assert_eq!(stats.allocations, 0);
        assert_eq!(stats.peak_bytes, 0);
        assert_eq!(stats.current_bytes, 0);
        assert_eq!(stats.memory_efficiency(), 1.0);
    }

    #[test]
    fn test_memory_efficiency() {
        let mut ctx = AnalyticsContext::new("test");

        ctx.allocate(100);
        ctx.allocate(100);
        // peak = 200, current = 200
        assert_eq!(ctx.stats().memory_efficiency(), 1.0);

        ctx.release_all();
        // peak = 200, current = 0
        assert_eq!(ctx.stats().memory_efficiency(), 0.0);
    }

    #[test]
    fn test_handle_index() {
        let mut ctx = AnalyticsContext::new("test");

        let h0 = ctx.allocate(10);
        let h1 = ctx.allocate(20);
        let h2 = ctx.allocate(30);

        assert_eq!(h0.index(), 0);
        assert_eq!(h1.index(), 1);
        assert_eq!(h2.index(), 2);
    }

    #[test]
    fn test_zero_allocation() {
        let mut ctx = AnalyticsContext::new("test");

        let h = ctx.allocate(0);
        assert_eq!(ctx.get(h).len(), 0);
        assert_eq!(ctx.allocation_size(h), 0);
    }

    #[test]
    fn test_large_allocation() {
        let mut ctx = AnalyticsContext::new("test");

        // 1 MB allocation
        let h = ctx.allocate(1024 * 1024);
        assert_eq!(ctx.get(h).len(), 1024 * 1024);
        assert_eq!(ctx.stats().current_bytes, 1024 * 1024);
    }
}