🐆 CheetahString
A lightweight, high-performance string type optimized for real-world use cases.
CheetahString is a versatile string type that goes beyond the standard library's String, providing zero-allocation optimizations for common patterns and seamless interoperability with various string representations. It's designed for both std and no_std environments.
✨ Features
-
🚀 Zero-Allocation Optimization
- Small String Optimization (SSO): Strings ≤ 23 bytes stored inline (no heap allocation)
- Static string support with
'staticlifetime - Efficient Arc-based sharing for larger strings
-
🔧 Rich API
- Query methods:
starts_with,ends_with,contains,find,rfind - Transformation:
to_uppercase,to_lowercase,replace,trim - Iteration:
split,lines,chars - Builder pattern:
with_capacity,push_str,reserve - String concatenation with
+and+=operators
- Query methods:
-
🌐 Flexible Integration
- Optional
bytessupport for zero-copy interop with thebytescrate - Optional
serdesupport for serialization/deserialization no_stdcompatible (requiresalloc)
- Optional
-
⚡ Performance Focused
- Optimized for common string operations
- Reduced memory allocations via intelligent internal representation
- Optional SIMD acceleration for string matching operations (x86_64 SSE2)
- Benchmarked against standard library types
-
🛡️ Safe & Correct
- UTF-8 validation with safe constructors (
try_from_bytes,try_from_vec) - Comprehensive test coverage
- Well-documented API with examples
- UTF-8 validation with safe constructors (
📦 Installation
Add this to your Cargo.toml:
[]
= "1.0.0"
Optional Features
[]
= { = "1.0.0", = ["bytes", "serde", "simd"] }
Available features:
std(default): Enable standard library supportbytes: Integration with thebytescrateserde: Serialization support via serdesimd: SIMD-accelerated string operations (x86_64 SSE2)
🚀 Quick Start
use CheetahString;
// Create from various sources
let s1 = from; // From &str
let s2 = from; // From String
let s3 = from_static_str; // From 'static str (zero-cost)
// Small strings (≤ 23 bytes) use no heap allocation
let small = from; // Stored inline!
// String operations
let s = from;
assert!; // Supports &str
assert!; // Also supports char
assert!;
assert!;
assert_eq!;
// Concatenation
let greeting = from;
let name = from;
let message = greeting + name.as_str; // "Hello Rust"
// Builder pattern for efficient construction
let mut builder = with_capacity;
builder.push_str;
builder.push_str;
builder.push_str;
// Safe UTF-8 validation
let bytes = b"hello";
let s = try_from_bytes.unwrap;
📊 Performance
CheetahString is designed with performance in mind:
- Small String Optimization (SSO): Strings up to 23 bytes are stored inline without heap allocation
- Efficient Sharing: Large strings use
Arc<str>for cheap cloning - Optimized Operations: Common operations like concatenation have fast-path implementations
- SIMD Acceleration (with
simdfeature): String matching operations (starts_with,ends_with,contains,find, equality comparisons) are accelerated using SSE2 SIMD instructions on x86_64 platforms. The implementation automatically falls back to scalar code for small inputs or when SIMD is not available.
Run benchmarks:
# With SIMD feature
🔍 Internal Representation
CheetahString intelligently chooses the most efficient storage:
| String Type | Storage | Heap Allocations | Use Case |
|---|---|---|---|
| ≤ 23 bytes | Inline (SSO) | 0 | Short strings, identifiers |
| Static | &'static str |
0 | String literals |
| Dynamic | Arc<str> |
1 | Long strings, shared data |
| From Arc | Arc<String> |
1 | Interop with existing Arc |
| Bytes | bytes::Bytes |
1 | Network buffers (with feature) |
🔧 API Overview
Construction
new(),empty(),default()- Create empty stringsfrom(s)- From&str,String,&String,char, etc.from_static_str(s)- Zero-cost wrapper for'static strfrom_string(s)- From ownedStringtry_from_bytes(b)- Safe construction from bytes with UTF-8 validationwith_capacity(n)- Pre-allocate capacity
Query Methods
len(),is_empty(),as_str(),as_bytes()starts_with(),ends_with(),contains()- Support both&strandcharpatternsfind(),rfind()
Transformation
to_uppercase(),to_lowercase()replace(),replacen()trim(),trim_start(),trim_end()substring(),repeat()
Iteration
chars()- Iterate over characters (double-ended iterator)split()- Split by pattern (supports&strandchar)lines()- Iterate over lines
Mutation
push_str()- Append string slicereserve()- Reserve additional capacity
Operators
+- Concatenation+=- Append in-place (optimized)==,!=- Equality comparison withstr,String, etc.
🎯 Use Cases
CheetahString is ideal for:
- High-performance servers: Reduce allocations in hot paths
- Memory-constrained environments: Efficient memory usage with SSO
- Network protocols: Integration with
bytescrate - Configuration systems: Fast handling of static and dynamic strings
- No-std applications: Embedded systems and WASM
🏗️ Projects Using CheetahString
- RocketMQ Rust - Apache RocketMQ Rust implementation
🤝 Contributing
Contributions are welcome! Here's how you can help:
- Report Issues: Found a bug? Open an issue
- Submit PRs: Improvements and bug fixes are appreciated
- Add Benchmarks: Help us track performance across use cases
- Improve Documentation: Better docs help everyone
Development Setup
# Clone the repository
# Run tests
# Run benchmarks
# Run with all features
📝 License
This project is licensed under either of:
- Apache License, Version 2.0 (LICENSE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
🙏 Acknowledgments
CheetahString is inspired by the need for a flexible, high-performance string type in Rust that bridges the gap between String, &str, Arc<str>, and specialized types like bytes::Bytes.