clock-hash 1.0.0

ClockHash-256: Consensus hash function for ClockinChain
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
//! Memory usage and allocation benchmarks for ClockHash-256
//!
//! This module provides benchmarks for understanding memory usage patterns,
//! allocation behavior, cache effects, and memory bandwidth utilization
//! of ClockHash-256 implementations.

use clock_hash::{estimate_memory_usage, ClockHasher, clockhash256};
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use std::hint::black_box;

/// Benchmark memory usage estimation accuracy
///
/// Validates the accuracy of the memory usage estimation functions
/// against actual measured memory usage.
fn bench_memory_usage_estimation(c: &mut Criterion) {
    let mut group = c.benchmark_group("memory_usage_estimation");

    let sizes = [0, 64, 256, 1024, 4096, 16384, 65536, 262144, 1048576];

    for &size in &sizes {
        group.bench_with_input(
            BenchmarkId::from_parameter(format!("{}_bytes", size)),
            &size,
            |b, &size| {
                b.iter(|| {
                    let estimated = estimate_memory_usage(black_box(size));
                    black_box(estimated);
                });
            },
        );
    }

    group.finish();
}

/// Benchmark hasher construction memory overhead
///
/// Measures memory allocation and initialization overhead
/// when creating new ClockHasher instances.
fn bench_hasher_construction_memory(c: &mut Criterion) {
    let mut group = c.benchmark_group("hasher_construction_memory");

    let iterations = [1, 10, 100, 1000];

    for &iter in &iterations {
        group.bench_with_input(
            BenchmarkId::from_parameter(format!("{}_hashers", iter)),
            &iter,
            |b, &iter| {
                b.iter(|| {
                    let mut hashers = Vec::with_capacity(iter);
                    for _ in 0..iter {
                        hashers.push(ClockHasher::new());
                    }
                    black_box(hashers);
                });
            },
        );
    }

    group.finish();
}

/// Benchmark incremental hashing memory patterns
///
/// Tests memory access patterns during incremental hashing
/// to understand cache behavior and memory locality.
fn bench_incremental_hashing_memory(c: &mut Criterion) {
    let mut group = c.benchmark_group("incremental_hashing_memory");

    let chunk_sizes = [64, 256, 1024, 4096];
    let total_size = 65536; // 64KB total

    for &chunk_size in &chunk_sizes {
        let data = vec![0x42u8; total_size];
        let chunks: Vec<&[u8]> = data.chunks(chunk_size).collect();

        group.bench_with_input(
            BenchmarkId::from_parameter(format!("{}_byte_chunks", chunk_size)),
            &chunks,
            |b, chunks| {
                b.iter(|| {
                    let mut hasher = ClockHasher::new();
                    for chunk in chunks.iter() {
                        hasher.update(black_box(chunk));
                    }
                    let hash = hasher.finalize();
                    black_box(hash);
                });
            },
        );
    }

    group.finish();
}

/// Benchmark memory bandwidth utilization
///
/// Measures how effectively ClockHash-256 utilizes memory bandwidth
/// with different data access patterns and sizes.
fn bench_memory_bandwidth(c: &mut Criterion) {
    let mut group = c.benchmark_group("memory_bandwidth");

    let sizes = [4096, 16384, 65536, 262144, 1048576]; // 4KB to 1MB

    for &size in &sizes {
        let data = vec![0x42u8; size];

        group.bench_with_input(
            BenchmarkId::from_parameter(format!("{}_bytes_bandwidth", size)),
            &data,
            |b, data| {
                b.iter(|| {
                    let hash = clockhash256(black_box(data));
                    black_box(hash);
                });
            },
        );
    }

    group.finish();
}

/// Benchmark cache effects on hashing performance
///
/// Tests how different data sizes and access patterns affect
/// cache performance and memory hierarchy utilization.
fn bench_cache_effects(c: &mut Criterion) {
    let mut group = c.benchmark_group("cache_effects");

    // Test different sizes that should hit different cache levels
    let cache_sizes = [
        ("l1_cache", 4096),       // ~L1 cache size
        ("l2_cache", 262144),     // ~L2 cache size
        ("l3_cache", 8388608),    // ~L3 cache size (8MB)
        ("memory", 33554432),     // Larger than typical caches
    ];

    for (cache_level, size) in &cache_sizes {
        let data = vec![0x42u8; *size];

        group.bench_with_input(
            BenchmarkId::from_parameter(format!("{}_{}_bytes", cache_level, size)),
            &data,
            |b, data| {
                b.iter(|| {
                    let hash = clockhash256(black_box(data));
                    black_box(hash);
                });
            },
        );
    }

    group.finish();
}

