libdictenstein 0.1.0

High-performance dictionary data structures (trie, DAWG, double-array trie, suffix automaton, lock-free durable persistent ART) behind one trait API; pairs with liblevenshtein for fuzzy matching
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
//! Benchmarks for serialization performance optimization.

use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use libdictenstein::pathmap::PathMapDictionary;
use libdictenstein::serialization::{BincodeSerializer, DictionarySerializer, JsonSerializer};
use std::io::Cursor;

#[cfg(feature = "protobuf")]
use libdictenstein::serialization::{OptimizedProtobufSerializer, ProtobufSerializer};

#[cfg(feature = "compression")]
use libdictenstein::serialization::GzipSerializer;

/// Create a dictionary of the specified size with varied word lengths
fn create_dictionary(size: usize) -> PathMapDictionary {
    let words: Vec<String> = (0..size)
        .map(|i| {
            // Create words of varying lengths (4-12 characters)
            let len = 4 + (i % 9);
            format!("word{:0width$}", i, width = len - 4)
        })
        .collect();
    PathMapDictionary::from_terms(words)
}

/// Benchmark: Bincode serialization performance
fn bench_bincode_serialize(c: &mut Criterion) {
    let mut group = c.benchmark_group("bincode_serialize");

    for size in [100, 500, 1000, 5000].iter() {
        let dict = create_dictionary(*size);
        group.throughput(Throughput::Elements(*size as u64));

        group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, _| {
            b.iter(|| {
                let mut buffer = Vec::new();
                BincodeSerializer::serialize(black_box(&dict), &mut buffer)
                    .expect("Serialization failed");
                black_box(buffer);
            });
        });
    }
    group.finish();
}

/// Benchmark: Bincode deserialization performance
fn bench_bincode_deserialize(c: &mut Criterion) {
    let mut group = c.benchmark_group("bincode_deserialize");

    for size in [100, 500, 1000, 5000].iter() {
        let dict = create_dictionary(*size);
        let mut buffer = Vec::new();
        BincodeSerializer::serialize(&dict, &mut buffer).unwrap();

        group.throughput(Throughput::Elements(*size as u64));
        group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, _| {
            b.iter(|| {
                let cursor = Cursor::new(black_box(&buffer));
                let loaded: PathMapDictionary =
                    BincodeSerializer::deserialize(cursor).expect("Deserialization failed");
                black_box(loaded);
            });
        });
    }
    group.finish();
}

/// Benchmark: JSON serialization performance
fn bench_json_serialize(c: &mut Criterion) {
    let mut group = c.benchmark_group("json_serialize");

    for size in [100, 500, 1000, 5000].iter() {
        let dict = create_dictionary(*size);
        group.throughput(Throughput::Elements(*size as u64));

        group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, _| {
            b.iter(|| {
                let mut buffer = Vec::new();
                JsonSerializer::serialize(black_box(&dict), &mut buffer)
                    .expect("Serialization failed");
                black_box(buffer);
            });
        });
    }
    group.finish();
}

/// Benchmark: JSON deserialization performance
fn bench_json_deserialize(c: &mut Criterion) {
    let mut group = c.benchmark_group("json_deserialize");

    for size in [100, 500, 1000, 5000].iter() {
        let dict = create_dictionary(*size);
        let mut buffer = Vec::new();
        JsonSerializer::serialize(&dict, &mut buffer).unwrap();

        group.throughput(Throughput::Elements(*size as u64));
        group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, _| {
            b.iter(|| {
                let cursor = Cursor::new(black_box(&buffer));
                let loaded: PathMapDictionary =
                    JsonSerializer::deserialize(cursor).expect("Deserialization failed");
                black_box(loaded);
            });
        });
    }
    group.finish();
}

#[cfg(feature = "protobuf")]
/// Benchmark: Protobuf V1 serialization performance
fn bench_protobuf_v1_serialize(c: &mut Criterion) {
    let mut group = c.benchmark_group("protobuf_v1_serialize");

    for size in [100, 500, 1000, 5000].iter() {
        let dict = create_dictionary(*size);
        group.throughput(Throughput::Elements(*size as u64));

        group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, _| {
            b.iter(|| {
                let mut buffer = Vec::new();
                ProtobufSerializer::serialize(black_box(&dict), &mut buffer)
                    .expect("Serialization failed");
                black_box(buffer);
            });
        });
    }
    group.finish();
}

