vex-algoswitch 1.7.0

Self-optimizing algorithm selection runtime with smart pattern detection
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
# AlgoSwitch - Algorithmic Runtime Switchboard

## Project Overview

**Name:** AlgoSwitch  
**Type:** Self-Optimizing Algorithm Runtime Library  
**Core Functionality:** Runtime algorithm selection that profiles multiple implementations, measures performance, and automatically selects the optimal algorithm for specific data patterns.  
**Target Users:** Performance-critical application developers, data engineers, systems programmers  
**Language:** Rust (for performance + safety)

---

## Problem Statement

### The Problem
- Developers must manually choose algorithms (sorting, searching, hashing)
- No single algorithm is optimal for all data patterns
- Data patterns change at runtime
- Benchmarking is time-consuming and often ignored

### The Solution
A runtime library that:
1. Executes multiple algorithm variants
2. Measures performance in real-time
3. Learns which algorithm works best for YOUR specific data
4. Automatically switches to the winner
5. Continuously re-evaluates as patterns change

---

## Core Concepts

### Execution Context
Information about current execution that affects algorithm performance:
- Input size
- Data distribution (sorted, random, partially ordered)
- Data type characteristics
- Memory access patterns
- CPU cache behavior

### Algorithm Family
A collection of algorithms solving the same problem:
- `sort`: quicksort, mergesort, heapsort, radix sort, insertion sort
- `search`: linear, binary, interpolation, exponential
- `hash`: fnv, siphash, xxhash, cityhash

### Selection Strategy
How the runtime picks the winner:
- **Brute Force**: Run all, pick fastest (warmup phase)
- **Pattern-Based**: Analyze data, predict best algorithm
- **Adaptive**: Run all initially, then short-circuit after learning
- **Shadow Mode**: Run candidate in background, swap if faster

### The Feedback Loop
```
┌─────────────┐    ┌──────────────┐    ┌─────────────────┐
│  Execute    │───▶│   Profile    │───▶│   Learn/Pick    │
│  Multiple   │    │   Timings    │    │   Winner        │
└─────────────┘    └──────────────┘    └─────────────────┘
       │                                       │
       │                                       ▼
       │                              ┌─────────────────┐
       └──────────────────────────────│   Cache Result  │
                                      │   for Context   │
                                      └─────────────────┘
```

---

## Architecture

### Module Structure

```
algoswitch/
├── Cargo.toml
├── src/
│   ├── lib.rs              # Main entry point
│   ├── core/
│   │   ├── mod.rs
│   │   ├── context.rs      # Execution context extraction
│   │   ├── profile.rs      # Timing and profiling
│   │   └── cache.rs        # Results caching by context
│   ├── strategies/
│   │   ├── mod.rs
│   │   ├── brute_force.rs
│   │   ├── pattern_based.rs
│   │   ├── adaptive.rs
│   │   └── shadow.rs
│   ├── algorithms/
│   │   ├── mod.rs
│   │   ├── sort.rs         # Sorting family
│   │   ├── search.rs       # Search family
│   │   ├── hash.rs         # Hash family
│   │   └── traits.rs       # Algorithm traits
│   ├── runtime/
│   │   ├── mod.rs
│   │   ├── executor.rs     # Multi-algo execution
│   │   ├── switcher.rs     # Algorithm switching logic
│   │   └── monitor.rs      # Continuous profiling
│   └── utils/
│       ├── mod.rs
│       ├── timing.rs       # High-res timing
│       └── patterns.rs     # Data pattern detection
├── tests/
│   ├── integration.rs
│   ├── algorithms/
│   └── strategies/
├── benchmarks/
│   └── benches/
└── examples/
    ├── simple_sort.rs
    └── real_world.rs
```

---

## Data Flow

```
User Code
┌─────────────────────────────────────────────┐
│  algo::select(AlgoFamily, data, config)    │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│  Context Extractor                          │
│  - Size, distribution, entropy, etc.       │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│  Cache Lookup (by context hash)             │
│  - Hit? Return cached winner               │
│  - Miss? Continue                          │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│  Strategy Executor                          │
│  - Run algorithms per selection strategy    │
│  - Profile each execution                   │
│  - Select winner                           │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│  Cache Update                               │
│  - Store winner for this context           │
│  - Optionally: update global statistics     │
└─────────────────────────────────────────────┘
┌─────────────────────────────────────────────┐
│  Return Result + Winner Info                │
└─────────────────────────────────────────────┘
```

---

## API Design

### Basic Usage

