StaticId
This Rust library provides an cache-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, e.g., financial products- Exceptional cache efficiency: Each
StaticIdis represented by a single 64-bit pointer. - 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.
How this works
-
Compact Representation: Each StaticId is stored as a single 64-bit pointer, regardless of the length of the underlying strings. Duplicate values are not allocated in memory; instead, the actual ID value exists in memory once, and each ID owns only a pointer to that memory location.
-
Fast Comparisons: Equality checks and hash computations only compare the 64-bit pointers, making these operations extremely fast and constant-time. Most operations after creation, such as hashing and comparison, use only 8 bytes and are likely to be cache-efficient.
-
Lazy Evaluation: The actual string data is only accessed when necessary (e.g., during serialization or debugging), minimizing unnecessary memory access. Users should be aware that these operations might be slower as they may require accessing the actual value from memory.
-
Creation Overhead: To ensure uniqueness, there's a slight overhead during initial creation (approximately 15 ns per object creation). This trade-off allows for significant performance gains in subsequent operations.
-
Input Constraints: For clarity and efficiency, the input values are limited: 'code' is restricted to 32 characters, and 'venue' to 16 characters.
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. - Accessing the data (e.g., during serialization or debugging) may be slow.
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!;
println!; // => APPL@NASDAQ
// Memory usage
println!; // Outputs: 8 bytes
Installation
Add this to your Cargo.toml:
[]
= "0.1"
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.