Byte-packed Lists
Also known as: bytes buffer binary packed memory layout cache line bitset checksum crc performance
● Advanced3 minBefore this: Lists, Data Types: Sized integers
A list of bytes stores bytes: [UInt8], [Int8] and [Bool] take one byte per item, so binary work runs at cache speed.
fn main() {
let flags: [Bool] = []
for i in 0..10 {
flags.push(i % 3 == 0)
}
print(flags.len())
print(flags)
let small: [UInt8] = [250, 5]
print(small[0] + small[1])
}The program ran and printed:
kavach run packed.kav
10[true, false, false, true, false, false, true, false, false, true]255
Nothing in the code changes: len, printing and indexing work as for any list. Many languages store each item as a full 8-byte word or a pointer; Kavach fits eight items in that space, so eight times as many fit in each line of the processor's cache.
Bytes and checksums
fn main() {
let xs: [UInt8] = []
for i in 0..1_000 {
xs.push(UInt8(i % 251))
}
const b = Bytes(xs)
print(b.len())
print(b.crc32c())
}The program ran and printed:
kavach run bytes.kav
1000301359648
Bytes(list) copies whole words at a time, and crc32c runs on the processor's own CRC instructions.
Still checked
fn main() {
let small: [UInt8] = [250, 10]
print(small.len())
print(small[0] + small[1])
}The program ran, then stopped with a panic:
kavach run overflow.kav
2panic: integer overflow: 250 + 10 doesn't fit in UInt8
What Kavach does for you
Small numbers are packed
A list of bytes uses one byte per item, and lists of 16- and 32-bit numbers are packed too.
Go deeper the why, for readers who want it
crc — build 64 MB byte by byte, checksum it (CRC32C) · WSL Ubuntu 24.04 · i7-13700HX · kavach build (LLVM) · g++ 13 -O2 · best of 9
git clone https://github.com/Mithlesh04/kavach && cd kavach
sh scripts/run-benchmarks.sh "" benchmarks/bytes