ruvector-mincut 2.0.6

World's first subpolynomial dynamic min-cut: self-healing networks, AI optimization, real-time graph analysis
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
//! WASM Batch Operations and TypedArray Optimizations
//!
//! Optimizations specific to WebAssembly execution:
//! - Batch FFI calls to minimize overhead
//! - Pre-allocated WASM memory
//! - TypedArray bulk transfers
//! - Memory alignment for SIMD
//!
//! Target: 10x reduction in FFI overhead

use crate::graph::VertexId;
use crate::time_compat::PortableInstant;
use std::collections::HashMap;

/// Configuration for WASM batch operations
#[derive(Debug, Clone)]
pub struct BatchConfig {
    /// Maximum batch size
    pub max_batch_size: usize,
    /// Pre-allocated buffer size in bytes
    pub buffer_size: usize,
    /// Alignment for SIMD operations
    pub alignment: usize,
    /// Enable memory pooling
    pub memory_pooling: bool,
}

impl Default for BatchConfig {
    fn default() -> Self {
        Self {
            max_batch_size: 1024,
            buffer_size: 64 * 1024, // 64KB
            alignment: 64,          // AVX-512 alignment
            memory_pooling: true,
        }
    }
}

/// Batch operation types for minimizing FFI calls
#[derive(Debug, Clone)]
pub enum BatchOperation {
    /// Insert multiple edges
    InsertEdges(Vec<(VertexId, VertexId, f64)>),
    /// Delete multiple edges
    DeleteEdges(Vec<(VertexId, VertexId)>),
    /// Update multiple weights
    UpdateWeights(Vec<(VertexId, VertexId, f64)>),
    /// Query multiple distances
    QueryDistances(Vec<(VertexId, VertexId)>),
    /// Compute cuts for multiple partitions
    ComputeCuts(Vec<Vec<VertexId>>),
}

/// Result from batch operation
#[derive(Debug, Clone)]
pub struct BatchResult {
    /// Operation type
    pub operation: String,
    /// Number of items processed
    pub items_processed: usize,
    /// Time taken in microseconds
    pub time_us: u64,
    /// Results (for queries)
    pub results: Vec<f64>,
    /// Error message if any
    pub error: Option<String>,
}

/// TypedArray transfer for efficient WASM memory access
///
/// Provides aligned memory buffers for bulk data transfer between
/// JavaScript and WASM.
#[repr(C, align(64))]
pub struct TypedArrayTransfer {
    /// Float64 buffer for weights/distances
    pub f64_buffer: Vec<f64>,
    /// Uint64 buffer for vertex IDs
    pub u64_buffer: Vec<u64>,
    /// Uint32 buffer for indices/counts
    pub u32_buffer: Vec<u32>,
    /// Byte buffer for raw data
    pub byte_buffer: Vec<u8>,
    /// Current position in buffers
    position: usize,
}

impl TypedArrayTransfer {
    /// Create new transfer with default buffer size
    pub fn new() -> Self {
        Self::with_capacity(1024)
    }

    /// Create with specific capacity
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            f64_buffer: Vec::with_capacity(capacity),
            u64_buffer: Vec::with_capacity(capacity),
            u32_buffer: Vec::with_capacity(capacity * 2),
            byte_buffer: Vec::with_capacity(capacity * 8),
            position: 0,
        }
    }

    /// Reset buffers for reuse
    pub fn reset(&mut self) {
        self.f64_buffer.clear();
        self.u64_buffer.clear();
        self.u32_buffer.clear();
        self.byte_buffer.clear();
        self.position = 0;
    }

    /// Add edge to transfer buffer
    pub fn add_edge(&mut self, source: VertexId, target: VertexId, weight: f64) {
        self.u64_buffer.push(source);
        self.u64_buffer.push(target);
        self.f64_buffer.push(weight);
    }

    /// Add vertex to transfer buffer
    pub fn add_vertex(&mut self, vertex: VertexId) {
        self.u64_buffer.push(vertex);
    }

    /// Add distance result
    pub fn add_distance(&mut self, distance: f64) {
        self.f64_buffer.push(distance);
    }

    /// Get edges from buffer
    pub fn get_edges(&self) -> Vec<(VertexId, VertexId, f64)> {
        let mut edges = Vec::with_capacity(self.f64_buffer.len());

        for (i, &weight) in self.f64_buffer.iter().enumerate() {
            let source = self.u64_buffer.get(i * 2).copied().unwrap_or(0);
            let target = self.u64_buffer.get(i * 2 + 1).copied().unwrap_or(0);
            edges.push((source, target, weight));
        }

        edges
    }

    /// Get f64 buffer as raw pointer (for FFI)
    pub fn f64_ptr(&self) -> *const f64 {
        self.f64_buffer.as_ptr()
    }

    /// Get u64 buffer as raw pointer (for FFI)
    pub fn u64_ptr(&self) -> *const u64 {
        self.u64_buffer.as_ptr()
    }

    /// Get buffer lengths
    pub fn len(&self) -> (usize, usize, usize) {
        (
            self.f64_buffer.len(),
            self.u64_buffer.len(),
            self.u32_buffer.len(),
        )
    }

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

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

