| Crates.io | water_buffer |
| lib.rs | water_buffer |
| version | 1.2.2-Beta.7 |
| created_at | 2025-12-08 10:27:55.60394+00 |
| updated_at | 2026-01-25 03:46:06.348055+00 |
| description | A high-performance, zero-overhead byte buffer implementation in Rust that outperforms the industry-standard `BytesMut` by **6-11x** in most scenarios. |
| homepage | https://github.com/HassanSharara/water_buffer |
| repository | https://github.com/HassanSharara/water_buffer |
| max_upload_size | |
| id | 1973246 |
| size | 120,997 |
A high-performance, zero-overhead byte buffer implementation in Rust that outperforms the industry-standard BytesMut by 6-11x in most scenarios.
WaterBuffer is designed for maximum speed with minimal abstraction overhead. Built on raw pointer operations and optimized memory allocation strategies, it delivers exceptional performance for buffer-intensive applications.
Tested on: MacBook Pro (M-series/Intel), Rust 1.70+
| Operation | WaterBuffer | BytesMut | Speedup |
|---|---|---|---|
| 10M Single-Byte Pushes | 10.2 ms | 66.4 ms | 6.49x faster โก |
| 100K Mixed Operations | 5.8 ms | 63.5 ms | 10.88x faster โก |
| Preallocated Buffer | 5.7 ms | 61.7 ms | 10.84x faster โก |
| Reallocation Stress | 10.1 ms | 69.1 ms | 6.86x faster โก |
| Bulk Writes (10Kร100KB) | 39.6 ms | 39.6 ms | ~1.00x (tie) ๐ค |
| HTTP Streaming (4KB chunks) | 46.4 ms | 46.5 ms | ~1.00x (tie) ๐ค |
Add this to your Cargo.toml:
[dependencies]
water_buffer = "0.1.0"
use water_buffer::WaterBuffer;
fn main() {
// Create a buffer with initial capacity
let mut buffer = WaterBuffer::with_capacity(1024);
// Push single bytes
buffer.push(42);
buffer.push(43);
// Extend from slice
let data = b"Hello, World!";
buffer.extend_from_slice(data);
// Access elements
println!("First byte: {}", buffer[0]);
println!("Buffer length: {}", buffer.len());
// Get slice view
let slice = &buffer[..];
println!("Data: {:?}", slice);
// Clear buffer (keeps capacity)
buffer.clear();
// Reuse buffer
buffer.extend_from_slice(b"New data");
}
realloc for in-place growth when possible[] indexing and rangesIterator traitlet buffer = water_buffer::WaterBuffer::with_capacity(size);
buffer.push(byte); // Add single byte
buffer.extend_from_slice(&[1, 2, 3]); // Add multiple bytes
let byte = buffer[0]; // Index single element
let slice = &buffer[0..10]; // Get slice view
let all = &buffer[..]; // Get full slice
let len = buffer.len(); // Get length
for byte in buffer.into_iter() {
println!("{}", byte);
}
buffer.clear(); // Reset buffer (keeps capacity)
buffer.advance(n); // Skip n bytes
let remaining = buffer.remaining(); // Get remaining bytes
WaterBuffer has been thoroughly tested for memory safety:
Run the test suite:
# Standard tests
cargo test
# Miri undefined behavior check
cargo +nightly miri test
# Benchmarks
cargo run --release --example benchmark
Scenario: 10 million single-byte pushes
WaterBuffer: 10.2 ms | BytesMut: 66.4 ms
Result: WaterBuffer is 6.49x faster โก
Scenario: 10,000 ร 100KB chunks
WaterBuffer: 39.6 ms | BytesMut: 39.6 ms
Result: Tie - both equally fast ๐ค
Scenario: 100K iterations of extend + 100 pushes + clear
WaterBuffer: 5.8 ms | BytesMut: 63.5 ms
Result: WaterBuffer is 10.88x faster โก
Scenario: Start with 16 bytes, grow to 10M
WaterBuffer: 10.1 ms | BytesMut: 69.1 ms
Result: WaterBuffer is 6.86x faster โก
Scenario: 10M pushes with no reallocations
WaterBuffer: 5.7 ms | BytesMut: 61.7 ms
Result: WaterBuffer is 10.84x faster โก
Scenario: Process 1MB in 4KB chunks, 1000 times
WaterBuffer: 46.4 ms | BytesMut: 46.5 ms
Result: Tie - both equally fast ๐ค
WaterBuffer achieves its performance through:
#[inline(always)] for zero-cost abstractionsrealloc for in-place growth when possiblepub struct WaterBuffer<T> {
cap: usize, // Current capacity
start_pos: usize, // Start position for advance operations
pointer: *mut T, // Raw pointer to data
iterator_pos: usize, // Iterator state
filled_data_length
: usize, // Number of valid bytes
}
std::allocrealloc for efficient capacity growthDrop implementationnew_capacity = max(current_capacity * 1.5, required_size)
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
Buf and BufMut trait implementationsMade with โก and by Hassan Sharara
Performance benchmarks conducted on modern hardware. Your results may vary based on CPU, memory, and workload characteristics.