#[cfg(feature = "protobuf")]
/// Benchmark: Protobuf V1 deserialization performance
fn bench_protobuf_v1_deserialize(c: &mut Criterion) {
    let mut group = c.benchmark_group("protobuf_v1_deserialize");

    for size in [100, 500, 1000, 5000].iter() {
        let dict = create_dictionary(*size);
        let mut buffer = Vec::new();
        ProtobufSerializer::serialize(&dict, &mut buffer).unwrap();

        group.throughput(Throughput::Elements(*size as u64));
        group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, _| {
            b.iter(|| {
                let cursor = Cursor::new(black_box(&buffer));
                let loaded: PathMapDictionary =
                    ProtobufSerializer::deserialize(cursor).expect("Deserialization failed");
                black_box(loaded);
            });
        });
    }
    group.finish();
}

#[cfg(feature = "protobuf")]
/// Benchmark: Protobuf V2 (optimized) serialization performance
fn bench_protobuf_v2_serialize(c: &mut Criterion) {
    let mut group = c.benchmark_group("protobuf_v2_serialize");

    for size in [100, 500, 1000, 5000].iter() {
        let dict = create_dictionary(*size);
        group.throughput(Throughput::Elements(*size as u64));

        group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, _| {
            b.iter(|| {
                let mut buffer = Vec::new();
                OptimizedProtobufSerializer::serialize(black_box(&dict), &mut buffer)
                    .expect("Serialization failed");
                black_box(buffer);
            });
        });
    }
    group.finish();
}

#[cfg(feature = "protobuf")]
/// Benchmark: Protobuf V2 (optimized) deserialization performance
fn bench_protobuf_v2_deserialize(c: &mut Criterion) {
    let mut group = c.benchmark_group("protobuf_v2_deserialize");

    for size in [100, 500, 1000, 5000].iter() {
        let dict = create_dictionary(*size);
        let mut buffer = Vec::new();
        OptimizedProtobufSerializer::serialize(&dict, &mut buffer).unwrap();

        group.throughput(Throughput::Elements(*size as u64));
        group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, _| {
            b.iter(|| {
                let cursor = Cursor::new(black_box(&buffer));
                let loaded: PathMapDictionary = OptimizedProtobufSerializer::deserialize(cursor)
                    .expect("Deserialization failed");
                black_box(loaded);
            });
        });
    }
    group.finish();
}

/// Benchmark: Format comparison - serialization speed
fn bench_format_comparison_serialize(c: &mut Criterion) {
    let dict = create_dictionary(1000);
    let mut group = c.benchmark_group("format_comparison_serialize");
    group.throughput(Throughput::Elements(1000));

    group.bench_function("bincode", |b| {
        b.iter(|| {
            let mut buffer = Vec::new();
            BincodeSerializer::serialize(black_box(&dict), &mut buffer).unwrap();
            black_box(buffer);
        });
    });

    group.bench_function("json", |b| {
        b.iter(|| {
            let mut buffer = Vec::new();
            JsonSerializer::serialize(black_box(&dict), &mut buffer).unwrap();
            black_box(buffer);
        });
    });

    #[cfg(feature = "protobuf")]
    group.bench_function("protobuf_v1", |b| {
        b.iter(|| {
            let mut buffer = Vec::new();
            ProtobufSerializer::serialize(black_box(&dict), &mut buffer).unwrap();
            black_box(buffer);
        });
    });

    #[cfg(feature = "protobuf")]
    group.bench_function("protobuf_v2", |b| {
        b.iter(|| {
            let mut buffer = Vec::new();
            OptimizedProtobufSerializer::serialize(black_box(&dict), &mut buffer).unwrap();
            black_box(buffer);
        });
    });

    group.finish();
}