/// Benchmark memory allocation patterns
///
/// Tests different allocation strategies and their impact on performance.
fn bench_allocation_patterns(c: &mut Criterion) {
    let mut group = c.benchmark_group("allocation_patterns");

    // Single large allocation
    group.bench_function("single_large_allocation", |b| {
        b.iter(|| {
            let data = vec![0x42u8; 1048576]; // 1MB
            let hash = clockhash256(black_box(&data));
            black_box(hash);
        });
    });

    // Multiple small allocations
    group.bench_function("multiple_small_allocations", |b| {
        b.iter(|| {
            let mut hashes = Vec::new();
            for i in 0..16 {
                let data = vec![(i as u8); 65536]; // 64KB each
                let hash = clockhash256(black_box(&data));
                hashes.push(hash);
            }
            black_box(hashes);
        });
    });

    // Incremental allocation (streaming)
    group.bench_function("incremental_allocation", |b| {
        b.iter(|| {
            let mut hasher = ClockHasher::new();
            let chunk_size = 4096;
            let total_size = 1048576; // 1MB

            for i in 0..(total_size / chunk_size) {
                let chunk = vec![(i as u8); chunk_size];
                hasher.update(black_box(&chunk));
            }

            let hash = hasher.finalize();
            black_box(hash);
        });
    });

    group.finish();
}

/// Benchmark stack vs heap memory usage
///
/// Compares performance when using stack-allocated buffers
/// versus heap-allocated data structures.
fn bench_stack_vs_heap(c: &mut Criterion) {
    let mut group = c.benchmark_group("stack_vs_heap");

    // Stack-allocated small data
    group.bench_function("stack_allocated_small", |b| {
        b.iter(|| {
            let data = [0x42u8; 1024]; // 1KB on stack
            let hash = clockhash256(black_box(&data));
            black_box(hash);
        });
    });

    // Heap-allocated small data
    group.bench_function("heap_allocated_small", |b| {
        b.iter(|| {
            let data = vec![0x42u8; 1024]; // 1KB on heap
            let hash = clockhash256(black_box(&data));
            black_box(hash);
        });
    });

    // Large data (will be heap allocated regardless)
    group.bench_function("large_data_heap", |b| {
        b.iter(|| {
            let data = vec![0x42u8; 1048576]; // 1MB
            let hash = clockhash256(black_box(&data));
            black_box(hash);
        });
    });

    group.finish();
}

/// Benchmark memory copy overhead
///
/// Measures the performance impact of memory copies and data movement
/// during hashing operations.
fn bench_memory_copy_overhead(c: &mut Criterion) {
    let mut group = c.benchmark_group("memory_copy_overhead");

    let sizes = [1024, 4096, 16384, 65536];

    for &size in &sizes {
        // Direct hashing (minimal copying)
        group.bench_with_input(
            BenchmarkId::from_parameter(format!("direct_{}_bytes", size)),
            &size,
            |b, &size| {
                b.iter(|| {
                    let data = vec![0x42u8; size];
                    let hash = clockhash256(black_box(&data));
                    black_box(hash);
                });
            },
        );

        // Hashing with intermediate copy
        group.bench_with_input(
            BenchmarkId::from_parameter(format!("copy_{}_bytes", size)),
            &size,
            |b, &size| {
                b.iter(|| {
                    let data = vec![0x42u8; size];
                    let copy = data.clone(); // Explicit copy
                    let hash = clockhash256(black_box(&copy));
                    black_box(hash);
                });
            },
        );

        // Hashing with multiple copies
        group.bench_with_input(
            BenchmarkId::from_parameter(format!("multiple_copies_{}_bytes", size)),
            &size,
            |b, &size| {
                b.iter(|| {
                    let mut data = vec![0x42u8; size];
                    for i in 0..3 {
                        data.copy_from_slice(&vec![(i as u8); size]); // Multiple copies
                    }
                    let hash = clockhash256(black_box(&data));
                    black_box(hash);
                });
            },
        );
    }

    group.finish();
}