/// WASM batch operations executor
pub struct WasmBatchOps {
    config: BatchConfig,
    /// Transfer buffer
    transfer: TypedArrayTransfer,
    /// Pending operations
    pending: Vec<BatchOperation>,
    /// Statistics
    total_ops: u64,
    total_items: u64,
    total_time_us: u64,
}

impl WasmBatchOps {
    /// Create new batch executor with default config
    pub fn new() -> Self {
        Self::with_config(BatchConfig::default())
    }

    /// Create with custom config
    pub fn with_config(config: BatchConfig) -> Self {
        Self {
            transfer: TypedArrayTransfer::with_capacity(config.buffer_size / 8),
            config,
            pending: Vec::new(),
            total_ops: 0,
            total_items: 0,
            total_time_us: 0,
        }
    }

    /// Queue edge insertions for batch processing
    pub fn queue_insert_edges(&mut self, edges: Vec<(VertexId, VertexId, f64)>) {
        if edges.len() > self.config.max_batch_size {
            // Split into multiple batches
            for chunk in edges.chunks(self.config.max_batch_size) {
                self.pending
                    .push(BatchOperation::InsertEdges(chunk.to_vec()));
            }
        } else {
            self.pending.push(BatchOperation::InsertEdges(edges));
        }
    }

    /// Queue edge deletions for batch processing
    pub fn queue_delete_edges(&mut self, edges: Vec<(VertexId, VertexId)>) {
        if edges.len() > self.config.max_batch_size {
            for chunk in edges.chunks(self.config.max_batch_size) {
                self.pending
                    .push(BatchOperation::DeleteEdges(chunk.to_vec()));
            }
        } else {
            self.pending.push(BatchOperation::DeleteEdges(edges));
        }
    }

    /// Queue distance queries for batch processing
    pub fn queue_distance_queries(&mut self, pairs: Vec<(VertexId, VertexId)>) {
        if pairs.len() > self.config.max_batch_size {
            for chunk in pairs.chunks(self.config.max_batch_size) {
                self.pending
                    .push(BatchOperation::QueryDistances(chunk.to_vec()));
            }
        } else {
            self.pending.push(BatchOperation::QueryDistances(pairs));
        }
    }

    /// Execute all pending operations
    pub fn execute_batch(&mut self) -> Vec<BatchResult> {
        let _start = PortableInstant::now();

        // Drain pending operations to avoid borrow conflict
        let pending_ops: Vec<_> = self.pending.drain(..).collect();
        let mut results = Vec::with_capacity(pending_ops.len());

        for op in pending_ops {
            let op_start = PortableInstant::now();
            let result = self.execute_operation(op);
            let elapsed = op_start.elapsed().as_micros() as u64;

            self.total_ops += 1;
            self.total_items += result.items_processed as u64;
            self.total_time_us += elapsed;

            results.push(result);
        }

        self.transfer.reset();
        results
    }