/// Benchmark: Format comparison - deserialization speed
fn bench_format_comparison_deserialize(c: &mut Criterion) {
    let dict = create_dictionary(1000);
    let mut group = c.benchmark_group("format_comparison_deserialize");
    group.throughput(Throughput::Elements(1000));

    let mut bincode_buffer = Vec::new();
    BincodeSerializer::serialize(&dict, &mut bincode_buffer).unwrap();
    group.bench_function("bincode", |b| {
        b.iter(|| {
            let cursor = Cursor::new(black_box(&bincode_buffer));
            let loaded: PathMapDictionary = BincodeSerializer::deserialize(cursor).unwrap();
            black_box(loaded);
        });
    });

    let mut json_buffer = Vec::new();
    JsonSerializer::serialize(&dict, &mut json_buffer).unwrap();
    group.bench_function("json", |b| {
        b.iter(|| {
            let cursor = Cursor::new(black_box(&json_buffer));
            let loaded: PathMapDictionary = JsonSerializer::deserialize(cursor).unwrap();
            black_box(loaded);
        });
    });

    #[cfg(feature = "protobuf")]
    {
        let mut protobuf_v1_buffer = Vec::new();
        ProtobufSerializer::serialize(&dict, &mut protobuf_v1_buffer).unwrap();
        group.bench_function("protobuf_v1", |b| {
            b.iter(|| {
                let cursor = Cursor::new(black_box(&protobuf_v1_buffer));
                let loaded: PathMapDictionary = ProtobufSerializer::deserialize(cursor).unwrap();
                black_box(loaded);
            });
        });

        let mut protobuf_v2_buffer = Vec::new();
        OptimizedProtobufSerializer::serialize(&dict, &mut protobuf_v2_buffer).unwrap();
        group.bench_function("protobuf_v2", |b| {
            b.iter(|| {
                let cursor = Cursor::new(black_box(&protobuf_v2_buffer));
                let loaded: PathMapDictionary =
                    OptimizedProtobufSerializer::deserialize(cursor).unwrap();
                black_box(loaded);
            });
        });
    }

    group.finish();
}

#[cfg(feature = "compression")]
/// Benchmark: Gzip+Bincode serialization performance
fn bench_gzip_bincode_serialize(c: &mut Criterion) {
    let mut group = c.benchmark_group("gzip_bincode_serialize");

    for size in [100, 500, 1000, 5000].iter() {
        let dict = create_dictionary(*size);
        group.throughput(Throughput::Elements(*size as u64));

        group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, _| {
            b.iter(|| {
                let mut buffer = Vec::new();
                GzipSerializer::<BincodeSerializer>::serialize(black_box(&dict), &mut buffer)
                    .expect("Serialization failed");
                black_box(buffer);
            });
        });
    }
    group.finish();
}

#[cfg(feature = "compression")]
/// Benchmark: Gzip+Bincode deserialization performance
fn bench_gzip_bincode_deserialize(c: &mut Criterion) {
    let mut group = c.benchmark_group("gzip_bincode_deserialize");

    for size in [100, 500, 1000, 5000].iter() {
        let dict = create_dictionary(*size);
        let mut buffer = Vec::new();
        GzipSerializer::<BincodeSerializer>::serialize(&dict, &mut buffer).unwrap();

        group.throughput(Throughput::Elements(*size as u64));
        group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, _| {
            b.iter(|| {
                let cursor = Cursor::new(black_box(&buffer));
                let loaded: PathMapDictionary =
                    GzipSerializer::<BincodeSerializer>::deserialize(cursor)
                        .expect("Deserialization failed");
                black_box(loaded);
            });
        });
    }
    group.finish();
}

#[cfg(feature = "compression")]
/// Benchmark: Gzip+JSON serialization performance
fn bench_gzip_json_serialize(c: &mut Criterion) {
    let mut group = c.benchmark_group("gzip_json_serialize");

    for size in [100, 500, 1000, 5000].iter() {
        let dict = create_dictionary(*size);
        group.throughput(Throughput::Elements(*size as u64));

        group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, _| {
            b.iter(|| {
                let mut buffer = Vec::new();
                GzipSerializer::<JsonSerializer>::serialize(black_box(&dict), &mut buffer)
                    .expect("Serialization failed");
                black_box(buffer);
            });
        });
    }
    group.finish();
}

#[cfg(feature = "compression")]
/// Benchmark: Gzip+JSON deserialization performance
fn bench_gzip_json_deserialize(c: &mut Criterion) {
    let mut group = c.benchmark_group("gzip_json_deserialize");

    for size in [100, 500, 1000, 5000].iter() {
        let dict = create_dictionary(*size);
        let mut buffer = Vec::new();
        GzipSerializer::<JsonSerializer>::serialize(&dict, &mut buffer).unwrap();

        group.throughput(Throughput::Elements(*size as u64));
        group.bench_with_input(BenchmarkId::from_parameter(size), size, |b, _| {
            b.iter(|| {
                let cursor = Cursor::new(black_box(&buffer));
                let loaded: PathMapDictionary =
                    GzipSerializer::<JsonSerializer>::deserialize(cursor)
                        .expect("Deserialization failed");
                black_box(loaded);
            });
        });
    }
    group.finish();
}

