Crate zerovec
Zero-copy vector abstractions for arbitrary types, backed by byte slices.
zerovec enables a far wider range of types — beyond just &[u8] and &str — to participate in
zero-copy deserialization from byte slices. It is serde compatible and comes equipped with
proc macros
Clients upgrading to zerovec benefit from zero heap allocations when deserializing
read-only data.
This crate has four main types:
ZeroVec<'a, T>(andZeroSlice<T>) for fixed-width types likeu32VarZeroVec<'a, T>(andVarZeroSlice<T>) for variable-width types likestrZeroMap<'a, K, V>to map fromKtoVZeroMap2d<'a, K0, K1, V>to map from the pair(K0, K1)toV
The first two are intended as close-to-drop-in replacements for Vec<T> in Serde structs. The third and fourth are
intended as a replacement for HashMap or LiteMap. When used with Serde derives, be sure to apply
#[serde(borrow)] to these types, same as one would for Cow<'a, T>.
ZeroVec<'a, T>, VarZeroVec<'a, T>, ZeroMap<'a, K, V>, and ZeroMap2d<'a, K0, K1, V> all behave like
Cow<'a, T> in that they abstract over either borrowed or owned data. When performing deserialization
from human-readable formats (like json and xml), typically these types will allocate and fully own their data, whereas if deserializing
from binary formats like bincode and postcard, these types will borrow data directly from the buffer being deserialized from,
avoiding allocations and only performing validity checks. As such, this crate can be pretty fast (see below for more information)
on deserialization.
See the design doc for details on how this crate works under the hood.
Cargo features
This crate has several optional Cargo features:
serde: Allows serializing and deserializingzerovec's abstractions viaserdeyoke: Enables implementations ofYokeablefrom theyokecrate, which is also useful in situations involving a lot of zero-copy deserialization.derive: Makes it easier to use custom types in these collections by providing the#[make_ule]and#[make_varule]proc macros, which generate appropriateULEandVarULE-conformant types for a given "normal" type.std: Enabledstd::Errorimplementations for error types. This crate is by defaultno_stdwith a dependency onalloc.
Examples
Serialize and deserialize a struct with ZeroVec and VarZeroVec with Bincode:
# // feature = "serde"
Use custom types inside of ZeroVec:
# // feature = serde and derive
Performance
zerovec is designed for fast deserialization from byte buffers with zero memory allocations
while minimizing performance regressions for common vector operations.
Benchmark results on x86_64:
| Operation | Vec<T> |
zerovec |
|---|---|---|
Deserialize vec of 100 u32 |
233.18 ns | 14.120 ns |
Compute sum of vec of 100 u32 (read every element) |
8.7472 ns | 10.775 ns |
Binary search vec of 1000 u32 50 times |
442.80 ns | 472.51 ns |
| Deserialize vec of 100 strings | 7.3740 μs* | 1.4495 μs |
| Count chars in vec of 100 strings (read every element) | 747.50 ns | 955.28 ns |
| Binary search vec of 500 strings 10 times | 466.09 ns | 790.33 ns |
* This result is reported for Vec<String>. However, Serde also supports deserializing to the partially-zero-copy Vec<&str>; this gives 1.8420 μs, much faster than Vec<String> but a bit slower than zerovec.
| Operation | HashMap<K,V> |
LiteMap<K,V> |
ZeroMap<K,V> |
|---|---|---|---|
| Deserialize a small map | 2.72 μs | 1.28 μs | 480 ns |
| Deserialize a large map | 50.5 ms | 18.3 ms | 3.74 ms |
| Look up from a small deserialized map | 49 ns | 42 ns | 54 ns |
| Look up from a large deserialized map | 51 ns | 155 ns | 213 ns |
Small = 16 elements, large = 131,072 elements. Maps contain <String, String>.
The benches used to generate the above table can be found in the benches directory in the project repository.
zeromap benches are named by convention, e.g. zeromap/deserialize/small, zeromap/lookup/large. The type
is appended for baseline comparisons, e.g. zeromap/lookup/small/hashmap.
Modules
-
maps
This module contains additional utility types and traits for working with
ZeroMapandZeroMap2d. See their docs for more details on the general purpose of these types. - ule Traits over unaligned little-endian data (ULE, pronounced "yule").
-
vecs
This module contains additional utility types for working with
ZeroVecandVarZeroVec. See their docs for more details on the general purpose of these types.
Structs
-
VarZeroCow
Copy-on-write type that efficiently represents
VarULEtypes as their bitstream representation. -
VarZeroSlice
A zero-copy "slice", that works for unsized types, i.e. the zero-copy version of
[T]whereTis notSized. - VarZeroVec A zero-copy, byte-aligned vector for variable-width types.
-
ZeroMap
A zero-copy map datastructure, built on sorted binary-searchable
ZeroVecandVarZeroVec. - ZeroMap2d A zero-copy, two-dimensional map datastructure .
-
ZeroSlice
A zero-copy "slice", i.e. the zero-copy version of
[T]. - ZeroVec A zero-copy, byte-aligned vector for fixed-width types.
Macros
-
impl_ule_from_array
Given
Self($aligned),Self::ULE($unaligned), and a conversion function ($singleorSelf::from_aligned), implementfrom_arrayfor arrays of$alignedto$unaligned. -
zeroslice
Convenience wrapper for
ZeroSlice::from_ule_slice. The value will be created at compile-time, meaning that all arguments must also be constant. -
zerovec
Creates a borrowed
ZeroVec. Convenience wrapper forzeroslice!(...).as_zerovec(). The value will be created at compile-time, meaning that all arguments must also be constant.