---
title: "Variable-Length Integers in Go"
date: 2026-12-01T10:00:00Z
url: https://kmcd.dev/posts/variable-length-integers/
tags: ["golang","performance","protobuf","http3","omnibuf","serialization"]
categories: ["article"]
description: "Comparing Protobuf LEB128, QUIC, OmniBuf, Prefix-9, MessagePack, and fixed-width 64-bit integers in Go across wire size, CPU speed, compression, and branch prediction."
---


I've run into variable-length integers a few times recently, first when [benchmarking Protobuf varints against fixed-width integers](/posts/protobuf-varint-vs-fixed/) and again while implementing QUIC's framing for [HTTP/3 From Scratch](/posts/http3-from-scratch-part-1/). Comparing those two encodings is what pushed me to design a different varint strategy for [OmniBuf](/posts/omnibuf/).

I ended up benchmarking six different integer encodings in Go. I was originally interested in the tradeoff between CPU time and wire size, but some of the results were pretty unexpected. For example, sorting the input before encoding it made some of the decoders run three times as fast.

## Why Variable-Length Integers?

So... why do we want to use variable-length integers, anyway?

Let's use the number `300`. In binary, `300` is `1 0010 1100` (9 bits). Send it over the wire as a big-endian, fixed-width 64-bit unsigned integer (`uint64`), and those bits look like this:

```text
00000000 00000000
00000000 00000000
00000000 00000000
00000001 00101100
```

That's 55 bits of nothing. If we're encoding something like a Protobuf field number or an array length, we probably don't need anywhere near 64 bits.

We could remove those leading zeros, but then how would we encode two integers next to each other? If I write out the bits for `300` immediately followed by the bits for `5`, there's no way to recover those two numbers unless we know their lengths, which naively, would be another number we have to encode! The good news is that there are quite a few ways to solve this and we're going to cover a few of the popular ones.

## Six Integer Encodings

### Protobuf / LEB128

