segmentio / asm

Go library providing algorithms optimized to leverage the characteristics of modern CPUs
MIT No Attribution
869 stars 36 forks source link

Base64 #37

Closed kalamay closed 3 years ago

kalamay commented 3 years ago

I was curious how fast we could might be able to do some base64 encoding, so I started this based on https://github.com/aklomp/base64/tree/master/lib/arch/avx2 with some tweaks. This is a first pass, but it shows a 10x performance increase without much optimization. The thought is this might be a faster way to encode []byte values in segmentio/encoding/json

goos: darwin
goarch: amd64
pkg: github.com/segmentio/asm/base64
cpu: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
BenchmarkEncode/asm-12           3968252               308.1 ns/op      13293.77 MB/s
BenchmarkEncode/go-12             294000              3500 ns/op        1170.17 MB/s
BenchmarkDecode/asm-12           2917183               414.6 ns/op      13178.48 MB/s
BenchmarkDecode/go-12             314950              3761 ns/op        1452.99 MB/s

The API here matches the go standard library for the Encoding object except there are more restrictions on the encoding alphabet. The alphabet must be one of the known standard variants:

That is, arbitrary alphabets are not supported. It does panic() if the alphabet is not supported. This is in order to maintain drop-in compatibility. Though the use of custom alphabets seems like a rare enough use case that it seems reasonable to opt out of error handling.