beef
Alternative implementation of Cow
that's more compact in memory.
use Cow;
let borrowed = borrowed;
let owned = from;
assert_eq!;
// beef::Cow is 3 word sized, while std::borrow::Cow is 4 word sized
assert!;
How does it work?
The standard library Cow
is an enum with two variants:
For the most common pairs of values - &str
and String
, or &[u8]
and Vec<u8>
- this
means that the entire enum is 4 words wide:
Padding
|
v
+----------+----------+----------+----------+
Borrowed: | Tag | Pointer | Length | XXXXXXXX |
+----------+----------+----------+----------+
+----------+----------+----------+----------+
Owned: | Tag | Pointer | Length | Capacity |
+----------+----------+----------+----------+
Instead of being an enum with a tag, beef::Cow
uses capacity to determine whether the
value it's holding is owned (capacity is greater than 0), or borrowed (capacity is 0).