zipora 3.1.5

High-performance Rust implementation providing advanced data structures and compression algorithms with memory safety guarantees. Features LRU page cache, sophisticated caching layer, fiber-based concurrency, real-time compression, secure memory pools, SIMD optimizations, and complete C FFI for migration from C++.
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/**
 * Enhanced C++ Implementation - Comprehensive Benchmark Functions
 * 
 * This file contains the complete implementation of all enhanced wrapper functions
 * for comprehensive C++ vs Rust performance comparison.
 */

#include "wrapper.hpp"
#include <chrono>
#include <memory>
#include <atomic>
#include <vector>
#include <unordered_map>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <numeric>
#include <cmath>
#include <fstream>
#include <sstream>
#include <thread>

// System-specific headers for hardware detection and cache control
#ifdef __linux__
#include <sys/sysinfo.h>
#include <unistd.h>
#include <cpuid.h>
#endif

#ifdef _WIN32
#include <windows.h>
#include <intrin.h>
#endif

#ifdef __APPLE__
#include <sys/sysctl.h>
#include <sys/types.h>
#endif

// Enhanced memory tracking globals
extern std::atomic<uint64_t> g_memory_usage;
extern std::atomic<uint64_t> g_allocation_count;
extern std::atomic<uint64_t> g_deallocation_count;
extern std::atomic<uint64_t> g_peak_memory_usage;
extern std::atomic<uint64_t> g_total_allocated;
extern std::atomic<uint64_t> g_total_deallocated;