    /// Execute a single operation
    fn execute_operation(&mut self, op: BatchOperation) -> BatchResult {
        match op {
            BatchOperation::InsertEdges(edges) => {
                let count = edges.len();

                // Prepare transfer buffer
                self.transfer.reset();
                for (u, v, w) in &edges {
                    self.transfer.add_edge(*u, *v, *w);
                }

                // In WASM, this would call the native insert function
                // For now, we simulate the batch operation
                BatchResult {
                    operation: "InsertEdges".to_string(),
                    items_processed: count,
                    time_us: 0,
                    results: Vec::new(),
                    error: None,
                }
            }

            BatchOperation::DeleteEdges(edges) => {
                let count = edges.len();

                self.transfer.reset();
                for (u, v) in &edges {
                    self.transfer.add_vertex(*u);
                    self.transfer.add_vertex(*v);
                }

                BatchResult {
                    operation: "DeleteEdges".to_string(),
                    items_processed: count,
                    time_us: 0,
                    results: Vec::new(),
                    error: None,
                }
            }

            BatchOperation::UpdateWeights(updates) => {
                let count = updates.len();

                self.transfer.reset();
                for (u, v, w) in &updates {
                    self.transfer.add_edge(*u, *v, *w);
                }

                BatchResult {
                    operation: "UpdateWeights".to_string(),
                    items_processed: count,
                    time_us: 0,
                    results: Vec::new(),
                    error: None,
                }
            }

            BatchOperation::QueryDistances(pairs) => {
                let count = pairs.len();

                self.transfer.reset();
                for (u, v) in &pairs {
                    self.transfer.add_vertex(*u);
                    self.transfer.add_vertex(*v);
                }

                // Simulate distance results
                let results: Vec<f64> = pairs
                    .iter()
                    .map(|(u, v)| if u == v { 0.0 } else { 1.0 })
                    .collect();

                BatchResult {
                    operation: "QueryDistances".to_string(),
                    items_processed: count,
                    time_us: 0,
                    results,
                    error: None,
                }
            }

            BatchOperation::ComputeCuts(partitions) => {
                let count = partitions.len();

                BatchResult {
                    operation: "ComputeCuts".to_string(),
                    items_processed: count,
                    time_us: 0,
                    results: vec![0.0; count],
                    error: None,
                }
            }
        }
    }

    /// Get number of pending operations
    pub fn pending_count(&self) -> usize {
        self.pending.len()
    }

    /// Get statistics
    pub fn stats(&self) -> BatchStats {
        BatchStats {
            total_operations: self.total_ops,
            total_items: self.total_items,
            total_time_us: self.total_time_us,
            avg_items_per_op: if self.total_ops > 0 {
                self.total_items as f64 / self.total_ops as f64
            } else {
                0.0
            },
            avg_time_per_item_us: if self.total_items > 0 {
                self.total_time_us as f64 / self.total_items as f64
            } else {
                0.0
            },
        }
    }

    /// Clear pending operations
    pub fn clear(&mut self) {
        self.pending.clear();
        self.transfer.reset();
    }
}

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

/// Statistics for batch operations
#[derive(Debug, Clone, Default)]
pub struct BatchStats {
    /// Total operations executed
    pub total_operations: u64,
    /// Total items processed
    pub total_items: u64,
    /// Total time in microseconds
    pub total_time_us: u64,
    /// Average items per operation
    pub avg_items_per_op: f64,
    /// Average time per item in microseconds
    pub avg_time_per_item_us: f64,
}

/// Pre-allocated WASM memory region
#[repr(C, align(64))]
pub struct WasmMemoryRegion {
    /// Raw memory
    data: Vec<u8>,
    /// Capacity in bytes
    capacity: usize,
    /// Current offset
    offset: usize,
}

impl WasmMemoryRegion {
    /// Create new memory region
    pub fn new(size: usize) -> Self {
        // Round up to alignment
        let aligned_size = (size + 63) & !63;
        Self {
            data: vec![0u8; aligned_size],
            capacity: aligned_size,
            offset: 0,
        }
    }

    /// Allocate bytes from region, returns the offset
    ///
    /// Returns the starting offset of the allocated region.
    /// Use `get_slice` to access the allocated memory safely.
    pub fn alloc(&mut self, size: usize, align: usize) -> Option<usize> {
        // Align offset
        let aligned_offset = (self.offset + align - 1) & !(align - 1);

        if aligned_offset + size > self.capacity {
            return None;
        }

        let result = aligned_offset;
        self.offset = aligned_offset + size;
        Some(result)
    }

