StaticId
This Rust library provides an extremely memory-efficient implementation of StaticId for handling interned identifiers with optimal performance.
Features
StaticId: A highly optimized, interned identifier type combining a code and a venue.- Exceptional memory efficiency: Each
StaticIdis represented by a single 64-bit pointer. - Ultra-fast comparisons: Equality checks and hashing operations only compare 8 bytes, regardless of the actual string length.
- Lazy evaluation: The actual string data is only accessed during serialization.
- Serialization and deserialization support using Serde.
Memory Efficiency and Performance
The key features of StaticId are its memory efficiency and performance:
- Compact Representation: Each
StaticIdis stored as a single 64-bit pointer, regardless of the length of the underlying strings. - Fast Comparisons: Equality checks and hash computations only compare the 64-bit pointers, making these operations extremely fast and constant-time.
- Lazy Evaluation: The actual string data is only accessed when necessary (e.g., during serialization), minimizing unnecessary memory access.
Limitations
- The
codecomponent of aStaticIdcannot exceed 32 bytes. - The
venuecomponent of aStaticIdcannot exceed 16 bytes. - Attempting to create a
StaticIdwith components exceeding these limits will result in truncation.
Usage
StaticId combines a Code (up to 32 bytes) and a Venue (up to 16 bytes) into an interned identifier:
use crateStaticId;
// Create from string slices
let id = from_str;
// Create from byte slices
let id_bytes = from_bytes;
assert_eq!;
assert_eq!;
// Get the length of the combined code and venue
println!; // Outputs the sum of code and venue lengths
// Get the number of unique StaticIds in the cache
println!;
// Fast equality check (compares only 8 bytes)
let id2 = from_str;
assert_eq!;
// Memory usage
println!; // Outputs: 8 bytes
Installation
Add this to your Cargo.toml:
[]
= "0.1"
Performance Considerations
- The first creation of a
StaticIdwith unique content will involve allocation and interning. Subsequent creations ofStaticIds with the same content will reuse the interned value. - While creation and interning have some overhead, subsequent operations like equality checks and hashing are extremely fast.
- The library uses a global cache for interning, which is protected by a mutex. In highly concurrent scenarios, this could potentially become a bottleneck.
- The
cache_len()method allows you to monitor the size of the intern cache, which can be useful for understanding memory usage in your application.
License
This project is licensed under [LICENSE NAME] - see the LICENSE.md file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.