https://github.com/wyfo/smallvec-handle
An efficient small vector implementation
https://github.com/wyfo/smallvec-handle
Last synced: 7 months ago
JSON representation
An efficient small vector implementation
- Host: GitHub
- URL: https://github.com/wyfo/smallvec-handle
- Owner: wyfo
- License: mit
- Created: 2025-01-19T09:16:07.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-01-19T22:48:55.000Z (9 months ago)
- Last Synced: 2025-01-19T23:35:14.726Z (9 months ago)
- Language: Rust
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# smallvec-handle
A `Vec`-like container that can store a small number of elements inline.
Contrary to alternative small vector implementations, which use a tagged union to distinguish
between local and heap storage, this one uses a single pointer for both local and heap slice.Because the pointer may be invalidated when the vector is moved, it must be checked before each
operation on the vector. To avoid needlessly repeating this check, it is done only once, while
retrieving a "handle" to the vector. The handle is then used for every subsequent operations,
hence the crate name.When the whole data can fit in the local array, it allows saving an allocation, and may have
better cache locality than a regular `Vec`. Also, it should be more performant than using a
tagged union implementation, because it avoids branching at each operation.## Example
```rust
use smallvec_handle::SmallVec;
let mut vec = SmallVec::::new();
let mut vec_handle = vec.handle();
vec_handle.push(0);
vec_handle.push(1);
assert_eq!(vec_handle, [0, 1]);
assert_eq!(vec.as_slice(), [0, 1]);
```