Skip to content
xiaostPublic

About

A SIMD-accelerated, 100% compatible drop-in replacement for Go's `encoding/base64`

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

base64

A SIMD-accelerated, 100% compatible drop-in replacement for Go's encoding/base64.

import "github.com/xiaost/base64"

s := base64.StdEncoding.EncodeToString(data) // same API as encoding/base64
b, err := base64.URLEncoding.DecodeString(s)

The whole encoding/base64 API is mirrored, and CorruptInputError is an alias of the stdlib's type.

How it works

Assembly kernels encode and decode whole blocks of plain alphabet bytes, and a valid final decode quantum is decoded in Go. Everything else — encode tails, padding variants, errors, unsupported alphabets or CPUs — goes to encoding/base64 itself, so behavior is identical by construction. Line-wrapped input (PEM, MIME) re-enters the kernels after each run of newlines at a 4-char boundary; other newlines also go to the stdlib. The stream encoder and decoder are ports of the stdlib's (see LICENSE-Go) running on these kernels. Inputs shorter than one block go straight to the stdlib: 16 chars to decode (64 on arm64 for alphabets with bytes from 0x7F up), 24 bytes to encode on arm64, 12 on amd64.

arch kernels encode decode
arm64 NEON any alphabet any alphabet
amd64 AVX2, else SSSE3 alphabets sharing the standard first 62 chars std, URL
other none stdlib stdlib

Build with -tags purego to disable all assembly. The amd64 kernels implement Wojciech Muła's base64 SIMD algorithms; the arm64 kernels follow the design of aklomp/base64.

Compatibility

Differential tests and fuzz targets against encoding/base64 compare results, errors and panic values for predefined and custom encodings, corrupt and line-wrapped input, streams, the zero Encoding, and dst/src aliasing. CI runs the tests on Go 1.22 and stable; locally they also run on SSSE3 and AVX2 under Rosetta 2 (AVX2 with ROSETTA_ADVERTISE_AVX=1).

Still observable after switching the import path:

  • The types are distinct from the stdlib's (as reflection and panic messages show), and == on Encoding values compares internal pointers.
  • Bytes of a decode dst beyond the decoded output are unspecified, also after a panic; the stdlib leaves scratch bytes there.
  • Allocation counts can differ: EncodeToString may allocate less, while constructors allocate more.

Benchmarks

Benchmark output is published by GitHub Actions: github.com/xiaost/base64/actions.

About

A SIMD-accelerated, 100% compatible drop-in replacement for Go's `encoding/base64`

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages