water_buffer 1.2.8

A high-performance, zero-overhead byte buffer implementation in Rust that outperforms the industry-standard `BytesMut` by **6-11x** in most scenarios.
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
use std::time::Instant;
use std::hint::black_box;
use bytes::{BytesMut, BufMut};
use water_buffer::WaterBuffer;

const ITERATIONS: usize = 5;

struct BenchResult {
    name: String,
    water_avg: f64,
    bytes_avg: f64,
    water_min: f64,
    bytes_min: f64,
}

impl BenchResult {
    fn print(&self) {
        let ratio = if self.water_avg < self.bytes_avg {
            self.bytes_avg / self.water_avg
        } else {
            self.water_avg / self.bytes_avg
        };
        let winner = if self.water_avg < self.bytes_avg { "WaterBuffer" } else { "BytesMut" };

        println!("  WaterBuffer: avg={:.3}ms, min={:.3}ms", self.water_avg, self.water_min);
        println!("  BytesMut:    avg={:.3}ms, min={:.3}ms", self.bytes_avg, self.bytes_min);
        println!("{} is {:.2}x faster (by avg)\n", winner, ratio);
    }
}

fn run_benchmark<F1, F2>(name: &str, mut water_fn: F1, mut bytes_fn: F2) -> BenchResult
where
    F1: FnMut(),
    F2: FnMut(),
{
    println!("{}", name);
    println!("{}", "".repeat(name.len()));

    // Warmup
    water_fn();
    bytes_fn();

    let mut water_times = Vec::with_capacity(ITERATIONS);
    let mut bytes_times = Vec::with_capacity(ITERATIONS);

    for _ in 0..ITERATIONS {
        let start = Instant::now();
        water_fn();
        water_times.push(start.elapsed().as_micros() as f64 / 1000.0);

        let start = Instant::now();
        bytes_fn();
        bytes_times.push(start.elapsed().as_micros() as f64 / 1000.0);
    }

    let water_avg = water_times.iter().sum::<f64>() / ITERATIONS as f64;
    let bytes_avg = bytes_times.iter().sum::<f64>() / ITERATIONS as f64;
    let water_min = water_times.iter().cloned().fold(f64::INFINITY, f64::min);
    let bytes_min = bytes_times.iter().cloned().fold(f64::INFINITY, f64::min);

    let result = BenchResult {
        name: name.to_string(),
        water_avg,
        bytes_avg,
        water_min,
        bytes_min,
    };

    result.print();
    result
}