/// Benchmark memory fragmentation effects
///
/// Tests how memory fragmentation and allocation patterns
/// affect hashing performance over time.
fn bench_memory_fragmentation(c: &mut Criterion) {
    let mut group = c.benchmark_group("memory_fragmentation");

    let iterations = [10, 50, 100];

    for &iter in &iterations {
        // Simulate fragmented memory access
        group.bench_with_input(
            BenchmarkId::from_parameter(format!("fragmented_{}_iterations", iter)),
            &iter,
            |b, &iter| {
                b.iter(|| {
                    let mut results = Vec::new();

                    for i in 0..iter {
                        // Allocate data of varying sizes to create fragmentation
                        let size = 1024 + (i % 10) * 512; // Varying sizes
                        let data = vec![(i as u8); size];
                        let hash = clockhash256(black_box(&data));
                        results.push(hash);
                    }

                    black_box(results);
                });
            },
        );

        // Compare with uniform allocation
        group.bench_with_input(
            BenchmarkId::from_parameter(format!("uniform_{}_iterations", iter)),
            &iter,
            |b, &iter| {
                b.iter(|| {
                    let mut results = Vec::new();

                    for i in 0..iter {
                        let data = vec![(i as u8); 4096]; // Uniform size
                        let hash = clockhash256(black_box(&data));
                        results.push(hash);
                    }

                    black_box(results);
                });
            },
        );
    }

    group.finish();
}

/// Benchmark memory access patterns
///
/// Tests different memory access patterns (sequential, random, strided)
/// to understand memory subsystem behavior.
fn bench_memory_access_patterns(c: &mut Criterion) {
    let mut group = c.benchmark_group("memory_access_patterns");

    let size = 262144; // 256KB

    // Sequential access (baseline)
    group.bench_function("sequential_access", |b| {
        b.iter(|| {
            let data = vec![0x42u8; size];
            let hash = clockhash256(black_box(&data));
            black_box(hash);
        });
    });

    // Strided access (every Nth byte)
    let strides = [2, 4, 8, 16];
    for &stride in &strides {
        group.bench_with_input(
            BenchmarkId::from_parameter(format!("stride_{}", stride)),
            &stride,
            |b, &stride| {
                b.iter(|| {
                    let mut data = vec![0x42u8; size];
                    // Modify data with strided pattern
                    for i in (0..size).step_by(stride) {
                        data[i] = (i % 256) as u8;
                    }
                    let hash = clockhash256(black_box(&data));
                    black_box(hash);
                });
            },
        );
    }

    // Random access pattern
    group.bench_function("random_access_pattern", |b| {
        b.iter(|| {
            let mut data = vec![0x42u8; size];
            // Create a pseudo-random access pattern
            let mut idx = 0usize;
            for i in 0..(size / 16) {
                idx = (idx.wrapping_mul(1103515245).wrapping_add(12345)) % size;
                for j in 0..16 {
                    if idx + j < size {
                        data[idx + j] = (i % 256) as u8;
                    }
                }
            }
            let hash = clockhash256(black_box(&data));
            black_box(hash);
        });
    });

    group.finish();
}

/// Benchmark memory usage during bulk operations
///
/// Measures memory usage patterns during high-throughput
/// bulk hashing operations.
fn bench_bulk_memory_usage(c: &mut Criterion) {
    let mut group = c.benchmark_group("bulk_memory_usage");

    let batch_sizes = [10, 100, 1000];

    for &batch_size in &batch_sizes {
        let data_size = 4096; // 4KB per hash

        group.bench_with_input(
            BenchmarkId::from_parameter(format!("bulk_{}_hashes", batch_size)),
            &batch_size,
            |b, &batch_size| {
                b.iter(|| {
                    let mut hashes = Vec::with_capacity(batch_size);

                    for i in 0..batch_size {
                        let data = vec![(i as u8); data_size];
                        let hash = clockhash256(black_box(&data));
                        hashes.push(hash);
                    }

                    black_box(hashes);
                });
            },
        );
    }

    group.finish();
}

criterion_group!(
    benches,
    bench_memory_usage_estimation,
    bench_hasher_construction_memory,
    bench_incremental_hashing_memory,
    bench_memory_bandwidth,
    bench_cache_effects,
    bench_allocation_patterns,
    bench_stack_vs_heap,
    bench_memory_copy_overhead,
    bench_memory_fragmentation,
    bench_memory_access_patterns,
    bench_bulk_memory_usage,
);
criterion_main!(benches);