```rust
use algoswitch::prelude::*;
use algoswitch::algorithms::sort::*;
use algoswitch::strategies::Adaptive;

// Create algorithm family
let family = SortFamily::new()
    .with(QuickSort)
    .with(MergeSort)
    .with(HeapSort)
    .with(InsertionSort);

// Configure selection
let config = SelectConfig::default()
    .strategy(Adaptive::new()
        .warmup_runs(3)
        .confidence_threshold(0.95))
    .cache_persist(true);  // Save across runs

// Select and execute
let (sorted, winner) = algo::select(family, &mut data, config);

// winner tells you which algo was used
println!("Used: {:?} (took {}ns)", winner.name, winner.duration);
```

### Pattern Detection API

```rust
use algoswitch::patterns::*;

// Detect data patterns
let patterns = Patterns::analyze(&data);

// Use patterns to narrow algorithm choices
let suggestions = family.suggest(patterns);
```

### Custom Algorithm Family

```rust
use algoswitch::algorithms::traits::*;

pub struct MyAlgorithm;
impl Algorithm for MyAlgorithm {
    type Input = Vec<i32>;
    type Output = Vec<i32>;
    
    fn name(&self) -> &str { "my-algo" }
    
    fn execute(&self, input: &Self::Input) -> Self::Output {
        // Implementation
    }
    
    fn complexity(&self) -> Complexity { Complexity::O(n log n) }
}

// Register in family
let family = AlgorithmFamily::<Vec<i32>, Vec<i32>>::new()
    .with(MyAlgorithm)
    .with(QuickSort);
```

---

## Selection Strategies (Detailed)

### 1. Brute Force Strategy
```rust
// For each call:
// 1. Run ALL algorithms
// 2. Time each
// 3. Return fastest result + cache winner
// Pros: Always finds true winner
// Cons: Nx slower for N algorithms
```

### 2. Pattern-Based Strategy
```rust
// For each call:
// 1. Analyze input data patterns
// 2. Use heuristics to predict best algorithm
// 3. Run only predicted best
// 4. Optionally: verify with short test run
// Pros: Near-optimal speed
// Cons: Heuristics may be wrong
```

### 3. Adaptive Strategy (DEFAULT)
```rust
// For each call:
// 1. Check cache - return if known
// 2. First N times: run all (learning phase)
// 3. After learning: run winner, verify periodically
// 4. If data pattern changes: re-enter learning
// Pros: Fast after learning, adapts to change
// Cons: Initial overhead
```

### 4. Shadow Mode Strategy
```rust
// For each call:
// 1. Run currently cached winner (fast path)
// 2. ALSO run candidates in background
// 3. If candidate faster: swap winner
// 4. Update cache
// Pros: Zero overhead during learning
// Cons: Wastes resources, delayed switching
```

---

## Pattern Detection

### What We Analyze

| Pattern | Detection Method | Affects |
|---------|-----------------|---------|
| Sortedness | Check if sorted, partially sorted | Sort algo choice |
| Randomness | Entropy calculation | Hash function choice |
| Duplicates | Duplicate count/ratio | Sort strategy |
| Size | Input length | Complexity class |
| Distribution | Histogram analysis | Sampling strategy |
| Monotonicity | Direction detection | Search algo choice |
| Reversed | Check if reverse sorted | Special handling |

### Pattern Fingerprint
```rust
struct PatternFingerprint {
    size: usize,
    entropy: f64,
    sortedness: f64,      // 0 = random, 1 = sorted
    duplicate_ratio: f64,
    monotonic_dir: Option<Direction>,
    distribution: Distribution,
}
```

---

## Caching System

### Context Key
```rust
struct ContextKey {
    algorithm_family: FamilyId,
    input_type: TypeId,
    pattern_fingerprint: u64,  // Hash of patterns
    size_class: SizeClass,     // tiny/small/medium/large/huge
}
```

### Size Classes
```rust
enum SizeClass {
    Tiny(0..100),
    Small(100..1_000),
    Medium(1_000..100_000),
    Large(100_000..10_000_000),
    Huge(10_000_000..),
}
```

### Cache Storage
```rust
struct Cache {
    // Fast lookup by exact context
    exact: HashMap<ContextKey, CacheEntry>,
    
    // Pattern-based fallback lookup
    patterns: Trie<ContextKey, CacheEntry>,
    
    // Global statistics per algorithm
    stats: HashMap<FamilyId, AlgorithmStats>,
}
```

---

## Configuration Options

```rust
pub struct Config {
    // Selection
    pub strategy: SelectionStrategy,
    pub warmup_runs: u32,
    pub confidence_threshold: f64,
    
    // Caching
    pub cache_enabled: bool,
    pub cache_persist: bool,
    pub cache_size_limit: usize,
    
    // Profiling
    pub profile_enabled: bool,
    pub profile_sample_rate: f64,
    
    // Shadow mode
    pub shadow_enabled: bool,
    pub shadow_interval: u32,
    
    // Safety
    pub timeout_ms: u64,
    pub max_overhead_percent: u32,
}
```