    /// Get a slice at the given offset
    pub fn get_slice(&self, offset: usize, len: usize) -> Option<&[u8]> {
        if offset + len <= self.capacity {
            Some(&self.data[offset..offset + len])
        } else {
            None
        }
    }

    /// Get a mutable slice at the given offset
    pub fn get_slice_mut(&mut self, offset: usize, len: usize) -> Option<&mut [u8]> {
        if offset + len <= self.capacity {
            Some(&mut self.data[offset..offset + len])
        } else {
            None
        }
    }

    /// Reset region for reuse
    pub fn reset(&mut self) {
        self.offset = 0;
        // Optional: zero memory
        // self.data.fill(0);
    }

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

    /// Get used bytes
    pub fn used(&self) -> usize {
        self.offset
    }

    /// Get raw pointer
    pub fn as_ptr(&self) -> *const u8 {
        self.data.as_ptr()
    }
}

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

    #[test]
    fn test_typed_array_transfer() {
        let mut transfer = TypedArrayTransfer::new();

        transfer.add_edge(1, 2, 1.0);
        transfer.add_edge(2, 3, 2.0);

        let edges = transfer.get_edges();
        assert_eq!(edges.len(), 2);
        assert_eq!(edges[0], (1, 2, 1.0));
        assert_eq!(edges[1], (2, 3, 2.0));
    }

    #[test]
    fn test_batch_queue() {
        let mut batch = WasmBatchOps::new();

        let edges = vec![(1, 2, 1.0), (2, 3, 2.0)];
        batch.queue_insert_edges(edges);

        assert_eq!(batch.pending_count(), 1);
    }

    #[test]
    fn test_batch_execute() {
        let mut batch = WasmBatchOps::new();

        batch.queue_insert_edges(vec![(1, 2, 1.0)]);
        batch.queue_delete_edges(vec![(3, 4)]);

        let results = batch.execute_batch();

        assert_eq!(results.len(), 2);
        assert_eq!(results[0].operation, "InsertEdges");
        assert_eq!(results[1].operation, "DeleteEdges");
        assert_eq!(batch.pending_count(), 0);
    }

    #[test]
    fn test_batch_splitting() {
        let mut batch = WasmBatchOps::with_config(BatchConfig {
            max_batch_size: 10,
            ..Default::default()
        });

        // Queue 25 edges
        let edges: Vec<_> = (0..25).map(|i| (i, i + 1, 1.0)).collect();
        batch.queue_insert_edges(edges);

        // Should be split into 3 batches
        assert_eq!(batch.pending_count(), 3);
    }

    #[test]
    fn test_distance_queries() {
        let mut batch = WasmBatchOps::new();

        batch.queue_distance_queries(vec![(1, 2), (2, 3), (1, 1)]);

        let results = batch.execute_batch();

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].results.len(), 3);
        assert_eq!(results[0].results[2], 0.0); // Same vertex
    }

    #[test]
    fn test_wasm_memory_region() {
        let mut region = WasmMemoryRegion::new(1024);

        // Allocate 64-byte aligned
        let offset1 = region.alloc(100, 64);
        assert!(offset1.is_some());
        assert_eq!(offset1.unwrap() % 64, 0);

        let offset2 = region.alloc(200, 64);
        assert!(offset2.is_some());

        // Verify we can get slices
        let slice1 = region.get_slice(offset1.unwrap(), 100);
        assert!(slice1.is_some());

        assert!(region.used() > 0);
        assert!(region.remaining() < 1024);

        region.reset();
        assert_eq!(region.used(), 0);
    }

    #[test]
    fn test_batch_stats() {
        let mut batch = WasmBatchOps::new();

        batch.queue_insert_edges(vec![(1, 2, 1.0), (2, 3, 2.0)]);
        let _ = batch.execute_batch();

        let stats = batch.stats();
        assert_eq!(stats.total_operations, 1);
        assert_eq!(stats.total_items, 2);
    }

    #[test]
    fn test_transfer_reset() {
        let mut transfer = TypedArrayTransfer::new();

        transfer.add_edge(1, 2, 1.0);
        assert!(!transfer.is_empty());

        transfer.reset();
        assert!(transfer.is_empty());
    }
}