# Optimize Reader.takeLeb128 · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/03a3269d48db03f1998166f63ad5195cbba2dd88)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 03a3269d48db03f1998166f63ad5195cbba2dd88

Requested commit: 03a3269d48db03f1998166f63ad5195cbba2dd88

Commit: 03a3269d48db03f1998166f63ad5195cbba2dd88

Tree: b91a0cc027cec970931fba74b41f89d3c63864df

Author: Nico Elbers

Committer: Nico Elbers

## Message

```
Optimize Reader.takeLeb128

Rewrite `Reader.takeLeb128` to not use `takeMultipleOf7Leb128` and
instead:
 * Use byte aligned integers
 * Turn the main reading loop into an inlined loop of static length
 * Special case small integers (<= 7 bits)

Notably signed and unsigned 32 bit integers have 5x to 12x(!)
performance improvement.

Outside of that:
For u8, u16 and u64 performance increases ~1.5x to ~6x
For i8, i16 and i64 performance increases ~1.5x to ~3.5x

For integers with bit multiples of 7 performance is roughly equal within the
margin or error.

Also expand on test coverage

Microbenchmark: https://zigbin.io/242cb1

```

## Parents

- [ceba9572ef5fc3a4c7e2e3f315d05842d2422d0c](https://git.cafe/gitcafe/zig/commit/ceba9572ef5fc3a4c7e2e3f315d05842d2422d0c?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/03a3269d48db03f1998166f63ad5195cbba2dd88?format=markdown)