---

## Performance Considerations

### Timing Overhead
- `std::time::Instant`: ~5-10ns
- `rdtsc` based: ~2-5ns
- Use instant for >1μs ops, rdtsc for fast algos

### Memory Overhead
- Cache: ~100 bytes per context entry
- Profiling: ~1KB per algorithm per family

### Warmup Strategy
```rust
// Don't profile first call (cold cache, JIT warmup)
// Start profiling after 2-3 calls
let should_profile = call_count > 2 && total_runs < config.warmup_runs;
```

---

## Testing Strategy

### Unit Tests
- Each algorithm correctness
- Pattern detection accuracy
- Cache correctness
- Timing accuracy

### Integration Tests
- Full selection flow
- Cache hit/miss handling
- Multiple families
- Concurrent access

### Benchmarks
- Synthetic patterns (sorted, random, etc.)
- Real-world datasets
- Comparison vs single-algorithm

---

## Milestones

### Phase 1: Core (Week 1-2)
- [ ] Project setup (Rust, CI, benchmarking)
- [ ] Algorithm traits and families
- [ ] Basic timing infrastructure
- [ ] Brute force selection
- [ ] Simple caching

### Phase 2: Intelligence (Week 3-4)
- [ ] Pattern detection
- [ ] Pattern-based selection
- [ ] Adaptive strategy
- [ ] Cache persistence (disk)
- [ ] API polish

### Phase 3: Production (Week 5-6)
- [ ] Shadow mode
- [ ] Concurrent safety
- [ ] Performance tuning
- [ ] Documentation
- [ ] Crate release

### Phase 4: Advanced (Week 7+)
- [ ] AI-generated variants
- [ ] More algorithm families
- [ ] WASM support
- [ ] C/Python bindings

---

## Success Metrics

| Metric | Target |
|--------|--------|
| Selection accuracy | >90% matches best algo |
| Overhead (after cache) | <1% vs optimal |
| Cache hit rate | >80% for typical workloads |
| Startup overhead | <10x for first run |
| Memory overhead | <1MB baseline |

---

## Example Scenarios

### Scenario 1: Sorting User IDs
```rust
// Users: 1M IDs, mostly sequential with gaps
let mut ids = generate_user_ids();

let (sorted, info) = algo::select(
    SortFamily::standard(),
    &mut ids,
    Config::default()
);

// Runtime detects:
// - Nearly sorted (sortedness = 0.95)
// - Size = Large
// - Picks insertion sort (adaptive)
// Result: 10x faster than quicksort
```

### Scenario 2: Hash Map Lookups
```rust
// Keys: strings with known distribution
let keys = generate_api_keys();

let (index, info) = algo::select(
    HashFamily::standard(),
    &keys,
    Config::default()
);

// Runtime detects:
// - High cardinality
// - Specific character distribution
// - Picks xxHash
```

### Scenario 3: Variable Data
```rust
// Data pattern changes over time
let mut data = get_initial_data();

for batch in data_batches {
    let (result, info) = algo::select(family, &mut batch, config);
    
    // After initial warmup:
    // - First 3 batches: profiles all
    // - Batch 4+: uses winner for that pattern
    // - New pattern detected: re-profiles
    
    process(result);
}
```

---

## Comparison to Alternatives

| Approach | Pros | Cons |
|----------|------|------|
| Manual optimization | Full control | Time-consuming |
| Compiler auto-vectorization | Zero code change | Limited scope |
| Algorithm libraries | Fast implementations | Static selection |
| **AlgoSwitch** | **Adaptive, runtime** | **Extra overhead** |

---

## Risk Assessment

| Risk | Likelihood | Impact | Mitigation |
|------|------------|--------|-------------|
| Overhead exceeds benefit | Medium | High | Careful benchmarking, optimization |
| Cache thrashing | Low | Medium | Pattern-based key reduction |
| Algorithm bugs | Low | High | Thorough test suite |
| No improvement found | Medium | Medium | Multiple strategy fallback |

---

## Future Extensions

### Phase 2 Ideas
- Template metaprogramming variants
- Machine learning model for selection
- Hardware counter integration (cache misses, etc.)
- Distributed profiling across services

### Phase 3 Ideas
- WASM runtime integration
- Language bindings (Python, JS, C)
- Cloud profiling (learn from all users)
- Auto-generated algorithm variants

---

## References

- "Just-in-Time Data Structures" (Onward! 2015)
- "Self-Designing Software" (CACM 2024)
- "EffiLearner: Self-Optimizing Code Generation" (2024)
- "Adaptive Memory Allocation" (Google Llama)
- REX: Runtime Emergent Software (OSDI 2016)