Protobuf uses [LEB128](https://protobuf.dev/programming-guides/encoding/#varints) (Little-Endian Base 128). Read a byte, take the bottom 7 bits, and check the top bit (the continuation bit). If it's set, read another byte and place its bottom 7 bits above the previous ones. Repeat until you get a byte where the top bit is zero.

{{< diagram >}}
{{< image src="layout-leb128.svg" class="center" inline="true" >}}
{{< /diagram >}}

There are already two implementations of this in Go that we can benchmark: [`encoding/binary.Uvarint`](https://pkg.go.dev/encoding/binary#Uvarint) in the standard library and [`protowire.ConsumeVarint`](https://pkg.go.dev/google.golang.org/protobuf/encoding/protowire#ConsumeVarint) in the Protobuf library. The Protobuf implementation unrolls the decoding loop, while the standard library uses a regular loop. I also wrote my own version (`LEB128 (fast1B)` in the benchmarks below) that handles single-byte integers in the outer loop, which appears to be a bit more efficient in many cases:

```go
func DecodeLEB128(src []byte, dst []uint64) int {
	i, n := 0, 0
	for i < len(src) {
		b := src[i]
		if b < 0x80 {
			dst[n] = uint64(b)
			i++
			n++
			continue
		}
		v := uint64(b & 0x7f)
		i++
		var shift uint = 7
		for {
			b = src[i]
			i++
			v |= uint64(b&0x7f) << shift
			if b < 0x80 {
				break
			}
			shift += 7
		}
		dst[n] = v
		n++
	}
	return n
}
```

We get 7 bits of our integer per byte, so an integer between 0 and 127 takes one byte, but we need as many as ten bytes to store a `uint64`.

There's also an issue with negative numbers in Protobuf. `-1` takes *ten bytes* with `int32` or `int64`. Yes, a 32-bit type actually uses 10 bytes to represent `-1` because Protobuf sign-extends negative `int32` values to 64-bit two's complement before varint encoding! Protobuf provides the `sint32` and `sint64` types, which use ZigZag encoding to map small negative numbers to small unsigned numbers before encoding them. Formats like DWARF and WebAssembly also use Signed LEB128 (SLEB128), which can represent small negative numbers with no ZigZag encoding.

### QUIC / HTTP/3

QUIC does something quite different from Protobuf. In [RFC 9000 Section 16](https://www.rfc-editor.org/rfc/rfc9000.html#name-variable-length-integer-enc), the top two bits of the first byte tell us how many bytes to read. There are only four possible sizes: 1, 2, 4, and 8 bytes. The rest of the bits contain the integer in big-endian order.

{{< diagram >}}
{{< image src="layout-quic.svg" class="center" inline="true" >}}
{{< /diagram >}}

```go
func DecodeQUIC(src []byte, dst []uint64) int {
	i, n := 0, 0
	for i < len(src) {
		b0 := src[i]
		switch b0 >> 6 {
		case 0:
			dst[n] = uint64(b0)
			i++
		case 1:
			dst[n] = uint64(binary.BigEndian.Uint16(src[i:]) & 0x3fff)
			i += 2
		case 2:
			dst[n] = uint64(binary.BigEndian.Uint32(src[i:]) & 0x3fffffff)
			i += 4
		case 3:
			dst[n] = binary.BigEndian.Uint64(src[i:]) & 0x3fffffffffffffff
			i += 8
		}
		n++
	}
	return n
}
```

What I like about this encoding is that we know exactly how many bytes to read after looking at the first byte. We can also use the functions in `encoding/binary` to read the remaining bits in one go.

With this encoding, we can't store anything larger than `2^62 - 1`. That's fine for HTTP/3 frame lengths, but I wanted OmniBuf to support the entire `uint64` range. Also, this is big endian byte ordering AKA network ordering. Almost all modern processors use little-endian byte ordering. Google actually used little endian byte ordering in the original versions of QUIC (aka gQUIC) and it was switched to big endian when being adopted as a standard by the IETF.

{{< figure src="quic-endian-slide.png" alt="Slide 2 from the IETF 97 QUIC endianness presentation: Big ('Every network protocol ever*') vs Little ('Every CPU in existence*')" caption="Martin Thomson's QUIC Endian slide from IETF 97 ([watch the discussion on YouTube](https://youtu.be/u9C4k_Zia54?t=6981))." >}}

Honestly, this alone is an interesting story and worthy of a full article. But that will wait for another time.

### OmniBuf

For [OmniBuf](https://omnibuf.com/how-it-works#prefix-varints), I wanted something like QUIC's varints that still kept the full 64-bit range and could fit `127` in a single byte. I also wanted little-endian byte order to avoid byte swapping most CPUs.

I went with a variation of prefix varints, similar to Rust's [`vint64`](https://docs.rs/vint64/latest/vint64/) and [LLVM's MLIR bytecode varints](https://mlir.llvm.org/docs/BytecodeFormat/). Instead of QUIC's top two bits, we count the trailing zeros at the bottom of the first byte. Since the integer is little-endian, keeping the tag in the low bits leaves all the payload bits contiguous so we can strip it with a single right shift:

- `...1` (0 trailing zeros): 1 byte, 7 payload bits (`0..127`)
- `...10` (1 trailing zero): 2 bytes, 14 payload bits (`0..16,383`)
- `...100` (2 trailing zeros): 4 bytes, 29 payload bits (`0..536,870,911`)
- `0x00`: 9 bytes, followed by a raw 8-byte little-endian `uint64`

{{< diagram >}}
{{< image src="layout-omnibuf.svg" class="center" inline="true" >}}
{{< /diagram >}}

```go
func DecodeOmniBuf(src []byte, dst []uint64) int {
	i, n := 0, 0
	for i < len(src) {
		b0 := src[i]
		switch {
		case b0&1 != 0:
			dst[n] = uint64(b0 >> 1)
			i++
		case b0&2 != 0:
			dst[n] = uint64(binary.LittleEndian.Uint16(src[i:]) >> 2)
			i += 2
		case b0&4 != 0:
			dst[n] = uint64(binary.LittleEndian.Uint32(src[i:]) >> 3)
			i += 4
		case b0 == 0:
			dst[n] = binary.LittleEndian.Uint64(src[i+1:])
			i += 9
		default:
			return n
		}
		n++
	}
	return n
}
```

By limiting the encoded widths to 1, 2, 4, and 9 bytes, we can extract each payload with a single `uint8`, `uint16`, `uint32`, or `uint64` read. Anything at `2^29` or above takes nine bytes, even when LEB128 would only use five.

### Prefix-9

What if we didn't skip those other widths? Each additional trailing zero in the first byte indicates another byte of encoded data: zero trailing zeros for 1 byte, one for 2 bytes, two for 3 bytes, and so on up to 9 bytes. (That also means OmniBuf's 4-byte encoding gets 29 payload bits, one more than Prefix-9's 28.) I'll call this encoding `Prefix-9` throughout the benchmarks.

It takes the same number of bytes as LEB128 for integers up to 56 bits. Anything larger gets a zero byte followed by a `uint64`, so we never need that tenth byte that LEB128 sometimes uses.

For values up to `16,383` (including `300`), Prefix-9 produces the exact same bytes as OmniBuf. Switching to `70,000` shows the 3-byte case that OmniBuf skips:

{{< diagram >}}
{{< image src="layout-prefix9.svg" class="center" inline="true" >}}
{{< /diagram >}}

There's a neat trick we can do with this format. Since the number of trailing zeros tells us both the length of the integer and how many bits we need to remove, we can decode the first eight widths with no switch statement. Read eight bytes and shift the bits we don't want off either end:

```go
tz := uint(bits.TrailingZeros8(b0))
width := tz + 1
word := binary.LittleEndian.Uint64(src[i:])
shiftLeft := (8 - width) * 8
dst[n] = (word << shiftLeft) >> (shiftLeft + width)
i += int(width)
```

In the full decoder, we check for the `0x00` 9-byte escape first and fall back to a byte-by-byte loop when fewer than eight bytes remain.

I also wanted to see how much we'd gain by special-casing small integers again. `Prefix-9 (fast1B)` is the same decoder with an extra `if b0&1 != 0` check before it does any of this work.

### MessagePack

[MessagePack](https://github.com/msgpack/msgpack/blob/master/spec.md#int-format-family) (MsgPack) is a self-describing format where the first byte encodes the value's type (and, for small integers, the value itself). Values from 0 to 127 just use `0x00` through `0x7f` directly, so they don't need a separate tag byte.

To encode `300` as an unsigned integer, we first write `0xcd` (the `uint16` type) and then write the two bytes of the integer in big-endian order.

{{< diagram >}}
{{< image src="layout-msgpack.svg" class="center" inline="true" >}}
{{< /diagram >}}

```go
b0 := src[i]
if b0 <= 127 {
	dst[n] = uint64(b0)
	i++
} else {
	switch b0 {
	case 0xcc:
		dst[n] = uint64(src[i+1])
		i += 2
	case 0xcd:
		dst[n] = uint64(binary.BigEndian.Uint16(src[i+1:]))
		i += 3
	case 0xce:
		dst[n] = uint64(binary.BigEndian.Uint32(src[i+1:]))
		i += 5
	case 0xcf:
		dst[n] = binary.BigEndian.Uint64(src[i+1:])
		i += 9
	}
}
```

The switch here looks a lot like the one for QUIC, except we're spending a whole byte on the type rather than a couple of bits from the integer.

### Fixed64

I also want to have a baseline for all of these benchmarks where we just write out the eight bytes of each `uint64` in little-endian order.

{{< diagram >}}
{{< image src="layout-fixed64.svg" class="center" inline="true" >}}
{{< /diagram >}}

There's an interesting opportunity to cheat a bit here. On a little-endian machine, the bytes we want to write are already laid out correctly in memory. We can use `unsafe.Slice` to treat our integers as a slice of bytes and just call `copy`.

I'll benchmark both that version (`Fixed64 Unsafe`) and a regular Go loop (`Fixed64`).

```go
func DecodeFixed64(src []byte, dst []uint64) int {
	n := len(src) / 8
	dst = dst[:n]
	for j := range dst {
		dst[j] = binary.LittleEndian.Uint64(src[:8])
		src = src[8:]
	}
	return n
}

func DecodeFixed64Unsafe(src []byte, dst []uint64) int {
	n := len(src) / 8
	if n == 0 {
		return 0
	}
	dst = dst[:n]
	out := unsafe.Slice((*byte)(unsafe.Pointer(unsafe.SliceData(dst))), n*8)
	copy(out, src[:n*8])
	return n
}
```

I originally wrote that loop with `src[j*8:]`, but reslicing with `src[:8]` and `src = src[8:]` lets the Go compiler drop a bounds check inside `binary.LittleEndian.Uint64`. That one change made Fixed64 noticeably faster.

### Other Schemes

There are a few other varint encodings that I didn't benchmark.

Group Varint (Varint-GB) encodes four integers together. The first byte contains four 2-bit lengths, so we can figure out where all four integers are before we read any of them. This is particularly useful for decoding arrays with SIMD instructions like `pshufb` or `tbl`. It doesn't fit quite as nicely into formats where we're encoding one integer at a time.

[CBOR](https://www.rfc-editor.org/rfc/rfc8949.html#section-3) is similar to MessagePack, except its first byte packs a 3-bit major type with a 5-bit value. Anything from `0` to `23` lives right in those 5 bits, and values `24` through `27` mean a 1, 2, 4, or 8-byte integer follows.

[SQLite4](https://sqlite.org/src4/doc/trunk/www/varint.wiki) went a different route: its varints sort in the same order whether you compare the raw bytes or the decoded unsigned integers, which is handy for database indexes.

### Format Summary

| Format | Widths (B) | Endian | 1-Byte | Max |
| :--- | :--- | :---: | :---: | :---: |
| **LEB128** | 1–10 | LE | `0..127` | 64-bit |
| **QUIC** | 1, 2, 4, 8 | BE | `0..63` | 62-bit |
| **OmniBuf** | 1, 2, 4, 9 | LE | `0..127` | 64-bit |
| **Prefix-9** | 1–9 | LE | `0..127` | 64-bit |
| **MessagePack** | 1, 2, 3, 5, 9 | BE | `0..127` | 64-bit |
| **Fixed64** | 8 | LE | None | 64-bit |

## Benchmark Setup

All of these benchmarks run on my M1 Pro MacBook with Go 1.27.4 (`-count=3 -benchtime=250ms`), and the numbers below are the means across those three runs. Each benchmark processes 10,000 integers at a time and reuses buffers small enough to stay in CPU cache.

My hand-rolled decoders also cheat a bit. They assume the input is valid and the output slice is already large enough, so they skip the bounds and error checks that `encoding/binary` and `protowire` have to run on every call.

I generated six datasets of 10,000 integers each:

| Dataset | Values |
| :--- | :--- |
| `Small` | Random integers from 0 to 63 |
| `Large` | Random 41-bit integers around `2^40` |
| `FullWidth` | Uniformly distributed random `uint64` values |
| `Skewed` | 70% small, 20% medium, 8% big, 2% huge (shuffled) |
| `MixedRandom` | 25% of each of the four sizes, shuffled |
| `MixedSorted` | The same integers as `MixedRandom`, sorted |

For `Skewed` and the two mixed datasets, I'm using these ranges:

| Bucket | Range |
| :--- | :--- |
| Small | `0..127` |
| Medium | `128..16,127` |
| Big | `65,536..100M` |
| Huge | Around `2^40` |

I used 63 as the upper limit for `Small` so that even QUIC can encode everything in one byte. I've left QUIC out of the `FullWidth` benchmarks because it can't encode the entire `uint64` range.

## Wire Size

Here is the encoded byte count for 10,000 `uint64` values across each dataset:

{{< tabs >}}
{{< tab name="Skewed" >}}
{{< chart sort="asc" unit="B" >}}
{
  "type": "bar",
  "data": {
    "labels": ["LEB128", "Prefix-9", "OmniBuf", "MsgPack", "QUIC", "Fixed64"],
    "datasets": [
      {
        "label": "Bytes",
        "data": [15386, 15386, 16009, 18812, 19266, 80000],
        "backgroundColor": [
          "rgba(255, 107, 74, 0.85)",
          "rgba(148, 163, 184, 0.85)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(56, 189, 248, 0.85)",
          "rgba(168, 85, 247, 0.85)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Size: Skewed (bytes)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< tab name="Mixed" >}}
{{< chart sort="asc" unit="B" >}}
{
  "type": "bar",
  "data": {
    "labels": ["LEB128", "Prefix-9", "QUIC", "OmniBuf", "MsgPack", "Fixed64"],
    "datasets": [
      {
        "label": "Bytes",
        "data": [32706, 32706, 39135, 40491, 45440, 80000],
        "backgroundColor": [
          "rgba(255, 107, 74, 0.85)",
          "rgba(148, 163, 184, 0.85)",
          "rgba(56, 189, 248, 0.85)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(168, 85, 247, 0.85)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Size: Mixed (bytes)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< tab name="Large (2^40)" >}}
{{< chart sort="asc" unit="B" >}}
{
  "type": "bar",
  "data": {
    "labels": ["LEB128", "Prefix-9", "QUIC", "Fixed64", "OmniBuf", "MsgPack"],
    "datasets": [
      {
        "label": "Bytes",
        "data": [60000, 60000, 80000, 80000, 90000, 90000],
        "backgroundColor": [
          "rgba(255, 107, 74, 0.85)",
          "rgba(148, 163, 184, 0.85)",
          "rgba(56, 189, 248, 0.85)",
          "rgba(168, 85, 247, 0.85)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(251, 191, 36, 0.85)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Size: Large (bytes)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< tab name="FullWidth" >}}
{{< chart sort="asc" unit="B" >}}
{
  "type": "bar",
  "data": {
    "labels": ["Fixed64", "Prefix-9", "OmniBuf", "MsgPack", "LEB128"],
    "datasets": [
      {
        "label": "Bytes",
        "data": [80000, 89967, 90000, 90000, 94988],
        "backgroundColor": [
          "rgba(168, 85, 247, 0.85)",
          "rgba(148, 163, 184, 0.85)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(255, 107, 74, 0.85)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Size: FullWidth 64-bit (bytes)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< tab name="Small" >}}
{{< chart sort="asc" unit="B" >}}
{
  "type": "bar",
  "data": {
    "labels": ["LEB128", "Prefix-9", "QUIC", "OmniBuf", "MsgPack", "Fixed64"],
    "datasets": [
      {
        "label": "Bytes",
        "data": [10000, 10000, 10000, 10000, 10000, 80000],
        "backgroundColor": [
          "rgba(255, 107, 74, 0.85)",
          "rgba(148, 163, 184, 0.85)",
          "rgba(56, 189, 248, 0.85)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(168, 85, 247, 0.85)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Size: Small (bytes)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< /tabs >}}

<details>
<summary><b>Show wire size table</b></summary>

| Codec | Small | Large (`2^40`) | FullWidth | Skewed | Mixed |
| :--- | :---: | :---: | :---: | :---: | :---: |
| **LEB128** | **10,000 B** | **60,000 B** | 94,988 B | **15,386 B** | **32,706 B** |
| **Prefix-9** | **10,000 B** | **60,000 B** | 89,967 B | **15,386 B** | **32,706 B** |
| **OmniBuf** | **10,000 B** | 90,000 B | 90,000 B | 16,009 B | 40,491 B |
| **MsgPack** | **10,000 B** | 90,000 B | 90,000 B | 18,812 B | 45,440 B |
| **QUIC** | **10,000 B** | 80,000 B | N/A | 19,266 B | 39,135 B |
| **Fixed64** | 80,000 B | 80,000 B | **80,000 B** | 80,000 B | 80,000 B |

</details>

The `Skewed` dataset is roughly what I had in mind when designing OmniBuf's varints. I gave up five of Prefix-9's possible byte widths, and it only cost me about 4% in this dataset.

That decision looks considerably worse on `Large`. OmniBuf needs nine bytes for every integer, while LEB128 and Prefix-9 only need six.

There's also not much point in using a varint for something like a random 64-bit hash. On `FullWidth`, all of these varint formats make the data bigger than it was to begin with.

## Decode Speed

{{< tabs >}}
{{< tab name="Skewed" >}}
{{< chart sort="asc" unit="ns" >}}
{
  "type": "bar",
  "data": {
    "labels": ["Fixed64 Unsafe", "Fixed64", "OmniBuf", "MsgPack", "LEB128 (fast1B)", "QUIC", "Prefix-9 (fast1B)", "Prefix-9", "LEB128 (stdlib)", "LEB128 (protowire)"],
    "datasets": [
      {
        "label": "ns/op",
        "data": [927, 7501, 9465, 10365, 10582, 12537, 15960, 28824, 33448, 40911],
        "backgroundColor": [
          "rgba(192, 132, 252, 0.95)",
          "rgba(168, 85, 247, 0.55)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(255, 107, 74, 0.85)",
          "rgba(56, 189, 248, 0.85)",
          "rgba(148, 163, 184, 0.95)",
          "rgba(148, 163, 184, 0.55)",
          "rgba(255, 107, 74, 0.35)",
          "rgba(255, 107, 74, 0.6)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Decode: Skewed (ns/op)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< tab name="Small" >}}
{{< chart sort="asc" unit="ns" >}}
{
  "type": "bar",
  "data": {
    "labels": ["Fixed64 Unsafe", "MsgPack", "OmniBuf", "LEB128 (fast1B)", "Prefix-9 (fast1B)", "Fixed64", "QUIC", "LEB128 (stdlib)", "LEB128 (protowire)", "Prefix-9"],
    "datasets": [
      {
        "label": "ns/op",
        "data": [920, 6264, 6305, 6335, 6340, 7459, 8456, 13359, 20969, 28638],
        "backgroundColor": [
          "rgba(192, 132, 252, 0.95)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(255, 107, 74, 0.85)",
          "rgba(148, 163, 184, 0.95)",
          "rgba(168, 85, 247, 0.55)",
          "rgba(56, 189, 248, 0.85)",
          "rgba(255, 107, 74, 0.35)",
          "rgba(255, 107, 74, 0.6)",
          "rgba(148, 163, 184, 0.55)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Decode: Small (ns/op)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< tab name="Large" >}}
{{< chart sort="asc" unit="ns" >}}
{
  "type": "bar",
  "data": {
    "labels": ["Fixed64 Unsafe", "Fixed64", "QUIC", "OmniBuf", "MsgPack", "Prefix-9 (fast1B)", "Prefix-9", "LEB128 (protowire)", "LEB128 (fast1B)", "LEB128 (stdlib)"],
    "datasets": [
      {
        "label": "ns/op",
        "data": [947, 7428, 9444, 15651, 15712, 28143, 28552, 29969, 32862, 43897],
        "backgroundColor": [
          "rgba(192, 132, 252, 0.95)",
          "rgba(168, 85, 247, 0.55)",
          "rgba(56, 189, 248, 0.85)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(148, 163, 184, 0.95)",
          "rgba(148, 163, 184, 0.55)",
          "rgba(255, 107, 74, 0.6)",
          "rgba(255, 107, 74, 0.85)",
          "rgba(255, 107, 74, 0.35)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Decode: Large (ns/op)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< tab name="FullWidth" >}}
{{< chart sort="asc" unit="ns" >}}
{
  "type": "bar",
  "data": {
    "labels": ["Fixed64 Unsafe", "Prefix-9", "Fixed64", "Prefix-9 (fast1B)", "MsgPack", "OmniBuf", "LEB128 (protowire)", "LEB128 (fast1B)", "LEB128 (stdlib)"],
    "datasets": [
      {
        "label": "ns/op",
        "data": [932, 7187, 8139, 8288, 15629, 15729, 65361, 79249, 98850],
        "backgroundColor": [
          "rgba(192, 132, 252, 0.95)",
          "rgba(148, 163, 184, 0.55)",
          "rgba(168, 85, 247, 0.55)",
          "rgba(148, 163, 184, 0.95)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(255, 107, 74, 0.6)",
          "rgba(255, 107, 74, 0.85)",
          "rgba(255, 107, 74, 0.35)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Decode: FullWidth 64-bit (ns/op)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< tab name="MixedRandom" >}}
{{< chart sort="asc" unit="ns" >}}
{
  "type": "bar",
  "data": {
    "labels": ["Fixed64 Unsafe", "Fixed64", "OmniBuf", "MsgPack", "Prefix-9", "Prefix-9 (fast1B)", "QUIC", "LEB128 (fast1B)", "LEB128 (protowire)", "LEB128 (stdlib)"],
    "datasets": [
      {
        "label": "ns/op",
        "data": [925, 7491, 21589, 25216, 29969, 30635, 34263, 36932, 60815, 63278],
        "backgroundColor": [
          "rgba(192, 132, 252, 0.95)",
          "rgba(168, 85, 247, 0.55)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(148, 163, 184, 0.55)",
          "rgba(148, 163, 184, 0.95)",
          "rgba(56, 189, 248, 0.85)",
          "rgba(255, 107, 74, 0.85)",
          "rgba(255, 107, 74, 0.6)",
          "rgba(255, 107, 74, 0.35)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Decode: MixedRandom (ns/op)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< tab name="MixedSorted" >}}
{{< chart sort="asc" unit="ns" >}}
{
  "type": "bar",
  "data": {
    "labels": ["Fixed64 Unsafe", "Fixed64", "QUIC", "OmniBuf", "MsgPack", "LEB128 (fast1B)", "Prefix-9 (fast1B)", "LEB128 (protowire)", "LEB128 (stdlib)", "Prefix-9"],
    "datasets": [
      {
        "label": "ns/op",
        "data": [916, 7511, 9704, 10706, 11946, 18881, 22789, 25045, 26983, 28673],
        "backgroundColor": [
          "rgba(192, 132, 252, 0.95)",
          "rgba(168, 85, 247, 0.55)",
          "rgba(56, 189, 248, 0.85)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(255, 107, 74, 0.85)",
          "rgba(148, 163, 184, 0.95)",
          "rgba(255, 107, 74, 0.6)",
          "rgba(255, 107, 74, 0.35)",
          "rgba(148, 163, 184, 0.55)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Decode: MixedSorted (ns/op)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< /tabs >}}

<details>
<summary><b>Show full decode table (ns/op for 10,000 values; Skewed ns/int is the average time per integer)</b></summary>

| Codec | Small | Large | FullWidth | Skewed | Skewed (ns/int) | MixedRandom | MixedSorted |
| :--- | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| **Fixed64 Unsafe** | **920** | **947** | **932** | **927** | **0.09** | **925** | **916** |
| **Fixed64** | 7,459 | 7,428 | 8,139 | 7,501 | 0.75 | 7,491 | 7,511 |
| **OmniBuf** | 6,305 | 15,651 | 15,729 | 9,465 | 0.95 | 21,589 | 10,706 |
| **MsgPack** | 6,264 | 15,712 | 15,629 | 10,365 | 1.04 | 25,216 | 11,946 |
| **LEB128 (fast1B)** | 6,335 | 32,862 | 79,249 | 10,582 | 1.06 | 36,932 | 18,881 |
| **QUIC** | 8,456 | 9,444 | N/A | 12,537 | 1.25 | 34,263 | 9,704 |
| **Prefix-9 (fast1B)** | 6,340 | 28,143 | 8,288 | 15,960 | 1.60 | 30,635 | 22,789 |
| **Prefix-9** | 28,638 | 28,552 | 7,187 | 28,824 | 2.88 | 29,969 | 28,673 |
| **LEB128 (stdlib)** | 13,359 | 43,897 | 98,850 | 33,448 | 3.34 | 63,278 | 26,983 |
| **LEB128 (protowire)** | 20,969 | 29,969 | 65,361 | 40,911 | 4.09 | 60,815 | 25,045 |

</details>

On `Small`, every hand-rolled decoder with a 1-byte fast path takes around 6.3 µs: MsgPack, OmniBuf, LEB128 (fast1B), and Prefix-9 (fast1B). Neither library implementation matches the hand-rolled fast path on `Small`; function-call overhead and input validation are likely contributors. On `FullWidth`, protowire's unrolling beats both other LEB128 decoders, but it is still over 4x slower than OmniBuf and about 9x slower than Prefix-9. Prefix-9 has an advantage here because it checks for the 9-byte escape first, while OmniBuf has to check the other three widths before reaching that case.

I should point out the scale of these numbers. The difference between OmniBuf and LEB128 (protowire) on `Skewed` works out to about three nanoseconds per integer. If your Protobuf message has five integer fields, you probably have more important things to optimize.

## Encode Speed

{{< tabs >}}
{{< tab name="Skewed" >}}
{{< chart sort="asc" unit="ns" >}}
{
  "type": "bar",
  "data": {
    "labels": ["Fixed64 Unsafe", "Fixed64", "OmniBuf", "QUIC", "LEB128 (fast1B)", "Prefix-9", "MsgPack", "LEB128 (stdlib)", "LEB128 (protowire)"],
    "datasets": [
      {
        "label": "ns/op",
        "data": [1046, 4283, 14595, 18125, 18478, 18867, 21087, 27225, 41712],
        "backgroundColor": [
          "rgba(192, 132, 252, 0.95)",
          "rgba(168, 85, 247, 0.55)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(56, 189, 248, 0.85)",
          "rgba(255, 107, 74, 0.85)",
          "rgba(148, 163, 184, 0.85)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(255, 107, 74, 0.35)",
          "rgba(255, 107, 74, 0.6)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Encode: Skewed (ns/op)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< tab name="Small" >}}
{{< chart sort="asc" unit="ns" >}}
{
  "type": "bar",
  "data": {
    "labels": ["Fixed64 Unsafe", "Fixed64", "QUIC", "OmniBuf", "LEB128 (fast1B)", "MsgPack", "Prefix-9", "LEB128 (stdlib)", "LEB128 (protowire)"],
    "datasets": [
      {
        "label": "ns/op",
        "data": [1027, 3977, 6322, 6398, 6409, 6445, 6547, 9539, 20725],
        "backgroundColor": [
          "rgba(192, 132, 252, 0.95)",
          "rgba(168, 85, 247, 0.55)",
          "rgba(56, 189, 248, 0.85)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(255, 107, 74, 0.85)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(148, 163, 184, 0.85)",
          "rgba(255, 107, 74, 0.35)",
          "rgba(255, 107, 74, 0.6)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Encode: Small (ns/op)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< tab name="Large" >}}
{{< chart sort="asc" unit="ns" >}}
{
  "type": "bar",
  "data": {
    "labels": ["Fixed64 Unsafe", "Fixed64", "QUIC", "OmniBuf", "Prefix-9", "MsgPack", "LEB128 (stdlib)", "LEB128 (fast1B)", "LEB128 (protowire)"],
    "datasets": [
      {
        "label": "ns/op",
        "data": [955, 4364, 15646, 18769, 21861, 24618, 31650, 34969, 37996],
        "backgroundColor": [
          "rgba(192, 132, 252, 0.95)",
          "rgba(168, 85, 247, 0.55)",
          "rgba(56, 189, 248, 0.85)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(148, 163, 184, 0.85)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(255, 107, 74, 0.35)",
          "rgba(255, 107, 74, 0.85)",
          "rgba(255, 107, 74, 0.6)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Encode: Large (ns/op)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< tab name="FullWidth" >}}
{{< chart sort="asc" unit="ns" >}}
{
  "type": "bar",
  "data": {
    "labels": ["Fixed64 Unsafe", "Fixed64", "OmniBuf", "MsgPack", "Prefix-9", "LEB128 (stdlib)", "LEB128 (fast1B)", "LEB128 (protowire)"],
    "datasets": [
      {
        "label": "ns/op",
        "data": [1048, 3956, 18763, 21876, 34569, 70750, 74935, 77876],
        "backgroundColor": [
          "rgba(192, 132, 252, 0.95)",
          "rgba(168, 85, 247, 0.55)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(148, 163, 184, 0.85)",
          "rgba(255, 107, 74, 0.35)",
          "rgba(255, 107, 74, 0.85)",
          "rgba(255, 107, 74, 0.6)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Encode: FullWidth 64-bit (ns/op)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< tab name="MixedRandom" >}}
{{< chart sort="asc" unit="ns" >}}
{
  "type": "bar",
  "data": {
    "labels": ["Fixed64 Unsafe", "Fixed64", "QUIC", "OmniBuf", "Prefix-9", "LEB128 (fast1B)", "LEB128 (stdlib)", "MsgPack", "LEB128 (protowire)"],
    "datasets": [
      {
        "label": "ns/op",
        "data": [918, 3932, 30974, 32179, 43931, 46156, 51982, 51984, 69636],
        "backgroundColor": [
          "rgba(192, 132, 252, 0.95)",
          "rgba(168, 85, 247, 0.55)",
          "rgba(56, 189, 248, 0.85)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(148, 163, 184, 0.85)",
          "rgba(255, 107, 74, 0.85)",
          "rgba(255, 107, 74, 0.35)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(255, 107, 74, 0.6)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Encode: MixedRandom (ns/op)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< tab name="MixedSorted" >}}
{{< chart sort="asc" unit="ns" >}}
{
  "type": "bar",
  "data": {
    "labels": ["Fixed64 Unsafe", "Fixed64", "QUIC", "OmniBuf", "Prefix-9", "MsgPack", "LEB128 (stdlib)", "LEB128 (fast1B)", "LEB128 (protowire)"],
    "datasets": [
      {
        "label": "ns/op",
        "data": [955, 3994, 11890, 11990, 13591, 16327, 19750, 21560, 27502],
        "backgroundColor": [
          "rgba(192, 132, 252, 0.95)",
          "rgba(168, 85, 247, 0.55)",
          "rgba(56, 189, 248, 0.85)",
          "rgba(45, 212, 191, 0.85)",
          "rgba(148, 163, 184, 0.85)",
          "rgba(251, 191, 36, 0.85)",
          "rgba(255, 107, 74, 0.35)",
          "rgba(255, 107, 74, 0.85)",
          "rgba(255, 107, 74, 0.6)"
        ],
        "borderWidth": 0
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Encode: MixedSorted (ns/op)" },
      "legend": { "display": false }
    },
    "scales": { "x": { "min": 0 } }
  }
}
{{< /chart >}}
{{< /tab >}}
{{< /tabs >}}

<details>
<summary><b>Show full encode table (ns/op)</b></summary>

| Codec | Small | Large | FullWidth | Skewed | MixedRandom | MixedSorted |
| :--- | :---: | :---: | :---: | :---: | :---: | :---: |
| **Fixed64 Unsafe** | **1,027** | **955** | **1,048** | **1,046** | **918** | **955** |
| **Fixed64** | 3,977 | 4,364 | 3,956 | 4,283 | 3,932 | 3,994 |
| **OmniBuf** | 6,398 | 18,769 | 18,763 | 14,595 | 32,179 | 11,990 |
| **QUIC** | 6,322 | 15,646 | N/A | 18,125 | 30,974 | 11,890 |
| **LEB128 (fast1B)** | 6,409 | 34,969 | 74,935 | 18,478 | 46,156 | 21,560 |
| **Prefix-9** | 6,547 | 21,861 | 34,569 | 18,867 | 43,931 | 13,591 |
| **MsgPack** | 6,445 | 24,618 | 21,876 | 21,087 | 51,984 | 16,327 |
| **LEB128 (stdlib)** | 9,539 | 31,650 | 70,750 | 27,225 | 51,982 | 19,750 |
| **LEB128 (protowire)** | 20,725 | 37,996 | 77,876 | 41,712 | 69,636 | 27,502 |

</details>

All of the hand-rolled varint encoders take around 6.4 µs on `Small`, while Fixed64 stays flat around 4 µs in safe Go and around 1 µs with `memmove`. On `FullWidth`, the LEB128 encoders are roughly 4x slower than OmniBuf.

## Branch Prediction

Look back at all those decoder implementations. In the LEB128 loop, we check a bit every time we read a byte. In OmniBuf, QUIC, and MessagePack, we have a switch to decide how many bytes to read.

If we're decoding a long series of small integers, we'll keep taking the same branch. The CPU can predict this and start executing instructions from the next iteration before it has finished the current one. If the widths are constantly changing, the CPU has a much harder time figuring out what to execute next.

Comparing `MixedSorted` against `MixedRandom` gives us a good indication of how sensitive each decoder is to branch prediction. Both inputs contain the exact same 10,000 integers and produce the exact same total byte count. Only the order changes.

{{< chart sort="false" unit="ns" >}}
{
  "type": "bar",
  "data": {
    "labels": ["Fixed64 Unsafe", "Fixed64", "OmniBuf", "MsgPack", "Prefix-9", "Prefix-9 (fast1B)", "QUIC", "LEB128 (fast1B)", "LEB128 (protowire)", "LEB128 (stdlib)"],
    "datasets": [
      {
        "label": "Sorted",
        "data": [916, 7511, 10706, 11946, 28673, 22789, 9704, 18881, 25045, 26983],
        "backgroundColor": "rgba(45, 212, 191, 0.85)",
        "barPercentage": 0.85,
        "categoryPercentage": 0.75
      },
      {
        "label": "Shuffled",
        "data": [925, 7491, 21589, 25216, 29969, 30635, 34263, 36932, 60815, 63278],
        "backgroundColor": "rgba(255, 107, 74, 0.85)",
        "barPercentage": 0.85,
        "categoryPercentage": 0.75
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Decode: Sorted vs Shuffled (ns/op)" }
    },
    "scales": {
      "x": { "min": 0 }
    }
  }
}
{{< /chart >}}

QUIC is the most affected by shuffling (3.5x slower), followed by the LEB128 implementations (2.0x to 2.4x). MessagePack and OmniBuf take roughly twice as long. Prefix-9 (fast1B) slows down by 34%, while the original Prefix-9 and both Fixed64 implementations are largely unaffected.

### Why Branchless Decoding Was Slower

Prefix-9 stayed around 29 µs whether the input was sorted or shuffled. Even on `MixedRandom`, though, it still lost to OmniBuf. And on `FullWidth`, where nearly every integer skips the branchless path and takes the 9-byte escape, it suddenly ran about four times as fast.

So what happened here? I expected the branchless version to do much better on `MixedRandom`.

Part of the problem is a loop-carried data dependency: there's quite a bit of work between reading the first byte of an integer and figuring out where the next integer starts. We have to count the trailing zeros and calculate the width before we know the next offset.

Compare that with OmniBuf's 1-byte case. Once the CPU predicts that branch, the next offset is just `i + 1`. It doesn't have to wait for the previous load to finish before figuring out where to read next.

We're also doing more work to decode the integer itself. In OmniBuf's 1-byte case, we read a byte and shift it right once. Prefix-9 reads eight bytes, counts trailing zeros, and does two variable shifts, even if the integer is just `1`.

Adding a 1-byte fast path to Prefix-9 fixed most of that on `Small` and cut `Skewed` in half while keeping the same wire size. I haven't measured CPU performance counters to confirm how much each factor contributes, but the speed of the 9-byte escape path points in the same direction.

## Fixed-Width Isn't Bad

For all this effort trying to encode integers efficiently, it's worth looking at what happens when we don't do any of it.

With a bit of `unsafe` Go code, we can copy all 80 KB of `Fixed64` data in around a microsecond. That's about 40 times as fast as my `LEB128 (fast1B)` decoder on `MixedRandom` (and over 65 times as fast as `protowire`). Even with safe Go, the fixed-width implementation wins four of the six decode benchmarks against the varint implementations and all of the encode benchmarks.

There's another advantage that none of the benchmarks capture. If I want the 500th integer in an array of `uint64` values, I know exactly where it is. With any of these varint formats, I'd need to scan through the preceding integers or maintain some sort of index.

### What about compression?

What if we're compressing the data anyway? Can we just use `Fixed64` and let the compressor take care of all those leading zeros?

I tested this with `zstd` (`github.com/klauspost/compress` at the default compression level):

| Codec | Small | Skewed | FullWidth |
| :--- | :---: | :---: | :---: |
| **LEB128 (protowire)** | 10,014 B | **15,400 B** | 95,004 B |
| **OmniBuf** | 10,014 B | 15,428 B | 90,016 B |
| **Fixed64 Unsafe** | **7,690 B** | 22,039 B | **80,016 B** |

`zstd` barely touches the varint buffers because the leading zeros have already been stripped out, leaving mostly random bits behind. For `Fixed64`, it easily collapses those runs of zero bytes, shrinking the 80 KB payload down to 7.7 KB on `Small` and 22 KB on `Skewed`.

Even after compression, though, `Fixed64` is still about 43% larger than the varint formats on `Skewed`. And that compression isn't free:

{{< chart sort="false" unit="ns" >}}
{
  "type": "bar",
  "data": {
    "labels": ["LEB128 (protowire)", "OmniBuf", "Fixed64 Unsafe"],
    "datasets": [
      {
        "label": "Encode + zstd",
        "data": [46715, 149725, 387509],
        "backgroundColor": "rgba(255, 107, 74, 0.85)",
        "barPercentage": 0.85,
        "categoryPercentage": 0.72
      },
      {
        "label": "zstd + Decode",
        "data": [42100, 14800, 101280],
        "backgroundColor": "rgba(45, 212, 191, 0.85)",
        "barPercentage": 0.85,
        "categoryPercentage": 0.72
      }
    ]
  },
  "options": {
    "indexAxis": "y",
    "plugins": {
      "title": { "display": true, "text": "Skewed with zstd (ns/op)" }
    },
    "scales": {
      "x": { "min": 0 }
    }
  }
}
{{< /chart >}}

With `Fixed64`, we've gone from copying 80 KB in 1 µs to spending nearly 390 µs compressing it and over 100 µs decompressing it. Meanwhile, raw OmniBuf encodes in 14.6 µs and decodes in 9.5 µs without `zstd`, and still ends up smaller on the wire.

(If you're wondering why `LEB128` encodes so much faster than `OmniBuf` in this chart: `zstd` sees no repeated byte sequences in the LEB128 output and bails out early with an uncompressed block, whereas OmniBuf's 9-byte values contain enough zero bytes to trigger the full compressor just to save 580 bytes.)

## Now what?

I'm still happy with my choice of varint encoding for OmniBuf. And after running these benchmarks, I'd be much more inclined to use fixed-width integers for large arrays of numbers.

All benchmark code, compression tests, and round-trip tests are [on GitHub](https://github.com/sudorandom/kmcd.dev/tree/main/content/posts/2026/variable-length-integers/benchmarks) alongside this post.

Prefix-varints are just one of the wire-format decisions I made for [OmniBuf](/posts/omnibuf/). There are some equally interesting tradeoffs in how it encodes collections, nested messages, and wire types. I'll cover those in future posts.


