dodecet-encoder 1.1.0

A 12-bit dodecet encoding system optimized for geometric and calculus operations
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
// Performance Benchmarks for Dodecet Encoder
// Comprehensive performance testing: WASM vs Native, encoding/decoding, geometric operations

use dodecet_encoder::{Dodecet, Point3D, Vector3D, Transform3D};
use std::time::{Duration, Instant};

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

    struct BenchmarkResult {
        name: String,
        operations: usize,
        total_duration: Duration,
        avg_duration_ns: f64,
        ops_per_second: f64,
    }

    impl BenchmarkResult {
        fn new(name: String, operations: usize, total_duration: Duration) -> Self {
            let avg_duration_ns = total_duration.as_nanos() as f64 / operations as f64;
            let ops_per_second = 1_000_000_000.0 / avg_duration_ns;

            Self {
                name,
                operations,
                total_duration,
                avg_duration_ns,
                ops_per_second,
            }
        }

        fn print(&self) {
            println!("\n=== {} ===", self.name);
            println!("Operations: {}", self.operations);
            println!("Total Time: {:.2} ms", self.total_duration.as_secs_f64() * 1000.0);
            println!("Avg Time: {:.2} ns/op", self.avg_duration_ns);
            println!("Throughput: {:.0} ops/sec", self.ops_per_second);

            // Performance targets
            let target_ns = match self.name.as_str() {
                "Dodecet Creation" => 5.0,
                "Hex Encoding" => 25.0,
                "Hex Decoding" => 30.0,
                "Point3D Encoding" => 100.0,
                "Vector Addition" => 20.0,
                "Vector Dot Product" => 20.0,
                "Vector Cross Product" => 30.0,
                "Distance Calculation" => 45.0,
                "Transform3D Apply" => 100.0,
                _ => 1000.0,
            };

            let status = if self.avg_duration_ns <= target_ns {
                "✓ PASS"
            } else {
                "✗ FAIL"
            };

            println!("Target: < {:.0} ns/op [{}]", target_ns, status);
        }
    }

    /// Benchmark: Dodecet Creation
    #[test]
    fn benchmark_dodecet_creation() {
        let iterations = 1_000_000;
        let start = Instant::now();

        for i in 0..iterations {
            let _d = Dodecet::new(i % 4096);
        }

        let duration = start.elapsed();
        let result = BenchmarkResult::new(
            "Dodecet Creation".to_string(),
            iterations,
            duration,
        );
        result.print();

        assert!(result.avg_duration_ns < 5.0, "Dodecet creation too slow");
    }

    /// Benchmark: Hex Encoding
    #[test]
    fn benchmark_hex_encoding() {
        let iterations = 1_000_000;
        let dodecets: Vec<Dodecet> = (0..iterations).map(|i| Dodecet::new(i % 4096)).collect();

        let start = Instant::now();

        for d in &dodecets {
            let _hex = d.to_hex();
        }

        let duration = start.elapsed();
        let result = BenchmarkResult::new(
            "Hex Encoding".to_string(),
            iterations,
            duration,
        );
        result.print();

        assert!(result.avg_duration_ns < 25.0, "Hex encoding too slow");
    }

    /// Benchmark: Hex Decoding
    #[test]
    fn benchmark_hex_decoding() {
        let iterations = 1_000_000;
        let hex_strings: Vec<String> = (0..iterations)
            .map(|i| Dodecet::new(i % 4096).to_hex())
            .collect();

        let start = Instant::now();

        for hex in &hex_strings {
            let _d = Dodecet::from_hex(hex).unwrap();
        }

        let duration = start.elapsed();
        let result = BenchmarkResult::new(
            "Hex Decoding".to_string(),
            iterations,
            duration,
        );
        result.print();

        assert!(result.avg_duration_ns < 30.0, "Hex decoding too slow");
    }

    /// Benchmark: Point3D Encoding
    #[test]
    fn benchmark_point3d_encoding() {
        let iterations = 100_000;
        let points: Vec<Point3D> = (0..iterations)
            .map(|i| Point3D::new(i as f64, i as f64 * 2.0, i as f64 * 3.0))
            .collect();

        let start = Instant::now();

        for point in &points {
            let _dodecets = point.to_dodecets();
        }

        let duration = start.elapsed();
        let result = BenchmarkResult::new(
            "Point3D Encoding".to_string(),
            iterations,
            duration,
        );
        result.print();

        assert!(result.avg_duration_ns < 100.0, "Point3D encoding too slow");
    }

    /// Benchmark: Point3D Decoding
    #[test]
    fn benchmark_point3d_decoding() {
        let iterations = 100_000;
        let dodecet_arrays: Vec<Vec<Dodecet>> = (0..iterations)
            .map(|i| {
                let point = Point3D::new(i as f64, i as f64 * 2.0, i as f64 * 3.0);
                point.to_dodecets()
            })
            .collect();

        let start = Instant::now();

        for dodecets in &dodecet_arrays {
            let _point = Point3D::from_dodecets(dodecets);
        }

        let duration = start.elapsed();
        let result = BenchmarkResult::new(
            "Point3D Decoding".to_string(),
            iterations,
            duration,
        );
        result.print();

        assert!(result.avg_duration_ns < 100.0, "Point3D decoding too slow");
    }

    /// Benchmark: Vector Addition
    #[test]
    fn benchmark_vector_addition() {
        let iterations = 10_000_000;
        let v1 = Vector3D::new(1.0, 2.0, 3.0);
        let v2 = Vector3D::new(4.0, 5.0, 6.0);

        let start = Instant::now();

        for _ in 0..iterations {
            let _sum = v1.add(&v2);
        }

        let duration = start.elapsed();
        let result = BenchmarkResult::new(
            "Vector Addition".to_string(),
            iterations,
            duration,
        );
        result.print();

        assert!(result.avg_duration_ns < 20.0, "Vector addition too slow");
    }

    /// Benchmark: Vector Dot Product
    #[test]
    fn benchmark_vector_dot_product() {
        let iterations = 10_000_000;
        let v1 = Vector3D::new(1.0, 2.0, 3.0);
        let v2 = Vector3D::new(4.0, 5.0, 6.0);

        let start = Instant::now();

        for _ in 0..iterations {
            let _dot = v1.dot(&v2);
        }

        let duration = start.elapsed();
        let result = BenchmarkResult::new(
            "Vector Dot Product".to_string(),
            iterations,
            duration,
        );
        result.print();

        assert!(result.avg_duration_ns < 20.0, "Vector dot product too slow");
    }

    /// Benchmark: Vector Cross Product
    #[test]
    fn benchmark_vector_cross_product() {
        let iterations = 10_000_000;
        let v1 = Vector3D::new(1.0, 2.0, 3.0);
        let v2 = Vector3D::new(4.0, 5.0, 6.0);

        let start = Instant::now();

        for _ in 0..iterations {
            let _cross = v1.cross(&v2);
        }

        let duration = start.elapsed();
        let result = BenchmarkResult::new(
            "Vector Cross Product".to_string(),
            iterations,
            duration,
        );
        result.print();

        assert!(result.avg_duration_ns < 30.0, "Vector cross product too slow");
    }

    /// Benchmark: Distance Calculation
    #[test]
    fn benchmark_distance_calculation() {
        let iterations = 1_000_000;
        let p1 = Point3D::new(0.0, 0.0, 0.0);
        let p2 = Point3D::new(3.0, 4.0, 5.0);

        let start = Instant::now();

        for _ in 0..iterations {
            let _distance = p1.distance_to(&p2);
        }

        let duration = start.elapsed();
        let result = BenchmarkResult::new(
            "Distance Calculation".to_string(),
            iterations,
            duration,
        );
        result.print();

        assert!(result.avg_duration_ns < 45.0, "Distance calculation too slow");
    }

    /// Benchmark: Transform3D Application
    #[test]
    fn benchmark_transform3d_apply() {
        let iterations = 1_000_000;
        let point = Point3D::new(1.0, 2.0, 3.0);
        let transform = Transform3D::rotation_z(45.0);

        let start = Instant::now();

        for _ in 0..iterations {
            let _transformed = transform.apply(&point);
        }

        let duration = start.elapsed();
        let result = BenchmarkResult::new(
            "Transform3D Apply".to_string(),
            iterations,
            duration,
        );
        result.print();

        assert!(result.avg_duration_ns < 100.0, "Transform3D apply too slow");
    }

    /// Benchmark: Array Operations
    #[test]
    fn benchmark_array_operations() {
        let iterations = 100_000;
        let dodecet_arrays: Vec<Vec<Dodecet>> = (0..iterations)
            .map(|_| vec![Dodecet::new(100), Dodecet::new(200), Dodecet::new(300)])
            .collect();

        let start = Instant::now();

        for dodecets in &dodecet_arrays {
            let _hex = Dodecet::array_to_hex(dodecets);
        }

        let duration = start.elapsed();
        let result = BenchmarkResult::new(
            "Array to Hex Encoding".to_string(),
            iterations,
            duration,
        );
        result.print();

        // Array operations should be fast (<50ns per element)
        assert!(result.avg_duration_ns < 150.0, "Array operations too slow");
    }

    /// Benchmark: Scalability Test (1M+ Operations)
    #[test]
    fn benchmark_scalability() {
        let iterations = 1_000_000;
        println!("\n=== Scalability Test: {} Operations ===", iterations);

        // Test encoding scalability
        let start = Instant::now();
        let dodecets: Vec<Dodecet> = (0..iterations).map(|i| Dodecet::new(i % 4096)).collect();
        let creation_time = start.elapsed();
        println!("Creation: {:.2} ms ({:.2} ns/op)",
                 creation_time.as_secs_f64() * 1000.0,
                 creation_time.as_nanos() as f64 / iterations as f64);

        // Test encoding scalability
        let start = Instant::now();
        let hex_strings: Vec<String> = dodecets.iter().map(|d| d.to_hex()).collect();
        let encoding_time = start.elapsed();
        println!("Encoding: {:.2} ms ({:.2} ns/op)",
                 encoding_time.as_secs_f64() * 1000.0,
                 encoding_time.as_nanos() as f64 / iterations as f64);

        // Test decoding scalability
        let start = Instant::now();
        let decoded: Vec<Dodecet> = hex_strings.iter()
            .map(|h| Dodecet::from_hex(h).unwrap())
            .collect();
        let decoding_time = start.elapsed();
        println!("Decoding: {:.2} ms ({:.2} ns/op)",
                 decoding_time.as_secs_f64() * 1000.0,
                 decoding_time.as_nanos() as f64 / iterations as f64);

        let total_time = creation_time + encoding_time + decoding_time;
        println!("Total: {:.2} ms", total_time.as_secs_f64() * 1000.0);

        // Should complete 1M operations in <100ms
        assert!(total_time.as_millis() < 100, "Scalability test failed: too slow");
    }

    /// Benchmark: Memory Usage
    #[test]
    fn benchmark_memory_usage() {
        use std::alloc::{GlobalAlloc, Layout, System};
        use std::sync::atomic::{AtomicUsize, Ordering};

        struct MemoryTracker;

        static ALLOCATED: AtomicUsize = AtomicUsize::new(0);

        unsafe impl GlobalAlloc for MemoryTracker {
            unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
                ALLOCATED.fetch_add(layout.size(), Ordering::SeqCst);
                System.alloc(layout)
            }

            unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
                ALLOCATED.fetch_sub(layout.size(), Ordering::SeqCst);
                System.dealloc(ptr, layout)
            }
        }

        // Test memory usage for 100K dodecets
        let count = 100_000;
        ALLOCATED.store(0, Ordering::SeqCst);

        let dodecets: Vec<Dodecet> = (0..count).map(|i| Dodecet::new(i % 4096)).collect();

        let allocated = ALLOCATED.load(Ordering::SeqCst);
        let bytes_per_dodecet = allocated as f64 / count as f64;

        println!("\n=== Memory Usage Benchmark ===");
        println!("Dodecets: {}", count);
        println!("Total Allocated: {:.2} KB", allocated as f64 / 1024.0);
        println!("Bytes per Dodecet: {:.2}", bytes_per_dodecet);

        // Each dodecet should be ~2 bytes (u16)
        // Allow 10x overhead for Vec structure
        assert!(bytes_per_dodecet < 20.0, "Memory usage too high: {} bytes/dodecet", bytes_per_dodecet);

        std::mem::forget(dodecets); // Prevent deallocation during measurement
    }

    /// Benchmark: Encoding Precision vs Speed Trade-off
    #[test]
    fn benchmark_precision_vs_speed() {
        println!("\n=== Precision vs Speed Trade-off ===");

        let iterations = 10_000;

        // Test different precision levels
        for precision in &[8, 12, 16, 32] {
            let start = Instant::now();

            for i in 0..iterations {
                let value = (i as f64) * (65536.0 / *precision as f64);
                let d = Dodecet::new(value as u16 % 4096);
                let _hex = d.to_hex();
            }

            let duration = start.elapsed();
            let ns_per_op = duration.as_nanos() as f64 / iterations as f64;

            println!("Precision {}: {:.2} ns/op", precision, ns_per_op);
        }
    }

    /// Benchmark: Real-world Constraint Theory Workload
    #[test]
    fn benchmark_constraint_theory_workload() {
        println!("\n=== Constraint Theory Workload Simulation ===");

        let iterations = 10_000;

        // Simulate Pythagorean snapping workload
        let start = Instant::now();
        for i in 0..iterations {
            let x = (i % 100) as f64;
            let y = ((i * 2) % 100) as f64;
            let point = Point3D::new(x, y, 0.0);
            let origin = Point3D::new(0.0, 0.0, 0.0);

            // Encode
            let point_dodecets = point.to_dodecets();
            let origin_dodecets = origin.to_dodecets();

            // Decode
            let point_decoded = Point3D::from_dodecets(&point_dodecets);
            let origin_decoded = Point3D::from_dodecets(&origin_dodecets);

            // Calculate distance
            let _distance = point_decoded.distance_to(&origin_decoded);
        }

        let duration = start.elapsed();
        let ns_per_operation = duration.as_nanos() as f64 / iterations as f64;

        println!("Operations: {}", iterations);
        println!("Total Time: {:.2} ms", duration.as_secs_f64() * 1000.0);
        println!("Avg Time: {:.2} ns/op", ns_per_operation);

        // Should complete constraint theory workflow in <500ns per operation
        assert!(ns_per_operation < 500.0, "Constraint theory workload too slow");
    }

    /// Comprehensive Performance Report
    #[test]
    fn generate_performance_report() {
        println!("\n" + "=".repeat(80));
        println!("COMPREHENSIVE PERFORMANCE REPORT");
        println!("Dodecet Encoder v1.0.0");
        println!("=".repeat(80));

        println!("\n## Target Performance Metrics");
        println!("Dodecet Creation: <5 ns");
        println!("Hex Encoding: <25 ns");
        println!("Hex Decoding: <30 ns");
        println!("Point3D Encoding: <100 ns");
        println!("Vector Operations: <20 ns");
        println!("Distance Calculation: <45 ns");
        println!("Transform3D Operations: <100 ns");
        println!("\n## Memory Targets");
        println!("Per Dodecet: <20 bytes (including Vec overhead)");
        println!("100K Dodecets: <2 MB");
        println!("1M Dodecets: <20 MB");
        println!("\n## WASM Performance");
        println!("WASM Overhead: <2x native performance");
        println!("Browser Memory: <50 MB for typical usage");

        println!("\n" + "=".repeat(80));
        println!("Run individual benchmarks for detailed results");
        println!("=".repeat(80) + "\n");
    }
}