fn main() {

    // let pr = Profiler::new_heap();
    println!("╔═══════════════════════════════════════════════════════════╗");
    println!("║  ADVANCED BUFFER BENCHMARK SUITE - PRODUCTION SCENARIOS  ║");
    println!("╚═══════════════════════════════════════════════════════════╝\n");

    let mut results = Vec::new();
    const TH:usize = 100_000;
    // ============ Test 1: HTTP Request Building ============
    results.push(run_benchmark(
        "Test 1: HTTP Request Building (100K requests)",
        || {
            let headers = b"Content-Type: application/json\r\nAuthorization: Bearer token123\r\n";
            let body = b"{\"user\":\"test\",\"data\":\"payload\"}";

            for _ in 0..TH {
                let mut buf = WaterBuffer::with_capacity(256);
                buf.extend_from_slice(b"POST /api/endpoint HTTP/1.1\r\n");
                buf.extend_from_slice(headers);
                buf.extend_from_slice(b"Content-Length: ");
                buf.extend_from_slice(body.len().to_string().as_bytes());
                buf.extend_from_slice(b"\r\n\r\n");
                buf.extend_from_slice(body);
                black_box(buf);
            }
        },
        || {
            let headers = b"Content-Type: application/json\r\nAuthorization: Bearer token123\r\n";
            let body = b"{\"user\":\"test\",\"data\":\"payload\"}";

            for _ in 0..TH {
                let mut buf = BytesMut::with_capacity(256);
                buf.extend_from_slice(b"POST /api/endpoint HTTP/1.1\r\n");
                buf.extend_from_slice(headers);
                buf.extend_from_slice(b"Content-Length: ");
                buf.extend_from_slice(body.len().to_string().as_bytes());
                buf.extend_from_slice(b"\r\n\r\n");
                buf.extend_from_slice(body);
                black_box(buf);
            }
        },
    ));

    // ============ Test 2: JSON Serialization Simulation ============
    results.push(run_benchmark(
        "Test 2: JSON Serialization (50K complex objects)",
        || {
            for i in 0..50_000 {
                let mut buf = WaterBuffer::with_capacity(512);
                buf.push(b'{');
                buf.extend_from_slice(b"\"id\":");
                buf.extend_from_slice(i.to_string().as_bytes());
                buf.extend_from_slice(b",\"name\":\"user_");
                buf.extend_from_slice(i.to_string().as_bytes());
                buf.extend_from_slice(b"\",\"active\":true,\"tags\":[\"a\",\"b\",\"c\"],\"metadata\":{\"key\":\"value\"}}");
                black_box(buf);
            }
        },
        || {
            for i in 0..50_000 {
                let mut buf = BytesMut::with_capacity(512);
                buf.put_u8(b'{');
                buf.extend_from_slice(b"\"id\":");
                buf.extend_from_slice(i.to_string().as_bytes());
                buf.extend_from_slice(b",\"name\":\"user_");
                buf.extend_from_slice(i.to_string().as_bytes());
                buf.extend_from_slice(b"\",\"active\":true,\"tags\":[\"a\",\"b\",\"c\"],\"metadata\":{\"key\":\"value\"}}");
                black_box(buf);
            }
        },
    ));

    // ============ Test 3: Packet Fragmentation/Reassembly ============
    results.push(run_benchmark(
        "Test 3: Network Packet Reassembly (10K packets, 1KB each)",
        || {
            // Pre-allocate with better estimate
            let mut accumulated = WaterBuffer::with_capacity(100_000);
            let packet = vec![0u8; 1024];

            for _ in 0..10_000 {
                accumulated.extend_from_slice(&packet);
                if accumulated.len() >= 100_000 {
                    accumulated.clear();
                }
            }
            black_box(accumulated);
        },
        || {
            let mut accumulated = BytesMut::with_capacity(100_000);
            let packet = vec![0u8; 1024];

            for _ in 0..10_000 {
                accumulated.extend_from_slice(&packet);
                if accumulated.len() >= 100_000 {
                    accumulated.clear();
                }
            }
            black_box(accumulated);
        },
    ));

    // ============ Test 4: Pathological Growth (Fibonacci-like) ============
    results.push(run_benchmark(
        "Test 4: Pathological Growth Pattern (exponential reallocs)",
        || {
            let mut buf = WaterBuffer::with_capacity(8);
            for size in [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536] {
                let chunk = vec![0u8; size];
                for _ in 0..1000 {
                    buf.extend_from_slice(&chunk);
                }
                buf.clear();
            }
            black_box(buf);
        },
        || {
            let mut buf = BytesMut::with_capacity(8);
            for size in [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536] {
                let chunk = vec![0u8; size];
                for _ in 0..1000 {
                    buf.extend_from_slice(&chunk);
                }
                buf.clear();
            }
            black_box(buf);
        },
    ));

    // ============ Test 5: Database Row Serialization ============
    results.push(run_benchmark(
        "Test 5: Database Row Serialization (100K rows)",
        || {
            for i in 0..100_000 {
                let mut buf = WaterBuffer::with_capacity(128);
                // Simulate binary protocol: length prefix + data
                buf.push(5); // field count
                // buf.extend_from_slice(&i.to_le_bytes());
                buf.extend_from_slice(b"username_");
                buf.extend_from_slice(i.to_string().as_bytes());
                buf.push(0); // null terminator
                buf.extend_from_slice(&(i as f64 * 1.5).to_le_bytes());
                buf.push(1); // boolean
                black_box(buf);
            }
        },
        || {
            for i in 0..100_000 {
                let mut buf = BytesMut::with_capacity(128);
                buf.put_u8(5);
                // buf.extend_from_slice(&i.to_le_bytes());
                buf.extend_from_slice(b"username_");
                buf.extend_from_slice(i.to_string().as_bytes());
                buf.put_u8(0);
                buf.extend_from_slice(&(i as f64 * 1.5).to_le_bytes());
                buf.put_u8(1);
                black_box(buf);
            }
        },
    ));

    // ============ Test 6: WebSocket Frame Building ============
    results.push(run_benchmark(
        "Test 6: WebSocket Frame Encoding (50K messages)",
        || {
            let payload = b"Hello, WebSocket! This is a test message with some data.";
            for _ in 0..50_000 {
                let mut buf = WaterBuffer::with_capacity(128);
                buf.push(0x81); // FIN + text frame
                buf.push(payload.len() as u8);
                buf.extend_from_slice(payload);
                black_box(buf);
            }
        },
        || {
            let payload = b"Hello, WebSocket! This is a test message with some data.";
            for _ in 0..50_000 {
                let mut buf = BytesMut::with_capacity(128);
                buf.put_u8(0x81);
                buf.put_u8(payload.len() as u8);
                buf.extend_from_slice(payload);
                black_box(buf);
            }
        },
    ));

    // ============ Test 7: Memory Churn (Allocate/Drop) ============
    results.push(run_benchmark(
        "Test 7: Memory Churn - Allocate & Drop (100K cycles)",
        || {
            let data = vec![42u8; 1024];
            for _ in 0..100_000 {
                let mut buf = WaterBuffer::with_capacity(2048);
                buf.extend_from_slice(&data);
                buf.extend_from_slice(&data);
                black_box(&buf);
                // buf drops here
            }
        },
        || {
            let data = vec![42u8; 1024];
            for _ in 0..100_000 {
                let mut buf = BytesMut::with_capacity(2048);
                buf.extend_from_slice(&data);
                buf.extend_from_slice(&data);
                black_box(&buf);
                // buf drops here
            }
        },
    ));

    // ============ Test 8: CSV Generation ============
    results.push(run_benchmark(
        "Test 8: CSV Row Generation (50K rows, 10 columns)",
        || {
            for i in 0..50_000 {
                let mut buf = WaterBuffer::with_capacity(256);
                for col in 0..10 {
                    if col > 0 {
                        buf.push(b',');
                    }
                    buf.extend_from_slice(format!("col{}_{}", col, i).as_bytes());
                }
                buf.push(b'\n');
                black_box(buf);
            }
        },
        || {
            for i in 0..50_000 {
                let mut buf = BytesMut::with_capacity(256);
                for col in 0..10 {
                    if col > 0 {
                        buf.put_u8(b',');
                    }
                    buf.extend_from_slice(format!("col{}_{}", col, i).as_bytes());
                }
                buf.put_u8(b'\n');
                black_box(buf);
            }
        },
    ));

    // ============ Test 9: Protocol Buffer-like Encoding ============
    results.push(run_benchmark(
        "Test 9: Protobuf-style Encoding (100K messages)",
        || {
            for i in 0..100_000 {
                let mut buf = WaterBuffer::with_capacity(64);
                // Field 1: varint
                buf.push(0x08);
                buf.extend_from_slice(&encode_varint(i as u64));
                // Field 2: string
                buf.push(0x12);
                let s = format!("msg_{}", i);
                buf.push(s.len() as u8);
                buf.extend_from_slice(s.as_bytes());
                black_box(buf);
            }
        },
        || {
            for i in 0..100_000 {
                let mut buf = BytesMut::with_capacity(64);
                buf.put_u8(0x08);
                buf.extend_from_slice(&encode_varint(i as u64));
                buf.put_u8(0x12);
                let s = format!("msg_{}", i);
                buf.put_u8(s.len() as u8);
                buf.extend_from_slice(s.as_bytes());
                black_box(buf);
            }
        },
    ));

    // ============ Test 10: Extreme Reallocation Torture ============
    results.push(run_benchmark(
        "Test 10: Reallocation Torture (start 1 byte → 10MB)",
        || {
            let mut buf = WaterBuffer::with_capacity(1);
            for i in 0..10_000_000 {
                buf.push((i & 0xFF) as u8);
            }
            black_box(buf);
        },
        || {
            let mut buf = BytesMut::with_capacity(1);
            for i in 0..10_000_000 {
                buf.put_u8((i & 0xFF) as u8);
            }
            black_box(buf);
        },
    ));

    // ============ Test 11: Alternating Read/Write ============
    results.push(run_benchmark(
        "Test 11: Alternating Operations (10K cycles: write → read → clear)",
        || {
            let data = vec![42u8; 1000];
            for _ in 0..10_000 {
                let mut buf = WaterBuffer::with_capacity(2000);
                buf.extend_from_slice(&data);
                buf.extend_from_slice(&data);
                let _len = buf.len();
                let _slice = buf.as_ref();
                buf.clear();
            }
        },
        || {
            let data = vec![42u8; 1000];
            for _ in 0..10_000 {
                let mut buf = BytesMut::with_capacity(2000);
                buf.extend_from_slice(&data);
                buf.extend_from_slice(&data);
                let _len = buf.len();
                let _slice = buf.as_ref();
                buf.clear();
            }
        },
    ));

    // ============ Test 12: Log Line Formatting ============
    results.push(run_benchmark(
        "Test 12: Log Line Formatting (100K entries)",
        || {
            for i in 0..100_000 {
                let mut buf = WaterBuffer::with_capacity(256);
                buf.extend_from_slice(b"[2024-12-12 10:30:45] [INFO] ");
                buf.extend_from_slice(format!("Request {} processed successfully", i).as_bytes());
                buf.extend_from_slice(b" - duration: ");
                buf.extend_from_slice(format!("{}ms", i % 1000).as_bytes());
                buf.push(b'\n');
                black_box(buf);
            }
        },
        || {
            for i in 0..100_000 {
                let mut buf = BytesMut::with_capacity(256);
                buf.extend_from_slice(b"[2024-12-12 10:30:45] [INFO] ");
                buf.extend_from_slice(format!("Request {} processed successfully", i).as_bytes());
                buf.extend_from_slice(b" - duration: ");
                buf.extend_from_slice(format!("{}ms", i % 1000).as_bytes());
                buf.put_u8(b'\n');
                black_box(buf);
            }
        },
    ));

    // Summary
    println!("\n╔═══════════════════════════════════════════════════════════╗");
    println!("║                    BENCHMARK SUMMARY                      ║");
    println!("╚═══════════════════════════════════════════════════════════╝\n");

    let mut water_wins = 0;
    let mut bytes_wins = 0;

    for result in &results {
        if result.water_avg < result.bytes_avg {
            water_wins += 1;
            println!("✓ WaterBuffer wins: {}", result.name);
        } else {
            bytes_wins += 1;
            println!("✓ BytesMut wins:    {}", result.name);
        }
    }

    println!("\n╔═══════════════════════════════════════════════════════════╗");
    println!("║  Final Score: WaterBuffer {} - {} BytesMut", water_wins, bytes_wins);
    println!("╚═══════════════════════════════════════════════════════════╝");
}

fn encode_varint(mut value: u64) -> Vec<u8> {
    let mut buf = Vec::new();
    while value >= 0x80 {
        buf.push((value as u8) | 0x80);
        value >>= 7;
    }
    buf.push(value as u8);
    buf
}