#[cfg(feature = "compression")]
/// Benchmark: Compression overhead comparison
fn bench_compression_overhead(c: &mut Criterion) {
    let dict = create_dictionary(1000);
    let mut group = c.benchmark_group("compression_overhead");
    group.throughput(Throughput::Elements(1000));

    // Bincode without compression
    group.bench_function("bincode_plain", |b| {
        b.iter(|| {
            let mut buffer = Vec::new();
            BincodeSerializer::serialize(black_box(&dict), &mut buffer).unwrap();
            black_box(buffer);
        });
    });

    // Bincode with compression
    group.bench_function("bincode_gzip", |b| {
        b.iter(|| {
            let mut buffer = Vec::new();
            GzipSerializer::<BincodeSerializer>::serialize(black_box(&dict), &mut buffer).unwrap();
            black_box(buffer);
        });
    });

    // JSON without compression
    group.bench_function("json_plain", |b| {
        b.iter(|| {
            let mut buffer = Vec::new();
            JsonSerializer::serialize(black_box(&dict), &mut buffer).unwrap();
            black_box(buffer);
        });
    });

    // JSON with compression
    group.bench_function("json_gzip", |b| {
        b.iter(|| {
            let mut buffer = Vec::new();
            GzipSerializer::<JsonSerializer>::serialize(black_box(&dict), &mut buffer).unwrap();
            black_box(buffer);
        });
    });

    group.finish();
}

#[cfg(feature = "compression")]
/// Benchmark: File size comparison
fn bench_file_size_comparison(c: &mut Criterion) {
    let dict = create_dictionary(1000);
    let mut group = c.benchmark_group("file_size");

    // Measure sizes without running benchmarks
    let mut bincode_plain = Vec::new();
    BincodeSerializer::serialize(&dict, &mut bincode_plain).unwrap();

    let mut bincode_gzip = Vec::new();
    GzipSerializer::<BincodeSerializer>::serialize(&dict, &mut bincode_gzip).unwrap();

    let mut json_plain = Vec::new();
    JsonSerializer::serialize(&dict, &mut json_plain).unwrap();

    let mut json_gzip = Vec::new();
    GzipSerializer::<JsonSerializer>::serialize(&dict, &mut json_gzip).unwrap();

    println!("\n=== File Size Comparison (1000 words) ===");
    println!("Bincode:           {:6} bytes", bincode_plain.len());
    println!(
        "Bincode+Gzip:      {:6} bytes ({:.1}% of original)",
        bincode_gzip.len(),
        100.0 * bincode_gzip.len() as f64 / bincode_plain.len() as f64
    );
    println!("JSON:              {:6} bytes", json_plain.len());
    println!(
        "JSON+Gzip:         {:6} bytes ({:.1}% of original)",
        json_gzip.len(),
        100.0 * json_gzip.len() as f64 / json_plain.len() as f64
    );

    // Dummy benchmark to trigger output
    group.bench_function("dummy", |b| b.iter(|| 1));
    group.finish();
}

criterion_group!(
    serialization_benches,
    bench_bincode_serialize,
    bench_bincode_deserialize,
    bench_json_serialize,
    bench_json_deserialize,
    bench_format_comparison_serialize,
    bench_format_comparison_deserialize,
);

#[cfg(feature = "protobuf")]
criterion_group!(
    protobuf_benches,
    bench_protobuf_v1_serialize,
    bench_protobuf_v1_deserialize,
    bench_protobuf_v2_serialize,
    bench_protobuf_v2_deserialize,
);

#[cfg(feature = "compression")]
criterion_group!(
    compression_benches,
    bench_gzip_bincode_serialize,
    bench_gzip_bincode_deserialize,
    bench_gzip_json_serialize,
    bench_gzip_json_deserialize,
    bench_compression_overhead,
    bench_file_size_comparison,
);

#[cfg(all(feature = "protobuf", feature = "compression"))]
criterion_main!(serialization_benches, protobuf_benches, compression_benches);

#[cfg(all(feature = "protobuf", not(feature = "compression")))]
criterion_main!(serialization_benches, protobuf_benches);

#[cfg(all(not(feature = "protobuf"), feature = "compression"))]
criterion_main!(serialization_benches, compression_benches);

#[cfg(all(not(feature = "protobuf"), not(feature = "compression")))]
criterion_main!(serialization_benches);