extern "C" {

// ============================================================================
// Enhanced String Operations Implementation
// ============================================================================

void* cpp_fstring_create_from_cstr(const char* cstr) {
    if (!cstr) return nullptr;
    size_t len = strlen(cstr);
    return cpp_fstring_create(reinterpret_cast<const uint8_t*>(cstr), len);
}

int cpp_fstring_compare(void* fstr1, void* fstr2) {
    if (!fstr1 || !fstr2) return fstr1 ? 1 : (fstr2 ? -1 : 0);
    
    auto* s1 = static_cast<fstring*>(fstr1);
    auto* s2 = static_cast<fstring*>(fstr2);
    
#ifdef HAVE_REFERENCE_LIB
    return s1->compare(*s2);
#else
    // Stub implementation
    if (s1->size() != s2->size()) {
        return s1->size() < s2->size() ? -1 : 1;
    }
    return memcmp(s1->ptr(), s2->ptr(), s1->size());
#endif
}

int cpp_fstring_starts_with(void* fstr, const uint8_t* prefix, size_t prefix_len) {
    if (!fstr || !prefix) return 0;
    
    auto* s = static_cast<fstring*>(fstr);
    if (prefix_len > s->size()) return 0;
    
#ifdef HAVE_REFERENCE_LIB
    fstring prefix_str(prefix, prefix_len);
    return s->starts_with(prefix_str) ? 1 : 0;
#else
    return memcmp(s->ptr(), prefix, prefix_len) == 0 ? 1 : 0;
#endif
}

int cpp_fstring_ends_with(void* fstr, const uint8_t* suffix, size_t suffix_len) {
    if (!fstr || !suffix) return 0;
    
    auto* s = static_cast<fstring*>(fstr);
    if (suffix_len > s->size()) return 0;
    
#ifdef HAVE_REFERENCE_LIB
    fstring suffix_str(suffix, suffix_len);
    return s->ends_with(suffix_str) ? 1 : 0;
#else
    size_t start = s->size() - suffix_len;
    return memcmp(s->ptr() + start, suffix, suffix_len) == 0 ? 1 : 0;
#endif
}

void* cpp_fstring_concat(void* fstr1, void* fstr2) {
    if (!fstr1 || !fstr2) return nullptr;
    
    auto* s1 = static_cast<fstring*>(fstr1);
    auto* s2 = static_cast<fstring*>(fstr2);
    
    size_t total_len = s1->size() + s2->size();
    uint8_t* buffer = new uint8_t[total_len];
    
    memcpy(buffer, s1->ptr(), s1->size());
    memcpy(buffer + s1->size(), s2->ptr(), s2->size());
    
    auto* result = new fstring(buffer, total_len);
    delete[] buffer;
    
    return result;
}

void* cpp_fstring_repeat(void* fstr, size_t times) {
    if (!fstr || times == 0) return nullptr;
    
    auto* s = static_cast<fstring*>(fstr);
    size_t total_len = s->size() * times;
    uint8_t* buffer = new uint8_t[total_len];
    
    for (size_t i = 0; i < times; ++i) {
        memcpy(buffer + i * s->size(), s->ptr(), s->size());
    }
    
    auto* result = new fstring(buffer, total_len);
    delete[] buffer;
    
    return result;
}

// ============================================================================
// Hash Map Operations Implementation
// ============================================================================

void* cpp_hashmap_create() {
    track_allocation(sizeof(HashMap));
    return new HashMap();
}

void cpp_hashmap_destroy(void* map) {
    if (map) {
        track_deallocation(sizeof(HashMap));
        delete static_cast<HashMap*>(map);
    }
}

int cpp_hashmap_insert(void* map, const char* key, int32_t value) {
    if (!map || !key) return 0;
    
    auto* m = static_cast<HashMap*>(map);
    return m->insert(std::string(key), value) ? 1 : 0;
}

int cpp_hashmap_get(void* map, const char* key, int32_t* value) {
    if (!map || !key || !value) return 0;
    
    auto* m = static_cast<HashMap*>(map);
    return m->get(std::string(key), *value) ? 1 : 0;
}

int cpp_hashmap_remove(void* map, const char* key) {
    if (!map || !key) return 0;
    
    auto* m = static_cast<HashMap*>(map);
    return m->remove(std::string(key)) ? 1 : 0;
}

size_t cpp_hashmap_size(void* map) {
    if (!map) return 0;
    return static_cast<HashMap*>(map)->size();
}

void cpp_hashmap_clear(void* map) {
    if (map) {
        static_cast<HashMap*>(map)->clear();
    }
}

int cpp_hashmap_insert_batch(void* map, const char** keys, const int32_t* values, size_t count) {
    if (!map || !keys || !values) return 0;
    
    auto* m = static_cast<HashMap*>(map);
    for (size_t i = 0; i < count; ++i) {
        if (!m->insert(std::string(keys[i]), values[i])) {
            return 0;
        }
    }
    return 1;
}

int cpp_hashmap_get_batch(void* map, const char** keys, int32_t* values, size_t count) {
    if (!map || !keys || !values) return 0;
    
    auto* m = static_cast<HashMap*>(map);
    for (size_t i = 0; i < count; ++i) {
        if (!m->get(std::string(keys[i]), values[i])) {
            return 0;
        }
    }
    return 1;
}

// ============================================================================
// Bit Vector Operations Implementation
// ============================================================================

void* cpp_bitvector_create() {
    track_allocation(sizeof(BitVector));
    return new BitVector();
}

void cpp_bitvector_destroy(void* bv) {
    if (bv) {
        track_deallocation(sizeof(BitVector));
        delete static_cast<BitVector*>(bv);
    }
}

void cpp_bitvector_push(void* bv, int bit) {
    if (bv) {
        static_cast<BitVector*>(bv)->push_back(bit != 0);
    }
}

int cpp_bitvector_get(void* bv, size_t pos) {
    if (!bv) return 0;
    
    auto* b = static_cast<BitVector*>(bv);
    return (pos < b->size() && (*b)[pos]) ? 1 : 0;
}

size_t cpp_bitvector_size(void* bv) {
    return bv ? static_cast<BitVector*>(bv)->size() : 0;
}

void cpp_bitvector_push_batch(void* bv, const int* bits, size_t count) {
    if (!bv || !bits) return;
    
    auto* b = static_cast<BitVector*>(bv);
    for (size_t i = 0; i < count; ++i) {
        b->push_back(bits[i] != 0);
    }
}

// ============================================================================
// Memory Management Implementation
// ============================================================================

uint64_t cpp_get_deallocation_count() {
    return g_deallocation_count.load();
}

uint64_t cpp_get_peak_memory_usage() {
    return g_peak_memory_usage.load();
}

void cpp_get_memory_stats(CppMemoryStats* stats) {
    if (!stats) return;
    
    stats->total_allocated = g_total_allocated.load();
    stats->total_deallocated = g_total_deallocated.load();
    stats->current_usage = g_memory_usage.load();
    stats->peak_usage = g_peak_memory_usage.load();
    stats->allocation_count = g_allocation_count.load();
    stats->deallocation_count = g_deallocation_count.load();
    
    if (stats->allocation_count > 0) {
        stats->average_allocation_size = static_cast<double>(stats->total_allocated) / stats->allocation_count;
    } else {
        stats->average_allocation_size = 0.0;
    }
    
    // Simple fragmentation estimation
    if (stats->total_allocated > 0) {
        stats->fragmentation_ratio = static_cast<double>(stats->current_usage) / stats->total_allocated;
    } else {
        stats->fragmentation_ratio = 0.0;
    }
}

void* cpp_memory_pool_create(size_t block_size, size_t initial_blocks) {
    track_allocation(sizeof(MemoryPool) + block_size * initial_blocks);
    return new MemoryPool(block_size, initial_blocks);
}

void cpp_memory_pool_destroy(void* pool) {
    if (pool) {
        auto* p = static_cast<MemoryPool*>(pool);
        track_deallocation(sizeof(MemoryPool) + p->block_size * (p->allocated_count() + p->free_count()));
        delete p;
    }
}

void* cpp_memory_pool_alloc(void* pool) {
    return pool ? static_cast<MemoryPool*>(pool)->alloc() : nullptr;
}

void cpp_memory_pool_free(void* pool, void* ptr) {
    if (pool) {
        static_cast<MemoryPool*>(pool)->free(ptr);
    }
}

size_t cpp_memory_pool_allocated_blocks(void* pool) {
    return pool ? static_cast<MemoryPool*>(pool)->allocated_count() : 0;
}

size_t cpp_memory_pool_free_blocks(void* pool) {
    return pool ? static_cast<MemoryPool*>(pool)->free_count() : 0;
}

// ============================================================================
// Advanced Performance Measurement Implementation
// ============================================================================

void cpp_flush_caches() {
#ifdef __linux__
    // Flush CPU caches by reading/writing large amount of data
    const size_t cache_size = 32 * 1024 * 1024; // 32MB
    volatile char* data = new char[cache_size];
    
    for (size_t i = 0; i < cache_size; ++i) {
        data[i] = static_cast<char>(i);
    }
    
    // Read back to ensure cache pollution
    volatile char sum = 0;
    for (size_t i = 0; i < cache_size; ++i) {
        sum += data[i];
    }
    
    delete[] data;
    (void)sum; // Prevent optimization
#endif
}

double cpp_measure_cache_miss_rate(void* data, size_t size, size_t iterations) {
    if (!data) return 0.0;
    
    volatile char* ptr = static_cast<volatile char*>(data);
    auto start = std::chrono::high_resolution_clock::now();
    
    // Random access pattern to induce cache misses
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<size_t> dis(0, size - 1);
    
    volatile char sum = 0;
    for (size_t i = 0; i < iterations; ++i) {
        sum += ptr[dis(gen)];
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration<double, std::micro>(end - start);
    
    (void)sum; // Prevent optimization
    return duration.count();
}

double cpp_measure_memory_bandwidth(size_t size, size_t iterations) {
    auto* data = new volatile char[size];
    
    auto start = std::chrono::high_resolution_clock::now();
    
    for (size_t iter = 0; iter < iterations; ++iter) {
        // Sequential write
        for (size_t i = 0; i < size; ++i) {
            data[i] = static_cast<char>(i + iter);
        }
        
        // Sequential read
        volatile char sum = 0;
        for (size_t i = 0; i < size; ++i) {
            sum += data[i];
        }
        (void)sum;
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration<double>(end - start);
    
    delete[] data;
    
    // Return bandwidth in MB/s
    double bytes_processed = static_cast<double>(size * iterations * 2); // read + write
    return (bytes_processed / (1024 * 1024)) / duration.count();
}

double cpp_measure_deallocation_speed(void** ptrs, size_t count) {
    if (!ptrs) return 0.0;
    
    auto start = std::chrono::high_resolution_clock::now();
    
    for (size_t i = 0; i < count; ++i) {
        if (ptrs[i]) {
            free(ptrs[i]);
        }
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration<double, std::micro>(end - start);
    
    return duration.count();
}

double cpp_measure_reallocation_speed(size_t initial_size, size_t final_size, size_t iterations) {
    auto start = std::chrono::high_resolution_clock::now();
    
    for (size_t i = 0; i < iterations; ++i) {
        void* ptr = malloc(initial_size);
        ptr = realloc(ptr, final_size);
        free(ptr);
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration<double, std::micro>(end - start);
    
    return duration.count();
}

// ============================================================================
// String Performance Measurement Implementation
// ============================================================================

double cpp_measure_string_find_speed(const uint8_t* text, size_t text_len, 
                                     const uint8_t* pattern, size_t pattern_len, 
                                     size_t iterations) {
    if (!text || !pattern) return 0.0;
    
    auto start = std::chrono::high_resolution_clock::now();
    
    for (size_t i = 0; i < iterations; ++i) {
        void* fstr = cpp_fstring_create(text, text_len);
        int64_t pos = cpp_fstring_find(fstr, pattern, pattern_len);
        cpp_fstring_destroy(fstr);
        (void)pos; // Prevent optimization
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration<double, std::micro>(end - start);
    
    return duration.count();
}

double cpp_measure_string_compare_speed(const uint8_t* str1, size_t len1,
                                        const uint8_t* str2, size_t len2,
                                        size_t iterations) {
    if (!str1 || !str2) return 0.0;
    
    void* fstr1 = cpp_fstring_create(str1, len1);
    void* fstr2 = cpp_fstring_create(str2, len2);
    
    auto start = std::chrono::high_resolution_clock::now();
    
    for (size_t i = 0; i < iterations; ++i) {
        int result = cpp_fstring_compare(fstr1, fstr2);
        (void)result; // Prevent optimization
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration<double, std::micro>(end - start);
    
    cpp_fstring_destroy(fstr1);
    cpp_fstring_destroy(fstr2);
    
    return duration.count();
}

// ============================================================================
// Vector Performance Measurement Implementation
// ============================================================================

double cpp_measure_vector_push_speed(size_t count, size_t iterations) {
    auto start = std::chrono::high_resolution_clock::now();
    
    for (size_t iter = 0; iter < iterations; ++iter) {
        void* vec = cpp_valvec_create();
        for (size_t i = 0; i < count; ++i) {
            cpp_valvec_push(vec, static_cast<int32_t>(i));
        }
        cpp_valvec_destroy(vec);
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration<double, std::micro>(end - start);
    
    return duration.count();
}

double cpp_measure_vector_access_speed(void* vec, size_t size, size_t iterations) {
    if (!vec) return 0.0;
    
    auto start = std::chrono::high_resolution_clock::now();
    
    for (size_t iter = 0; iter < iterations; ++iter) {
        int64_t sum = 0;
        for (size_t i = 0; i < size; ++i) {
            sum += cpp_valvec_get(vec, i);
        }
        (void)sum; // Prevent optimization
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration<double, std::micro>(end - start);
    
    return duration.count();
}

double cpp_measure_vector_iteration_speed(void* vec, size_t iterations) {
    if (!vec) return 0.0;
    
    size_t size = cpp_valvec_size(vec);
    auto start = std::chrono::high_resolution_clock::now();
    
    for (size_t iter = 0; iter < iterations; ++iter) {
        int64_t sum = 0;
        for (size_t i = 0; i < size; ++i) {
            sum += cpp_valvec_get(vec, i);
        }
        (void)sum; // Prevent optimization
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration<double, std::micro>(end - start);
    
    return duration.count();
}

// ============================================================================
// Hash Map Performance Measurement Implementation
// ============================================================================

double cpp_measure_hashmap_insert_speed(size_t count, size_t iterations) {
    auto start = std::chrono::high_resolution_clock::now();
    
    for (size_t iter = 0; iter < iterations; ++iter) {
        void* map = cpp_hashmap_create();
        for (size_t i = 0; i < count; ++i) {
            std::string key = "key_" + std::to_string(i);
            cpp_hashmap_insert(map, key.c_str(), static_cast<int32_t>(i));
        }
        cpp_hashmap_destroy(map);
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration<double, std::micro>(end - start);
    
    return duration.count();
}

double cpp_measure_hashmap_lookup_speed(void* map, const char** keys, size_t count, size_t iterations) {
    if (!map || !keys) return 0.0;
    
    auto start = std::chrono::high_resolution_clock::now();
    
    for (size_t iter = 0; iter < iterations; ++iter) {
        for (size_t i = 0; i < count; ++i) {
            int32_t value;
            int result = cpp_hashmap_get(map, keys[i], &value);
            (void)result; (void)value; // Prevent optimization
        }
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration<double, std::micro>(end - start);
    
    return duration.count();
}

// ============================================================================
// System Information Implementation
// ============================================================================

void cpp_get_system_info(CppSystemInfo* info) {
    if (!info) return;
    
    memset(info, 0, sizeof(CppSystemInfo));
    
#ifdef __linux__
    info->physical_memory = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE);
    info->page_size = sysconf(_SC_PAGE_SIZE);
    info->cpu_cores = sysconf(_SC_NPROCESSORS_ONLN);
    info->logical_cores = std::thread::hardware_concurrency();
    
    // Try to read cache info from /proc/cpuinfo
    std::ifstream cpuinfo("/proc/cpuinfo");
    std::string line;
    while (std::getline(cpuinfo, line)) {
        if (line.find("cache size") != std::string::npos) {
            // Parse cache size
            size_t pos = line.find(":");
            if (pos != std::string::npos) {
                std::string cache_str = line.substr(pos + 1);
                std::stringstream ss(cache_str);
                size_t cache_kb;
                if (ss >> cache_kb) {
                    info->l3_cache_size = cache_kb * 1024; // Convert to bytes
                }
            }
        }
        if (line.find("model name") != std::string::npos) {
            size_t pos = line.find(":");
            if (pos != std::string::npos) {
                std::string model = line.substr(pos + 1);
                strncpy(info->cpu_model, model.c_str(), sizeof(info->cpu_model) - 1);
            }
        }
    }
    
    // Set typical cache sizes if not detected
    if (info->l1_cache_size == 0) info->l1_cache_size = 32 * 1024;
    if (info->l2_cache_size == 0) info->l2_cache_size = 256 * 1024;
    if (info->l3_cache_size == 0) info->l3_cache_size = 8 * 1024 * 1024;
    if (info->cache_line_size == 0) info->cache_line_size = 64;
    
#elif defined(_WIN32)
    SYSTEM_INFO sysinfo;
    GetSystemInfo(&sysinfo);
    info->cpu_cores = sysinfo.dwNumberOfProcessors;
    info->logical_cores = std::thread::hardware_concurrency();
    info->page_size = sysinfo.dwPageSize;
    
    MEMORYSTATUSEX meminfo;
    meminfo.dwLength = sizeof(MEMORYSTATUSEX);
    GlobalMemoryStatusEx(&meminfo);
    info->physical_memory = meminfo.ullTotalPhys;
    
#elif defined(__APPLE__)
    size_t size = sizeof(info->physical_memory);
    sysctlbyname("hw.memsize", &info->physical_memory, &size, NULL, 0);
    
    size = sizeof(info->cpu_cores);
    sysctlbyname("hw.physicalcpu", &info->cpu_cores, &size, NULL, 0);
    
    size = sizeof(info->logical_cores);
    sysctlbyname("hw.logicalcpu", &info->logical_cores, &size, NULL, 0);
    
#endif
    
    strcpy(info->cpu_vendor, "Unknown");
}

int cpp_has_avx2() {
#ifdef __linux__
    uint32_t eax, ebx, ecx, edx;
    if (__get_cpuid_max(0, NULL) >= 7) {
        __cpuid_count(7, 0, eax, ebx, ecx, edx);
        return (ebx & (1 << 5)) ? 1 : 0; // AVX2 bit
    }
#endif
    return 0;
}

int cpp_has_sse42() {
#ifdef __linux__
    uint32_t eax, ebx, ecx, edx;
    if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
        return (ecx & (1 << 20)) ? 1 : 0; // SSE4.2 bit
    }
#endif
    return 0;
}

int cpp_has_bmi2() {
#ifdef __linux__
    uint32_t eax, ebx, ecx, edx;
    if (__get_cpuid_max(0, NULL) >= 7) {
        __cpuid_count(7, 0, eax, ebx, ecx, edx);
        return (ebx & (1 << 8)) ? 1 : 0; // BMI2 bit
    }
#endif
    return 0;
}

// ============================================================================
// Advanced Timing and Statistics Implementation
// ============================================================================

void cpp_timer_start(CppTimer* timer) {
    if (!timer) return;
    
    timer->start_time = std::chrono::duration<double>(
        std::chrono::high_resolution_clock::now().time_since_epoch()
    ).count();
    
#ifdef __x86_64__
    asm volatile("rdtsc" : "=a"(timer->start_cycles), "=d"(timer->end_cycles));
    timer->start_cycles |= (static_cast<uint64_t>(timer->end_cycles) << 32);
#endif
}

void cpp_timer_stop(CppTimer* timer) {
    if (!timer) return;
    
    timer->end_time = std::chrono::duration<double>(
        std::chrono::high_resolution_clock::now().time_since_epoch()
    ).count();
    
#ifdef __x86_64__
    uint32_t low, high;
    asm volatile("rdtsc" : "=a"(low), "=d"(high));
    timer->end_cycles = static_cast<uint64_t>(low) | (static_cast<uint64_t>(high) << 32);
#endif
}

double cpp_timer_elapsed_seconds(const CppTimer* timer) {
    if (!timer) return 0.0;
    return timer->end_time - timer->start_time;
}

uint64_t cpp_timer_elapsed_cycles(const CppTimer* timer) {
    if (!timer) return 0;
    return timer->end_cycles - timer->start_cycles;
}

// ============================================================================
// Memory Access Pattern Analysis Implementation
// ============================================================================

double cpp_measure_sequential_access_speed(void* data, size_t size, size_t iterations) {
    if (!data) return 0.0;
    
    volatile char* ptr = static_cast<volatile char*>(data);
    auto start = std::chrono::high_resolution_clock::now();
    
    for (size_t iter = 0; iter < iterations; ++iter) {
        volatile char sum = 0;
        for (size_t i = 0; i < size; ++i) {
            sum += ptr[i];
        }
        (void)sum; // Prevent optimization
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration<double, std::micro>(end - start);
    
    return duration.count();
}

double cpp_measure_random_access_speed(void* data, size_t size, size_t iterations) {
    if (!data) return 0.0;
    
    volatile char* ptr = static_cast<volatile char*>(data);
    
    // Generate random indices
    std::vector<size_t> indices(iterations);
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<size_t> dis(0, size - 1);
    
    for (size_t i = 0; i < iterations; ++i) {
        indices[i] = dis(gen);
    }
    
    auto start = std::chrono::high_resolution_clock::now();
    
    volatile char sum = 0;
    for (size_t i = 0; i < iterations; ++i) {
        sum += ptr[indices[i]];
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration<double, std::micro>(end - start);
    
    (void)sum; // Prevent optimization
    return duration.count();
}

double cpp_measure_strided_access_speed(void* data, size_t size, size_t stride, size_t iterations) {
    if (!data) return 0.0;
    
    volatile char* ptr = static_cast<volatile char*>(data);
    auto start = std::chrono::high_resolution_clock::now();
    
    for (size_t iter = 0; iter < iterations; ++iter) {
        volatile char sum = 0;
        for (size_t i = 0; i < size; i += stride) {
            sum += ptr[i];
        }
        (void)sum; // Prevent optimization
    }
    
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration<double, std::micro>(end - start);
    
    return duration.count();
}

// ============================================================================
// Comprehensive Benchmark Suite Implementation
// ============================================================================

void cpp_run_comprehensive_benchmark(CppPerformanceSummary* summary) {
    if (!summary) return;
    
    memset(summary, 0, sizeof(CppPerformanceSummary));
    
    // Vector performance
    summary->vector_push_throughput = 1.0 / cpp_measure_vector_push_speed(10000, 100);
    
    void* test_vec = cpp_valvec_create();
    for (int i = 0; i < 10000; ++i) {
        cpp_valvec_push(test_vec, i);
    }
    summary->vector_access_throughput = 1.0 / cpp_measure_vector_access_speed(test_vec, 10000, 100);
    cpp_valvec_destroy(test_vec);
    
    // String performance
    const char* test_string = "The quick brown fox jumps over the lazy dog";
    summary->string_hash_throughput = 1.0 / cpp_measure_hash_speed(
        reinterpret_cast<const uint8_t*>(test_string), strlen(test_string), 1000
    );
    
    const char* pattern = "fox";
    summary->string_find_throughput = 1.0 / cpp_measure_string_find_speed(
        reinterpret_cast<const uint8_t*>(test_string), strlen(test_string),
        reinterpret_cast<const uint8_t*>(pattern), strlen(pattern), 1000
    );
    
    // Hash map performance
    summary->hashmap_insert_throughput = 1.0 / cpp_measure_hashmap_insert_speed(1000, 10);
    
    // Memory system performance
    summary->allocation_throughput = 1.0 / cpp_measure_allocation_speed(1000, 64);
    summary->memory_bandwidth = cpp_measure_memory_bandwidth(1024 * 1024, 10);
    
    // Cache efficiency (inverse of cache miss time)
    void* cache_data = malloc(1024 * 1024);
    summary->cache_efficiency = 1.0 / cpp_measure_cache_miss_rate(cache_data, 1024 * 1024, 10000);
    free(cache_data);
    
    // Calculate overall score as geometric mean
    double scores[] = {
        summary->vector_push_throughput,
        summary->vector_access_throughput,
        summary->string_hash_throughput,
        summary->string_find_throughput,
        summary->hashmap_insert_throughput,
        summary->allocation_throughput,
        summary->memory_bandwidth,
        summary->cache_efficiency
    };
    
    double product = 1.0;
    for (double score : scores) {
        if (score > 0) product *= score;
    }
    
    summary->overall_score = std::pow(product, 1.0 / (sizeof(scores) / sizeof(scores[0])));
}

void cpp_compare_with_baseline(const CppPerformanceSummary* current, 
                               const CppPerformanceSummary* baseline,
                               double* improvement_factors) {
    if (!current || !baseline || !improvement_factors) return;
    
    improvement_factors[0] = current->vector_push_throughput / baseline->vector_push_throughput;
    improvement_factors[1] = current->vector_access_throughput / baseline->vector_access_throughput;
    improvement_factors[2] = current->string_hash_throughput / baseline->string_hash_throughput;
    improvement_factors[3] = current->string_find_throughput / baseline->string_find_throughput;
    improvement_factors[4] = current->hashmap_insert_throughput / baseline->hashmap_insert_throughput;
    improvement_factors[5] = current->allocation_throughput / baseline->allocation_throughput;
    improvement_factors[6] = current->memory_bandwidth / baseline->memory_bandwidth;
    improvement_factors[7] = current->cache_efficiency / baseline->cache_efficiency;
    improvement_factors[8] = current->overall_score / baseline->overall_score;
}

} // extern "C"